xcopy does not create directory structure
Asked Answered
E

6

13

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>

Extremity answered 29/11, 2011 at 5:58 Comment(3)
I don't understand where the issue is? If you are copying from C:\myfolder\a\b\c\d and the destination folder is C:\myfolder then why should any directory structure be created when it already exists?United
@BaliC sorry if I was not very clear..the destination folder structure does not exist currently. I have modified the original question to make more clear. And I see what you are saying here; that's what I meant when I wrote 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 behaviorExtremity
Thanks for clarifying, I understand now, see my answer below, I hope that helps.United
K
8

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.

Korwun answered 29/11, 2011 at 12:13 Comment(3)
I am aware of ROBOCOPY. Not allowed to us it. Thanks anyways for the tipExtremity
@PulakAgrawal I updated my answer with a solution that uses XCOPY.Korwun
This still fails if the target folder doesn't exist.Tailback
P
3

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*).

Philately answered 29/11, 2011 at 15:18 Comment(4)
I already mentioned in point C. that I cannot go to the source file location and run the command. But drawing from your point - If I am able to actually run the command from source location C:\sourcefolder\a\b\c\d\ you say it WILL create the destination folder structure making it C:\destinationfolder\a\b\c\d\ . What I am interested in is finding any way to do the same while sitting in the destination C:\destinationfolder or anywhere else on C: EXCEPT in the source folderExtremity
Also look at my conversation with Bali C above, whether this question is worth pursuing at all ?Extremity
You don't need to go in the source location. If you use the wildcard filename (*.java, or something.jav*), and use the /s parameter, xcopy will automatically look for files that match that file name in the current folder, and all subfolders.Philately
Appreciate the response, but can not do that as well. I am getting the list of source files exactly as output from another command (files created after a specific date) and no * business would work :(Extremity
U
2

I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:

xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y

Hope this helps!

United answered 30/11, 2011 at 9:32 Comment(2)
thanks but again I have mentioned in B. above that /T does not create any empty ones for me.. I've basically tried most if not all switches in XCOPY. It has something to do with the way I am calling the command or from where am I calling it.Extremity
I know you mentioned the /T switch but wasn't sure if you had tried the /E, tbh I'm not sure how you can do this, sorry!United
A
1

In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.

You can use:

C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S

Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.

Afterward answered 30/11, 2011 at 21:58 Comment(2)
please read the whole thread, I have mentioned twice- I can not run this command from the source folder.Extremity
XCOPY c:\sourcefolder\something.java* C:\destinationfolder\ /S should work no matter the current folder (with the above mentioned caveat).Afterward
V
1

I have written a very similar batch file using xcopy. Perhaps what I did will help you.

This is the command I used:

xcopy "c:\Data Files\Dave's Data\*.*"   "m:\Dave's Data"      /R/D /E/H

In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP

I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.

The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.

Viminal answered 7/8, 2018 at 16:4 Comment(0)
O
1

It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.

You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.

xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D

If something.java were the only file under C:\MyJavaStuff, you could also use

xcopy /sei c:\MyJavaStuff C:\destinationfolder

That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.

Ofilia answered 7/8, 2018 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.