1

I'm making a batch file that makes a lot of other files and I'd like to name them by date and time e.g

YYYY-MM-DD HH-MM-SS-MS

Because filenames can't contain "/", ":" or "." in Windows, I need to change how the date and time display and also change the order of display so it shows the files from newest to oldest when ordering by name.

marloso2
  • 117
  • 1
  • 1
  • 9
  • 3
    Don't use `%date%`, it's locale dependent. Use [`wmic`](http://stackoverflow.com/q/15378719/995714) instead. Split the variable and use the date values whatever way you want – phuclv Apr 18 '17 at 01:53

1 Answers1

1

You can replace parts of strings:

C:\>echo %date%
Mon 04/17/2017

C:\>echo %date:/=-%
Mon 04-17-2017

The syntax is: %Variable:[old-string]=[new-string]%

To do similar to time:

C:\>set MYTIME=%time::=-%
C:\>set MYTIME=%MYTIME:.=+%
C:\>echo %MYTIME%
21-35-18+60

To put it all together, break the date into pieces:

set YR=%date:~-4%
set DY=%date:~7,-5%
set MO=%DATE:~4,-8%

set MYTIME=%time::=-%
set MYTIME=%MYTIME:.=-%
echo %YR%-%MO%-%DY% %MYTIME%

Output:

2017-04-17 21-43-06-05
abelenky
  • 63,815
  • 23
  • 109
  • 159