copy files in batch

July 19, 2010 at 14:01:48
Specs: Windows Server 2003, 1 3.06 Ghz / 2 GB
I want to copy the .rpt files within the 'Documents' directory that are 1 directory-level down
(without going into deeper sub-directories that may or may not exist).
I want to save the copy to a new directory while concatinating the sub-directory id into the .rpt file name.



I have a partial statement that simply copies the files into the source- but need to
add the concatenation of the sub-directory id.

for /d %%a in ("D:\Documents\*") do @copy "%%~Fa\*.rpt" "D:\temp\PublishedReports\" 2>NUL

I've been searching and attempting for the better part of the day- just can't seem to get it.

Thanks for any assistance.

See More: copy files in batch

Report •

July 19, 2010 at 15:47:55
Interesting but wouldn't this be simpler..

for /d %%a in ('dir D:\Documents\*.rpt /s') do @copy "%%a" "D:\temp\PublishedReports\" 2>NUL

Report •

July 19, 2010 at 16:09:33
Your script generates "cannot find file specified" 2 times.
It also seems (could be lack of experience talking here) that your script would create a like sub-directory for each of my files.
This would be less than ideal as I would like all the files to live in a common directory for easier reference.
If I can append only the parent directory 'id' to the file name, then this would be all the reference I need in terms of knowing where the file originated from.
Kind Regards-

Report •

July 21, 2010 at 06:14:33
Try the following:

@echo off

setlocal EnableDelayedExpansion

md "D:\Temp\PublishedReports" 2>nul

for /d %%a in ("D:\Documents\*") do (
  for /f "tokens=1 delims=_" %%b in ("%%~na") do set ID=%%b
  for %%c in ("%%a\*.rpt") do (
    set newfile=!ID!_%%~nxc
    copy "%%c" "D:\Temp\PublishedReports\!newfile!"

Report •

Related Solutions

Ask Question