3

I have a list of files extracted from a table in a SQL database. The file can be either a delimited text file or an excel spreadsheet, whichever works best.

The file looks like this

sourcePath1\fileName1, destPath1\fileName1
sourcePath2\fileName2, destPath2\fileName2
sourcePath3\fileName3, destPath3\fileName3

etc

One column contains the original path and filename of the files, the second column contains the desired path and filename of the files. This has been obtained using a standard REPLACE() function in the original SELECT statement.

Now I have the list(s) I actually need to copy the source file to the destination.

Is this achievable using the command line (batch or Powershell?) or via scripting or a GUI?

3 Answers 3

2

Here is a native Windows batch solution to parsing a comma separated value (csv) text file.

@echo off
for /f "tokens=1,2 delims=," %%A in (file.csv) do (
    copy %%A %%B
)
2
  • This looks to be along the lines of what I need however, if the folder structure does not already exist, I receive a "The system cannot find the path specified" error. Is it possible to create the folder structure on the go?
    – Danny
    Feb 26, 2013 at 10:46
  • 1
    To create a folder structure, we would have to either parse the path and use md before the copy command or as you did in your answer, use xcopy. Feb 26, 2013 at 15:05
2

Based upon David Ruhmann's answer, the following works. It uses xcopy and passes an F to the prompt that asks if the file is a file or folder.

@echo off
for /f "tokens=1,2 delims=," %%A in (book1.csv) do (
    cmd /c echo F | xcopy "%%~A" "%%~B"
)

EDIT: Added tilde (~) as per @DavidRuhmann suggestion.

3
  • Glad you were able to get it working. :) Just some batch notes. 1. The cmd /c is not necessary. 2. When adding quotations to paths, add the tilde to remove any existing quotes (if any) "%%~A" . This is just an error proofing step in case quotations are already included around the paths. Feb 26, 2013 at 15:03
  • Be sure to mark your answer if it solves your question. Feb 26, 2013 at 15:04
  • @DavidRuhmann Thanks for following up with the additional points and for your answer.
    – Danny
    Feb 27, 2013 at 9:28
0

This is fairly easy to do with a Unix shell. Here's how you might do it with my Hamilton C shell. (The free version will do this.) You could do something very similar with Cygwin bash.

The really easy way to do it would be to transform the list using sed into a script where each line a cp operation. Then just run the script.

sed "s/^^/cp '/;s/, /' '/;s/^$/'/" < list.csv > copyscript.csh
copyscript

Here's what that copyscript.csh file would look like:

Using sed to convert a CSV list into a list of cp commands

3
  • Thanks for posting this. Where would I specify the input file containing the list of files?
    – Danny
    Feb 25, 2013 at 15:20
  • In my examples, I've read the list from the lines following using the << endExample ... endExample notation. You could read the list from a file using < listoffilepairs. If you have trouble making this work, please call and I'll walk you through it. (Yes, I support even my free version.) Feb 25, 2013 at 15:31
  • @Danny I've simplified my answer to just doing it the easy way and to read from a file containing the list. I hope that's helpful. Feb 25, 2013 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .