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...)


This might be useful to you, see this question

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.