Copy entire folder THEN rename each file with appended date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JustMyAlias
Posts: 4
Joined: 04 Aug 2010 10:43

Copy entire folder THEN rename each file with appended date

#1 Post by JustMyAlias » 04 Aug 2010 10:57

I need help with this...
and if you could, when replying, add REM with notes to explain each step, I would REALLY appreciate it. I have Googled and Googled without success - and when I do find scripts, I do not understand the majority of what each line is trying to accomplish...

this is what I have:
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set date=%%a%%b%%c)

copy "C:\Documents and Settings\MyName\Desktop\Test Files Folder\"*.* "E:\TEST Files BKUP folder\"/v
ren "E:\TEST Files BKUP folder\"*.* *"_%date%"_".*


my results:
Test File 1.xlsx_08042010_

what I want:
Test File 1_08042010_.xlsx or whatever the appropriate file extension

In all honesty - I do not even understand the set date code (what DELIMS are, %, tokens,...) - but it has been working for me when I only have a single file to backup.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy entire folder THEN rename each file with appended d

#2 Post by aGerman » 04 Aug 2010 13:08

It seems to me that the output of date /t is ddd MM/DD/YYYY in your case. So forget the first for loop, you probably don't need it. The second is interresting.
You process the output of date /t. Delims means, which characters are the data separators for your processing. As you can see it's Slash and Space. So the output is splitted to 4 separate parts: ddd, MM, DD and YYYY. The tokens are the parts (2 until 4 in your case) which will be assigned to the dynamic variables, starting with %%a. Finally you will find MM in %%a, DD in %%b and YYYY in %%c. After "do" set these parts to variable date (but this name is not a good one, because it still exists into the cmd environment before).

Rename the files after copying is inconvenient. You need a for loop to separate the file name and the file extension. You could also use the copy command to rename the files in one step.

Code: Select all

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set datestamp=%%a%%b%%c
pushd "C:\Documents and Settings\MyName\Desktop\Test Files Folder"
for /f "delims=" %%a in ('dir /a-d /b *.*') do (
  copy "%%a" "E:\TEST Files BKUP folder\%%~na_%datestamp%_%%~xa" /v
)
popd

Into the second for loop %%a is the found file, %%~na is only the file name and %%~xa is only the file extension.

Regards
aGerman

JustMyAlias
Posts: 4
Joined: 04 Aug 2010 10:43

Re: Copy entire folder THEN rename each file with appended d

#3 Post by JustMyAlias » 04 Aug 2010 13:35

You are AWESOME!!
Thank you so much - it worked perfectly.
I do still have several questions - but no time right now.
(more of the how it works, so I can hopefully understand better/ learn...)
I will reply again this evening.
Thank you!!

:D

JustMyAlias
Posts: 4
Joined: 04 Aug 2010 10:43

Re: Copy entire folder THEN rename each file with appended d

#4 Post by JustMyAlias » 04 Aug 2010 18:35

aGerman -
I went through each part of the code and identified what everything was doing (from your comments and lots of Google) - so I now pretty much understand it.

You see what happens when I just copy the code and do not understand it - for example my first 2 lines setting the date variable.

Thank you so much - this was extremely helpful to me!

JustMyAlias
Posts: 4
Joined: 04 Aug 2010 10:43

Re: Copy entire folder THEN rename each file with appended d

#5 Post by JustMyAlias » 04 Aug 2010 19:45

okay - that was short-lived... :oops:
i'm back

now, say my folder
C:\Documents and Settings\MyName\Desktop\Test Files Folder
also had subfolders with files in them

would I be able to copy and rename/date append all of the files in the subfolders as well?
tia!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy entire folder THEN rename each file with appended d

#6 Post by aGerman » 05 Aug 2010 07:37

Should work similar. The question is from where to where would you copy the files?

Regards
aGerman

Post Reply