Jump to content

Insert date in to filename in Batch


Recommended Posts

Hi,

Going to try and make a long story short here. I'm importing a bunch of Reg files and would like to have a log of what installed and what didn't. I'm having a problem with coming up with a way that creates said log.

Here is what I have:

FOR /F "tokens=1,2,3 delims=/" %M IN ('date /t') DO set LOG=RegSettings-BSPResults(%N-%M-%O).log && echo RegSettings Results: > "%LOGPATH%\%LOG%" && echo. >> "%LOGPATH%\%LOG%"

The above works fine on the command line. It creates a Log file with the appropriate date stamp in the file name at the specified location. When I try the same line of code (with the appropriate meassures taken in regards to paramaeters, It throws an error saying it cannot find the path specified. No files are created and at the end of the file name it seems to insert a space at the end of the name but before the extension. Here's what the code looks like in a batch file:

FOR /F "tokens=1,2,3 delims=/" %%M IN ('date /t') DO set LOG=RegSettings-BSPResults(%%N-%%M-%%O).log && echo RegSettings Results: > "%LOGPATH%\%LOG%" && echo. >> "%LOGPATH%\%LOG%"

Here is what I get:

The system cannot find the path specified.
LOG=RegSettings-BSPResults(11-05-2010 ).log
D:\unattended\Logs\RegSettings-BSPResults(11-05-2010 ).log
Press any key to continue . . .

Can anyone help me find out what's going on? I've tried everything I can think of but I guess this is way beyond me. Probably something silly I've overlooked.

Thanks.

Link to comment
Share on other sites


Can anyone help me find out what's going on? I've tried everything I can think of but I guess this is way beyond me. Probably something silly I've overlooked.

Maybe variable expansion in the FOR loop? :unsure:

http://www.robvanderwoude.com/variableexpansion.php

Try:

FOR /F "tokens=1,2,3 delims=/" %%M IN ('date /t') DO (
set LOG=RegSettings-BSPResults(%%N-%%M-%%O).log
echo RegSettings Results: > "%LOGPATH%\%LOG%"
echo. >> "%LOGPATH%\%LOG%"
echo The logfile is "%LOGPATH%\%LOG%"&Pause
)

And:

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2,3 delims=/" %%M IN ('date /t') DO (
set LOG=RegSettings-BSPResults(%%N-%%M-%%O).log
echo RegSettings Results: > "!LOGPATH!\!LOG!"
echo. >> "!LOGPATH!\!LOG!"
echo The logfile is "!LOGPATH!\!LOG!"&Pause
)

WHERE is LOGPATH defined?

jaclaz

Link to comment
Share on other sites

Ok first off, Sorry for the late reply. I could of swore I replied to Jaclaz this afternoon.

After looking at the link left by Jaclaz - I finally got it to work. As fas as I understand the variable I was trying to set in the FOR command was expanding too early.

I fixed the script by adding SETLOCAL EnableDelayedExpansion at the start of the script and then changing the FOR statement to this:


REM Creating script log

:CREATELOG
For /F "tokens=1,2,3 delims=/" %%M IN ('echo %DATE%') DO set LOG=RegSettings-Results(%%M-%%N-%%O).log && echo RegSettings-Results: > "%LOGPATH%\!LOG!" && echo. >> "%LOGPATH%\!LOG!"

It now works fine. No errors at all and the Log file is created in the path I specified. Thanks so much to both of you. :D

Link to comment
Share on other sites

Now that you've decided to change the order of the tokens in the date output, you don't need a 'for' statement and you certainly don't need to start enabling 'delayed expansion'.

>RegSettings-Results(%DATE:/=-%).log (ECHO=RegSettings-Results:&ECHO=)

Link to comment
Share on other sites

Now that you've decided to change the order of the tokens in the date output, you don't need a 'for' statement and you certainly don't need to start enabling 'delayed expansion'.

>RegSettings-Results(%DATE:/=-%).log (ECHO=RegSettings-Results:&ECHO=)

Which translated means:

2K/XP and later have a built-in dynamic environment variable "Date", as well as "Time" and a few other ones:

http://www.robvanderwoude.com/datetimentbasics.php

http://www.robvanderwoude.com/ntset.php

What worked in NT may still work on later OS, but there are added features that are more handy.

And, in case you asked for it (which you did ;)), you are not really the first one needing to put a date in a filename :whistle: :

http://www.robvanderwoude.com/faq.php#DateDir

jaclaz

Link to comment
Share on other sites

Now that you've decided to change the order of the tokens in the date output, you don't need a 'for' statement and you certainly don't need to start enabling 'delayed expansion'.

>RegSettings-Results(%DATE:/=-%).log (ECHO=RegSettings-Results:&ECHO=)

Yes, You see I initially thought the system I was working on had a MM/DD/YY date format. Turns out Canadians use the same date format as the English. Your code is very interesting. I do know about those special variables (They were my first choice) But I could not get my first approach to work. It looked like this:


echo Regsettings-Results: > RegSettings-Results(%DATE).log

That always threw a The system cannot find the path specified error. I just modified the above using your example and it works. :thumbup


echo RegSettings-Results: > RegSettings-Results(%DATE:/=-%).log

Can you tell me exactly what the :/=- part is - Does? The links left by Jaclaz seem to indicate that they are assignment operators but I don't understand why I need them in the %DATE% variable.

At any rate this has been a very informative thread for me, Thank you both for taking the time. Have a good weekend.

Link to comment
Share on other sites

It is variable expansion and character substitution. We need to do this because you cannot have a file name containing the date seperator you've indicated is being used.

In basic terms the %DATE% variable is expanded and then the /, (forward slash), is substituted for a -, (hyphen.

See some examples here.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...