Link to home
Start Free TrialLog in
Avatar of DebbieFost
DebbieFostFlag for United States of America

asked on

Need a Script to copy and rename files based on an input file

I have 20,000 documents scattered across multiple folders that I need to have copied to a new location and renamed with a file extension. I would like to have a script that can read an Input file and perform the function accordingly.

Example of original files:
F:\000100\203010\G3.FB
F:\090320\134010\X7.6P
F:\012050\761010\5V.J1

Example of comma delimited input file contents (called C:\Input.csv)
PDF,F:\000100\203010\G3.FB,Invoice from ABC
DOCX,F:\090320\134010\X7.6P, Letter to Susie
HTM,F:\012050\761010\5V.J1,Bank Rec Report

I would like to have a script that will read the input file and copy & rename the files to the G" drive in the following format:

G:\000100\203010\G3.FB.PDF
G:\090320\134010\X7.6P.DOCX
G:\012050\761010\5V.J1.HTM

We are essentially adding the appropriate file extension to each file (as referenced in the Input.csv file), and copying the document to the G: Drive
Avatar of Bill Prew
Bill Prew

Here's a small BAT script that should do the job.

@echo off
set DestDrive=G:
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  copy "%%~B" "%DestDrive%%%~pnxB.%%~A
)

Open in new window

~bp
@Bill: Forgot last " on line 5

Cheers,
Rene
So it should be this then:
@echo off
set DestDrive=G:
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  copy "%%~B" "%DestDrive%%%~pnxB.%%~A"
)

Open in new window

~bp
@Debbie: I don't expect points for my mini-tiny-wanabe-contribution, as Bill did all the work!

Cheers,
Rene
Only thing I'd say is... if the directory structure does not exist on the dest drive already then copy won't create it will it Bill?

It may be worth adding between the for and copy commands:

md "%destdrive%%%~pB"

(I think that's right!)

Steve
Good point Steve, this would address that possibility.

@echo off
set DestDrive=G:
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  if not exist "%DestDrive%%%~pB" md "%DestDrive%%%~pB"
  copy "%%~B" "%DestDrive%%%~pnxB.%%~A"
)

Open in new window

~bp
Avatar of DebbieFost

ASKER

Thanks bill (and Rene and Steve) - will give it a whirl today!
Ok....thinsk just got a lot simpler.....we no longer need to copy the documents to a new location, we just want to rename them in place!

Can you adjust this script so it just adds the extension to the filename in it's current location? (i.e. F:\,<folder>\,<filename>.<ext>
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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