How to create a batch to copy a file and rena

November 17, 2011 at 09:55:48
Specs: Windows XP
I`m trying to make a command to copy a file to a diffrent drive/folder and rename it by date.

I am using this to copy C:\WINDOWS\system32\xcopy D:\WINPCACE\BCTRANS.DAT C:\test
ren C:\test\BCTRANS.DAT BCTRANS%date%.txt

but i cant get anything to work to make it rename the file to a date.

D:\WINPCACE\BCTRANS.DAt <- is the file i want to copy
C:\test is the folder i want it to copy too.

C:\test\(current date).dat is what i want the file to be..


thanks for the help..


See More: How to create a batch to copy a file and rena

Report •


#1
November 17, 2011 at 16:11:19
The problem with this is that %date% includes characters that can not be included in a file and folder name, specifically /.

The way around it is to set a variable in your script with the date value without the / and use this variable instead of %date% to rename your file.

set strDate=
set strDayDate=
for /f "delims=/ tokens=1,2,3" %%A in ("%date%") do (
	set strDate=%%A%%B%%C
)
for /f "tokens=1,2 delims= " %%A in ("%strDate%") do (
	set strDayDate=%%A%%B
)
echo %strDayDate%

The variable %strDayDate% can now be used within the scope of your script to rename as the current date.

I hope this helps.


Report •

#2
November 18, 2011 at 07:40:22
Yes it does thanks,

Now i have a new problem however. I do not want it to copy its self over and over. Is there away to set it to have the date and the time so.. every time i click the batch it will make a new copy


Report •

#3
November 20, 2011 at 16:35:18
The same applies for the time with the : character. The script below fills the strTime variable with the current time and also strDayDate with the current date, copies the file to new location and renames it to BCTRANS%strTime%%strDayDate%.txt

@echo off
set strDate=
set strDayDate=
set strTime=
for /f "delims=/ tokens=1,2,3" %%A in ("%date%") do (
	set strDate=%%A%%B%%C
)
for /f "tokens=1,2 delims= " %%A in ("%strDate%") do (
	set strDayDate=%%A%%B
)
for /f "delims=: tokens=1,2,3" %%A in ("%time%") do (
	set strTime=%%A%%B%%C
)
copy D:\WINPCACE\BCTRANS.DAT C:\TEST\BCTRANS%strTime%%strDayDate%.txt
PAUSE


Report •

Related Solutions


Ask Question