The Computer Oracle

Windows command-line: create a file with the current date in its name

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 Windows Command-Line: Create A File With The Current Date In Its Name
00:57 Answer 1 Score 3
01:12 Accepted Answer Score 35
01:22 Answer 3 Score 3
02:26 Answer 4 Score 7
02:55 Thank you

--

Full question
https://superuser.com/questions/47885/wi...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #bash #environmentvariables

#avk47



ACCEPTED ANSWER

Score 35


You can replace symbols in variables by using :

set _date=%DATE:/=-%



ANSWER 2

Score 7


I have posted an answer to a very similar question here. The spltting of the date in fields is done in 1 line, instead of several lines in harrymc's answer.

The interesting part is:

@echo off
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%j%%i
echo Date: %yyyymmdd%

And the warning is still relevant:

Warning: the format of the date (yyyymmdd=%%k%%j%%i) depends on your regional settings. Because I use the French date format (dd/mm/yyyy), I have to use "%%k%%j%%i" as the format (%%i = day, %%j = month, %%j = year).

If your regional settings are set to US style (mm/dd/yyyy), you should use "%%k%%i%%j" (%%i = month, %%j = day, %%j = year).




ANSWER 3

Score 3


This rather horrid bit of code should do something like what you want

move "Filename.txt" "Filename%date:~6,4%%date:~3,2%%date:~0,2%_%Time:~0,2%%Time:~3,2%%Time:~6,2%.txt"

If you change the filename appropriately.




ANSWER 4

Score 3


Since your question is tagged as Windows, I found this solution that works in the good old .bat files :
"Windows Batch File (.bat) to get current date in MMDDYYYY format",
and can probably be adapted to your case:

echo on
@REM Seamonkey’s quick date batch (MMDDYYYY format)
@REM Setups %date variable
@REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY

FOR /F “TOKENS=1* DELIMS= ” %%A IN (’DATE/T’) DO SET CDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (’DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (’echo %CDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (’echo %CDATE%’) DO SET yyyy=%%Bv vSET date=%mm%%dd%%yyyy%

this does nothing but setup the %date variable to be todays date in MMDDYYYY format so it can be called later in the script.

Edit
Found a better syntax. Given that my date is today "Mon 28/09/2009":
set day=%Date:~0,2%
set month=%Date:~7,2%
set year=%Date:~10,4%