Creating a time stamped file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Matt20687
Posts: 54
Joined: 02 May 2012 14:42

Creating a time stamped file

#1 Post by Matt20687 » 02 May 2012 14:49

Hello,

I am in need to create a batch file that kills a service, outputs a timestamped file with some text within saying it was successful and then sends an email to one of my voice highlighting what has been done.

I am happy with all of it and have it working (in a sense) but i cannot figure out how you have the batch file output a text document with the date and time (ddmmyyyymmhh) everytime the batch file is run (there will be a scheduled entry within windows to run the batch file at 6pm every day.

Can anyone help me out with the timestamp issue?

Thanks,
Matt

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Creating a time stamped file

#2 Post by Fawers » 02 May 2012 16:34

ddmmyyyymmhh

ddmmyyyy = Day Month Year
mmhh = Minute Hour
right?

Assuming your DATE "variable" (note the quotes) is in the WWW DD/MM/YYYY (in which WWW is the weekday), this should do it.

Code: Select all

:DATE
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do (
set dd=%%a
set mm=%%b
set yy=%%c
)

:TIME
for /f "tokens=1,2 delims=:" %%a in ("%time%") do (
set hh=%%a
set min=%%b
)

:TIMESTAMP
set timestamp=%dd%%mm%%yy%%min%%hh%

And then you can use %timestamp% wherever you want to.

Result (tested directly from command line):
C:\DOCUME~1\Fawers\Desktop>for /f "tokens=2-4 delims=/ " %a in ("%date%") do (
Mais? set dd=%a
Mais? set mm=%b
Mais? set yy=%c
Mais? )

C:\DOCUME~1\Fawers\Desktop>(
set dd=02
set mm=05
set yy=2012

)

C:\DOCUME~1\Fawers\Desktop>for /f "tokens=1,2 delims=:" %a in ("%time%") do (
Mais? set hh=%a
Mais? set min=%b
Mais? )

C:\DOCUME~1\Fawers\Desktop>(
set hh=19
set min=26

)

C:\DOCUME~1\Fawers\Desktop>set timestamp=%dd%%mm%%yy%%min%%hh%

C:\DOCUME~1\Fawers\Desktop>set tim
timestamp=020520122619

Text in red = command line output.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Creating a time stamped file

#3 Post by foxidrive » 02 May 2012 16:54

In this case for a human readable text file, this command should suffice:

Untested:

Code: Select all

net stop "service" && (echo service stopped on %date% at %time%>file.txt) || (echo service FAILED TO STOP on %date% at %time%>file.txt)


::send email here with file

Post Reply