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
Creating a time stamped file
Moderator: DosItHelp
Re: Creating a time stamped file
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.
Re: Creating a time stamped file
In this case for a human readable text file, this command should suffice:
Untested:
::send email here with file
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