Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcopy does not create directory structure

I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:

I am using xcopy <src> <dest> /s/y.

<src>=C:\sourcefolder\a\b\c\d\something.java and

<dest>=C:\destinationfolder.

Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .

what I want is C:\destinationfolder\a\b\c\d\something.java and

what I get is C:\destinationfolder\something.java


  1. I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
  2. Tried it without any target in above

There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.

A. Yes I have done xcopy /? to see all options

B. /T also does not create any empty directory structure

C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>

like image 782
Pulak Agrawal Avatar asked Nov 29 '11 05:11

Pulak Agrawal


2 Answers

UPDATE

I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.

Assuming your folder structure is like this:

SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST   = C:\MyDestination

Run XCOPY like this:

XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E

Note the * in something*.java.

like image 61
aphoria Avatar answered Oct 02 '22 13:10

aphoria


The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to

xcopy *.java C:\myfolder /s/y

it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:

xcopy something.jav* C:\myfolder /s/y

Edit

You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:

FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y

What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.

I'm assuming here that:

  • You can get the list of files in a text file, with only the file names (and optinally the paths)
  • You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.

You can also use the following form:

FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y

where cmd needs to be replace with the command you use to generate your list of files to copy.

Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).

like image 25
Laf Avatar answered Oct 02 '22 13:10

Laf