Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create a batch file to copy files or folders in a specified directory and append the date and time to their names.
Here's my actual code:

@echo off
Set _bpath=T:\Backup\
if [%1]==[] goto :eof
Set _file=%~n1%
Set _ext=%~x1%
For /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") Do Set "delims=%%a%%b%%c"
For /f "tokens=1-4 delims=%delims%" %%G in ("%time%") Do (
   Set _hh=%%G
   Set _min=%%H
   Set _ss=%%I
   Set _ms=%%J
copy %1 "%_bpath%%_file%(%date:/=-% %_hh%h%_min%m%_ss%s)%_ext%"
if not [%1]==[] goto loop

This one works for files only and I could adapt it easily for folders with xcopy /E %1 "%_bpath%%_file%(%date:/=-% %_hh%h%_min%m%_ss%s) but I would like to avoid dealing with two batch files.

So how to copy/rename them indiscriminately (I have try with copy, xcopy and robocopy without success) or how to distinguish them to create two IF branch? (Using %~x1% is too unreliable...)

share|improve this question
up vote 1 down vote accepted

This might be useful to you, see this question

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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