Copy files with %variable% names

August 26, 2010 at 18:06:27
Specs: WinXP, P4 2.2Ghz
Hi M2,

Last year you helped me on a FTP script. I do appreciate your help.
Now I have a very simple question.

I have 3 files, sample1.txt,sample2.txt and sample3.txt.
I want to use one command to copy them to another folder and rename them as 280810_sample1.txt,
280810_sample2.txt,280810_sample3.txt.

My program is following.

set fdate=280810
copy *.txt d:\temp\%fdate%_*.txt

I think it is easy, but the result is 280810_.txt.
I think I need to modify %fdate%, but I do not know how.
Pls give me a hint.

Thanks.

Phil

Computer Programmer


See More: Copy files with %variable% names

Report •


#1
August 26, 2010 at 18:52:40
Maybe I can help...

This should work:

@ECHO OFF

FOR /f "tokens=*" %%a IN ('DIR /b "P:\a\t\h\*.txt"') DO (
	RENAME %%a "280810_%%a"
	COPY "280810_%%a.txt" "P:\a\t\h"
)
PAUSE

However, it can be extended to automatically get the date if you would like, let me know.


Report •

#2
August 26, 2010 at 19:52:27
Thanks Matt 123.

I would like to make it in one command line, not a loop.
I know how to get the date into a variable.

copy *.txt d:\temp\280810_*.txt
It will copy all TXT files with date ahead of its original names.
But with the variable inside the command line,
copy *.txt d:\temp\%fdate%_*.txt

It will distill the file names and leave the rest letter as file name.
Sample1.txt ==>280810_.txt.

I think % makes this trouble, but I do not know how to avoid it.

Thanks.

Phil

Computer Programmer


Report •

#3
August 27, 2010 at 12:52:13
I'm not sure how, if even possible, to do it without the loop.

Sorry.


Report •

Related Solutions

#4
August 29, 2010 at 18:01:07
Hi Matt123,

I made mistake on Copy Command.
Even I use 'copy sample1.txt 1*.txt', it is copied to 1ample1.txt.

It makes me clear that copy source destination with '*' will exactly replace the file name. I will take your method to do it. This POST is Closed.

Thanks a lot.

Phil

Computer Programmer


Report •


Ask Question