Windows batch,
How to rename a directory to date and time with seconds

Current drive and directory is S:\1 = a backup.

via batch, How to automate, How to rename a directory, not a file?
something like -- Rename S:\1 S:\20171022_131459

2017 year
10 October
22 day
13 hours
14 minutes
59 seconds

Below was does at the command line with one %
on a file named hope.txt, not a directory

ren hope.txt 20171022.txt


for /f "tokens=1-4 delims=- " %d in ("%date%") do rename "hope.txt" %d%e%f.txt

and in part 2 (time)
Seconds are missing in name and
it is not a directory, but, it does a file
named hope.txt with hours and minutes.

rename "hope.txt" 1314.txt 


for /f "tokens=1-2 delims=: " %G in ("%time%") do rename "hope.txt" %G%H.txt


  • 1
    Depending on your Windows version, you may not be able to rename a directory with ren: you may need to use move instead.
    – AFH
    Oct 22, 2017 at 23:18

2 Answers 2


I think this does what you are looking for:

for /f "tokens=2-7 delims=/:. " %a in ("%date% %time%") do ren dir1 %c%ab%b_%d%e%f

The output looks like this:

ren dir1 201710b22_201108

In a batch file you need extra % so this line becomes:

for /f "tokens=2-7 delims=/:. " %%a in ("%date% %time%") do ren dir1 %%c%%ab%%b_%%d%%e%%f

Below is one answer to renaming a directory
From C:\99
To _ C:\YearMMDD_hhMMssFF
(Year Month Day _ hours Minutes seconds Fraction of 1 second)

C:\99 might be the name of a file
C:\99 might be the name of a directory

On Windows XP 32 bit, ren works.
(ren = rename Directory or File)

On this one computer we have 3 delimiters of - : .

Date time format of:
Date ______ time
2017-10-23 19:28:13.01
a___ b__c__d__e__f__g

Answer, cmd line:

for /f "tokens=1-7 delims=-:. " %a in ("%date% %time%") do ren c:\99 %a%b%c_%d%e%f%g  

Directory C:\99
Directory C:\20171023_19473074


Answer, batch file with double %%:

for /f "tokens=1-7 delims=-:. " %%a in ("%date% %time%") do ren c:\99 %%a%%b%%c_%%d%%e%%f%%g  

Directory C:\99
Directory C:\20171023_19480420


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.