XCOPY switch to create specified directory if it doesn't exist?

182.5k Views Asked by At

I am using XCOPY in a post-build event to copy compiled DLLs from their output folders to the main app's output folder. The DLLs are being copied to a "Modules" subfolder in the main app output folder, like this:

xcopy  "$(TargetPath)" "$(SolutionDir)Prism4Demo.Shell\$(OutDir)Modules\" 

The command works fine if the Modules folder exists, but I have discovered during testing that if the folder doesn't exist, XCOPY doesn't create it, and the command fails.

Is there an XCOPY switch that will cause the folder to be created if it doesn't exist? If not, what would I add to my post-build event to create the folder if it doesn't exist? Thanks for your help.

10

There are 10 best solutions below

3
On BEST ANSWER

I tried this on the command line using

D:\>xcopy myfile.dat xcopytest\test\

and the target directory was properly created.

If not you can create the target dir using the mkdir command with cmd's command extensions enabled like

cmd /x /c mkdir "$(SolutionDir)Prism4Demo.Shell\$(OutDir)Modules\"

('/x' enables command extensions in case they're not enabled by default on your system, I'm not that familiar with cmd)

use

cmd /? 
mkdir /?
xcopy /?

for further information :)

1
On

Try /E

To get a full list of options: xcopy /?

5
On

I hate the PostBuild step, it allows for too much stuff to happen outside of the build tool's purview. I believe that its better to let MSBuild manage the copy process, and do the updating. You can edit the .csproj file like this:

  <Target Name="AfterBuild" Inputs="$(TargetPath)\**">
    <Copy SourceFiles="$(TargetPath)\**" DestinationFiles="$(SolutionDir)Prism4Demo.Shell\$(OutDir)Modules\**" OverwriteReadOnlyFiles="true"></Copy>
  </Target>
4
On

You could use robocopy:

robocopy "$(TargetPath)" "$(SolutionDir)Prism4Demo.Shell\$(OutDir)Modules" /E
0
On

To use xcopy command to create folders during the copying process, you can include the /I switch. This switch tells xcopy to assume that the destination is a directory if multiple files are being copied or if the destination does not exist. If the destination does not exist, it will be created as a directory.

Here's the basic syntax for using xcopy with the /I switch:

xcopy <source> <destination> /I
xcopy Documents D:\Backup /I
4
On

Answer to use "/I" is working but with little trick - in target you must end with character \ to tell xcopy that target is directory and not file!

Example:

xcopy "$(TargetDir)$(TargetName).dll" "$(SolutionDir)_DropFolder" /F /R /Y /I

does not work and return code 2, but this one:

xcopy "$(TargetDir)$(TargetName).dll" "$(SolutionDir)_DropFolder\" /F /R /Y /I

Command line arguments used in my sample:

/F - Displays full source & target file names

/R - This will overwrite read-only files

/Y - Suppresses prompting to overwrite an existing file(s)

/I - Assumes that destination is directory (but must ends with \)

0
On

I tried this on the command.it is working for me.

if "$(OutDir)"=="bin\Debug\"  goto Visual
:TFSBuild
goto exit
:Visual
xcopy /y "$(TargetPath)$(TargetName).dll" "$(ProjectDir)..\Demo"
xcopy /y "$(TargetDir)$(TargetName).pdb" "$(ProjectDir)..\Demo"
goto exit
:exit
0
On

Simple short answer is this:

xcopy /Y /I "$(SolutionDir)<my-src-path>" "$(SolutionDir)<my-dst-path>\"
0
On

Simply type in quotes slash delimiter "/" and add to final destination 2 back-slashes "\\"

It's will be create New folders to copy and copy need file(-s).

xcopy ".\myfile" "....folder1/folder2/destination\\"
0
On

Use the /i with xcopy and if the directory doesn't exist it will create the directory for you.