Batch file to copy and rename file with today's date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

Batch file to copy and rename file with today's date

#1 Post by hendrikbez » 02 Oct 2013 04:23

I have about 9 txt files that I want to copy to 9 different folder (This is working)

I then want to rename each file with today's date, but it must not override the files of the previous day (cannot get it to work)

Code: Select all

cd\Users\hbezuidenhout\Documents

xcopy "d:\Users\hbezuidenhout\Documents\Windows (GP) Restore.txt" "D:\Users\hbezuidenhout\Documents\Windows (GP)\Restore" /C /K /Y

ren D:\Users\hbezuidenhout\Documents\Windows (GP)\Restore\Windows (GP) Restore.txt Windows (GP) Restore %DATE%.txt


Getting syntex error

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to copy and rename file with today's date

#2 Post by foxidrive » 02 Oct 2013 04:36

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.


Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause


%date% often contains a slash which is an illegal filename character, but your code also needs double quotes to protect the long filename elements:

ren "D:\Users\hbezuidenhout\Documents\Windows (GP)\Restore\Windows (GP) Restore.txt" "Windows (GP) Restore %DATE%.txt"

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

Re: Batch file to copy and rename file with today's date

#3 Post by hendrikbez » 02 Oct 2013 05:14

Thank you, but I am getting error The system cannot find the path specified

I think is on the first line

Code: Select all

cd D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore
ren "D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore\Windows(GP)Restore.txt" "Windows(GP)Restore %DATE%.txt"


Here are all the code

Code: Select all


@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" rem & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
rem set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%%MM%%YYYY%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%DD%-%MM%-%YYYY%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
rem echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"

cd D:\Users\hbezuidenhout\Documents

xcopy "d:\Users\hbezuidenhout\Documents\Windows(GP)Restore.txt" "D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore" /C /K /Y

cd D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore

ren "D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore\Windows(GP)Restore.txt" "Windows(GP)Restore %DATE%.txt"


hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

Re: Batch file to copy and rename file with today's date

#4 Post by hendrikbez » 02 Oct 2013 06:08

When I run this code

Code: Select all

echo Current Directory = %CD%

it does show me that I am on the correct folder

so I think the problem is in this code

Code: Select all

ren "D:\Users\hbezuidenhout\Documents\WindowsGP\Restore\WindowsGPRestore.txt" "WindowsGPRestore%DATE%.txt"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to copy and rename file with today's date

#5 Post by foxidrive » 02 Oct 2013 06:09

This should be all that is needed: the MD command is only needed if the folder may not exist:

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" rem & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"

MD "D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore" 2>nul
echo copying file
copy "D:\Users\hbezuidenhout\Documents\Windows(GP)Restore.txt" "D:\Users\hbezuidenhout\Documents\Windows(GP)\Restore\Windows(GP)Restore %DD%%MM%%YYYY%.txt"  >nul
echo done
pause

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

Re: Batch file to copy and rename file with today's date

#6 Post by hendrikbez » 02 Oct 2013 06:33

Thank you foxidrive

Post Reply