Click to See Complete Forum and Search --> : .CMD or .BAT help


AlbertGM
November 20th, 2006, 12:21 PM
Hi,

I'd need a way to know date and time inside a .BAT o .CMD, save into a variable and write a file with that name.

I call an application (.EXE) from a batch script. If applications fails it returns me an error code which I can catch using ERRORLEVEL. Then I can restart the application again, but I'd like to know when that has happened. My script is like this:
@echo off
:start
Application.exe
if ERRORLEVEL 1 goto err_label
goto exit_label
:err_label
echo "There's an error... Restart" >> error.txt
goto start
:exit_label
echo "Exit success" >> ok.txt
goto end
:end
And instead ofecho "There's an error... Restart" >> error.txtI'd like something like:
echo "There's an error... Restart" + Date + Time >> error.txt
How Can I do that?

Thanks in advance!

PeejAvery
November 20th, 2006, 01:13 PM
You can echo both the date and the time in DOS.

echo %date%
echo %time%

AlbertGM
November 21st, 2006, 04:19 AM
Right, but how can I save them into a Variable? For example to create the name of a file:echo "Wathever" >> File_<Date>_<Time>.log

Thanks.

PeejAvery
November 21st, 2006, 11:48 AM
Remember that in DOS, you cannot have spaces in the filenames.

Format: File_MM-DD-YYYY_HH-MM-SS.txt
echo "Whatever" >> File_%date:~4,2%-%date:~7,2%-%date:~-4%_%time:~0,2%-%time:~3,2%-%time:~6,2%.txt

AlbertGM
November 21st, 2006, 12:24 PM
echo "Whatever" >> File_%date:~4,2%-%date:~7,2%-%date:~-4%_%time:~0,2%-%time:~3,2%-%time:~6,2%.txt
Cool!!! I'll try. Thanks a lot.

Albert.

PeejAvery
November 21st, 2006, 01:43 PM
You're welcome. Good luck. :thumb: