Link to home
Start Free TrialLog in
Avatar of j_thompson
j_thompson

asked on

Copy multiple files from a folder structure using a batch file

I have a set of ~100 folders that are all named sequentially, ie CPL-XXXX. Within each folder is a standard set of folders, and I would like to make a copy of the contents of some of these folders to another location so that I have a compiled set.

For example
root\CPL-XXXX\1 Client Drawings   <-- Copy *.PDFs in this folder to root\CPL-MASTER\1 Client Drawings

root\CP1-XXXX\2 CSV-DXFs <-- Copy *.CSVs in this folder to root\CPL-MASTER\2 CSV-DXFs

Would it be reasonable to run this with a batch file or powershell?

If you need any other details please let me know!
Avatar of NVIT
NVIT
Flag of United States of America image

Here's a .bat method using robocopy.

Make a .bat of this code. Revise the path on the FOR line as needed.

Note: Code is in debug mode for visual confirmation. When you're satisfied how the copy command will work, remove the ECHO prefix

@echo off

for /d %%A in (c:\root\CPL-*.) do (
  for /f "tokens=1-2 delims=$" %%a in (CopyWhat.txt) do (
    echo robocopy "%%~fA\%%a" "%%~dpACPL-MASTER\%%a" "%%b" /np /ndl /nfl /r:0 /w:0 /log+:c:\root\CopyLog.txt
  )
)

Open in new window

Make file CopyWhat.txt. Each line has the directory to copy, separated by an $ char, then the file pattern to copy:
dir name1$*.pdf
dir name2$*.csv
dir name3$*.csv *.pdf

Open in new window


Open a CMD window and run the .bat file.
Avatar of j_thompson
j_thompson

ASKER

Thanks for the quick reply!

I've tried to get this running, but I must be missing something. Below is the echo I'm getting, but the CopyLog.txt remains empty and no files are copied.

C:\Users\JThompson\Desktop\root>robocpy.bat
robocopy "C:\Users\JThompson\Desktop\root\CPL-0001\1 Client Drawings" "C:\Users\JThompson\Desktop\root\CPL-MASTER\1 Client Drawings" "*.pdf" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-0001\2 CSV-DXFs" "C:\Users\JThompson\Desktop\root\CPL-MASTER\2 CSV-DXFs" "*.csv" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-0002\1 Client Drawings" "C:\Users\JThompson\Desktop\root\CPL-MASTER\1 Client Drawings" "*.pdf" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-0002\2 CSV-DXFs" "C:\Users\JThompson\Desktop\root\CPL-MASTER\2 CSV-DXFs" "*.csv" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-0003\1 Client Drawings" "C:\Users\JThompson\Desktop\root\CPL-MASTER\1 Client Drawings" "*.pdf" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-0003\2 CSV-DXFs" "C:\Users\JThompson\Desktop\root\CPL-MASTER\2 CSV-DXFs" "*.csv" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-MASTER\1 Client Drawings" "C:\Users\JThompson\Desktop\root\CPL-MASTER\1 Client Drawings" "*.pdf" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
robocopy "C:\Users\JThompson\Desktop\root\CPL-MASTER\2 CSV-DXFs" "C:\Users\JThompson\Desktop\root\CPL-MASTER\2 CSV-DXFs" "*.csv" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt

Open in new window


This is what I've edited in the batch file
@echo off

for /d %%A in (C:\Users\JThompson\Desktop\root\CPL-*.) do (
  for /f "tokens=1-2 delims=$" %%a in (CopyWhat.txt) do (
    echo robocopy "%%~fA\%%a" "%%~dpACPL-MASTER\%%a" "%%b" /np /ndl /nfl /r:0 /w:0 /log+:C:\Users\JThompson\Desktop\root\CopyLog.txt
  )
)

Open in new window


And this is what I have for the CopyWhat.txt
1 Client Drawings$*.pdf
2 CSV-DXFs$*.csv

Open in new window


Do you see what I might be missing? Thanks again for the help!
The purpose of the ECHO next to RoboCopy it's for visual confirmation, as I wrote in the original post.

So, if what you see is correct, remove the ECHO and run it again.

Before doing that, I'd verify by copying and pasting the source folders to see if they open correctly.
yes, you're absolutely right- I missed the echo! I have it running on a local, however I would like to run this on a network drive. To do this I've tried adding a line to set the drive, but it seems to crash my command prompt. Any ideas?
pushd "X:\2341\20079-00\7.0 Survey_Mapping\_MASTER"

@echo off

for /d %%A in ("X:\2341\20079-00\7.0 Survey_Mapping\CPL-*.") do (
  for /f "tokens=1-2 delims=$" %%a in (CopyWhat.txt) do (
    robocopy "%%~fA\%%a" "%%~dpA_MASTER\%%a" "%%b" /np /ndl /nfl /r:0 /w:0 /log+:"X:\2341\20079-00\7.0 Survey_Mapping\_MASTER\CopyLog.txt"
  )
)

popd

Open in new window


Thanks again!
Change line 6 to...

  for /f "tokens=1-2 delims=$" %%a in ('type "c:\folder name\CopyWhat.txt"') do (

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much! That did it!