0

I wrote script to remove temporary files. Script creates log with all files removed:

SET RemoveLog=RemoveLog_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%__%time:~0,2%_%time:~3,2%.txt (that will create file like RemoveLog_2014_03_09__09_12.txt)

and to save something in this log I use:

ECHO. >> %MEMLET%\%RemoveLog% (Empty line in this case)

This have been working fine so far but on one laptop something else is happening.

File is saved as RemoveLog_2014_03_09__ (09_12.txt is missing)

And everywhere I used >> %MEMLET%\%RemoveLog% in the script, In the log at the end of every line i see 9_12.txt

Why is this happening?

Solver
  • 169
  • 7
  • 23

1 Answers1

1

You don't get what you expected because %DATE% returns the current date using the windows settings for the "short date format". This setting is fully (endlessly) customizable.

One user may configure its system to show the short date as Wed030914; while another user (even in the same system) may choose 09/03/2014. It's a complete nightmare for a BAT programmer.

One possible solution is to use WMIC, instead. WMIC is the WMI command line interface to WMI. WMI Windows Management Instrumentation is the http://en.wikipedia.org/wiki/Windows_Management_Instrumentation

WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table

returns the date in a convenient way to directly parse it with a FOR.

Completing the parse and putting the pieces together

 FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"') DO (
    SET REMOVELOG=RemoveLog_%%F_%%D_%%A_%%B_%%C.txt
 )
PA.
  • 28,486
  • 9
  • 71
  • 95
  • this will not have leading zeros for day, month, hour, minute, second. – Stephan Sep 03 '14 at 09:33
  • it answers your question, avoiding the %date% format problem. You need to build your solution around it. You might also try `WMIC OS get localdateTime` – PA. Sep 03 '14 at 11:23