Page 1 of 1
Batch file to copy and rename file with today's date
Posted: 02 Oct 2013 04:23
by hendrikbez
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
Re: Batch file to copy and rename file with today's date
Posted: 02 Oct 2013 04:36
by foxidrive
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
"
Re: Batch file to copy and rename file with today's date
Posted: 02 Oct 2013 05:14
by hendrikbez
Thank you, but I am getting error
The system cannot find the path specifiedI 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"
Re: Batch file to copy and rename file with today's date
Posted: 02 Oct 2013 06:08
by hendrikbez
When I run this code
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"
Re: Batch file to copy and rename file with today's date
Posted: 02 Oct 2013 06:09
by foxidrive
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
Re: Batch file to copy and rename file with today's date
Posted: 02 Oct 2013 06:33
by hendrikbez
Thank you foxidrive