0

I need to copy a few files and folders to their respective destinations using a Windows batch script.

All the files and folders I am supposed to copy, are kept within a folder, SOURCE.

Example:

  • folder: C:\X\Y\Z\SOURCE\A
  • file : C:\X\Y\Z\SOURCE\A.txt
  • file : C:\X\Y\Z\SOURCE\B.txt
  • folder: C:\X\Y\Z\SOURCE\ZZZ

The destination paths of all the above are provided as text file contents, destination.txt.

Content of destination.txt:

C:\FinalDestination\D\A\...
C:\FinalDestination\N\A.txt
C:\FinalDestination\C\B.txt
C:\FinalDestination\U\ZZZ\...

Where three dots at the end signifies a directory, otherwise it's a file.

What I need to do in the above scenario is:

  • Copy folder A from SOURCE to C:\FinalDestination\D\
  • Copy file A.txt from SOURCE to C:\FinalDestination\N\
  • Copy file B.txt from SOURCE to C:\FinalDestination\C\
  • Copy folder ZZZ from SOURCE to C:\FinalDestination\U\

I don't know how to do it as I am pretty new to Windows command line.

I know XCopy is a command which can work for me, xcopy source destination, but I don't know how to extract the source and destination details.

Compo
  • 36,585
  • 5
  • 27
  • 39
Jatin
  • 1,857
  • 4
  • 24
  • 37
  • 1
    Do you have any control over the design/layout of `destination.txt`? In my opinion using a sequence of two back slashes followed by three dots to signify a directory is absolutely crazy. – Compo Nov 01 '17 at 09:46
  • @compo: No, I don't have any control over it. – Jatin Nov 01 '17 at 10:02
  • @compo: There are NO two back slashes followed by 3 dots. – Jatin Nov 01 '17 at 10:04
  • @compo: There is only 1 back slash followed by 3 dots to represent a directory – Jatin Nov 01 '17 at 10:04
  • I have corrected that in your question, _it appears that it was necessary for you in your original to use a back slash as an escape character for another_. – Compo Nov 01 '17 at 10:10
  • @compo: Thank You for restructuring my question properly. – Jatin Nov 01 '17 at 10:12
  • Does anyone get anything which can help me? Please help me :( – Jatin Nov 01 '17 at 10:17
  • @compo: Just wondering, if you have get any idea how to accomplish it ? – Jatin Nov 01 '17 at 10:41
  • I thought you didn't have control over `destination.txt`. (Just saw your new question, where you appear to be using three dots only to identify directories, no back slash). – Compo Nov 03 '17 at 11:09
  • @Compo -- Thanks for noticing. I missed back-slash there. Updated my new question. – Jatin Nov 03 '17 at 11:24
  • @Compo -- R u there? Can u please suggest be a resolution for this question - https://stackoverflow.com/questions/47094912/how-to-redirect-output-of-a-batch-command-to-a-variables-value – Jatin Nov 03 '17 at 11:53

1 Answers1

1

Using an unchanged destination.txt and your supplied data, the following may help:

@Echo Off

Set "sD=C:\X\Y\Z\SOURCE"
Set "sF=destination.txt"

For /F "UseBackQ Delims=" %%A In ("%sF%"
) Do For %%B In ("%%~fA.") Do echo=XCopy/IY "%sD%\%%~nxB" "%%~dpA." 2>Nul
Pause

You need only modify the content of the variables at lines 3 and 4

Note:

I have currently made it so that nothing is copied, just the commands output to your screen. If you are happy with the output remove echo= from line 7 and delete the content of line, 8

Compo
  • 36,585
  • 5
  • 27
  • 39
  • It's working fine for files. But it's not for folders – Jatin Nov 01 '17 at 11:13
  • What is the first line of output before you remove `echo=` – Compo Nov 01 '17 at 11:36
  • This is what I have got (without removing anything from your script): Copy "C:\X\Y\Z\SOURCE\A" "C:\FinalDestination\D\" Copy "C:\X\Y\Z\SOURCE\A.txt" "C:\FinalDestination\N\" Copy "C:\X\Y\Z\SOURCE\B.txt" "C:\FinalDestination\C\" Copy "C:\X\Y\Z\SOURCE\ZZZ" "C:\FinalDestination\U\" Press any key to continue . . . – Jatin Nov 01 '17 at 11:47
  • Copy "C:\X\Y\Z\SOURCE\A" "C:\FinalDestination\D\" -- This command is not working on comand prompt. – Jatin Nov 01 '17 at 11:53
  • This is what shows when I run copy command on cmd: > Copy "C:\X\Y\Z\SOURCE\A" "C:\FinalDestination\D\" C:\X\Y\Z\SOURCE\A\* The system cannot find the file specified. 0 file(s) copied. – Jatin Nov 01 '17 at 11:55
  • Good, I have now updated the script and used your suggested `XCopy` command, please retry. – Compo Nov 01 '17 at 12:36
  • Thanks for the update. It is working fine with files. But, unfortunately, it's still not working for copying folders. – Jatin Nov 02 '17 at 07:50
  • XCopy/IY "C:\X\Y\Z\SOURCE\A" "C:\FinalDestination\D\A\." ---- This command will show you error message - 0 File(s) copied – Jatin Nov 02 '17 at 07:51
  • I have since posting tested this script on a Windows 7 machine. Both files and directories were copied as expected! – Compo Nov 02 '17 at 11:16