91

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.

1
  • See my comment to the accepted answer Mar 16, 2011 at 17:56

10 Answers 10

132

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 \)

4
  • 4
    The answer should be more useful if it was elaborated why use /F, why /R, why /Y. In my case /Y solved the problem and future readers would benefit from knowing why without data mining the Internet. Jun 18, 2013 at 19:36
  • What if I need to crate two directories? Can xcopy do that for me?
    – Pedro77
    Oct 4, 2013 at 11:54
  • 3
    In my case, /I wasn't required in order to conditionally create the directory, but +1 for ending the path with backslash. Nov 30, 2016 at 1:34
  • TIL that researching command line options is "data mining the Internet". :)
    – Tony
    May 11, 2021 at 5:05
60

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 :)

3
  • 13
    I accepted this answer because the first part turned out to be correct. XCOPY will create the directory, if it doesn't already exist. I discovered that Windows was trying to ask for overwrite confirmation on my XCOPY command. VS doesn't allow that, so it exited with Code 2. I added the /y param, which supresses the prompt, and the command works. Tested by cleaning the solution and deleting the folder, then rebuilding. Folder was created by XCOPY. Mar 3, 2011 at 16:41
  • @DavidVeeneman This does not work in VS2015 under Windows 10; xcopy errors with the message "error : Invalid path". So no. xcopy will not (always) create the destination directory
    – Tom Lint
    Oct 10, 2016 at 12:15
  • 4
    XCOPY does create the directory, but it will only do so if there is no ambiguity: This is ambiguous: D:\>xcopy myfile.dat xcopytest\test This is not: D:\>xcopy myfile.dat xcopytest\test\ (note the last '\')
    – azog
    Dec 1, 2017 at 10:19
15

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>
5
  • 1
    I think your Inputs, SourceFiles and DestinationFiles are not right. He's copying the DDL to the modules folder, not the other way round. Mar 3, 2011 at 2:23
  • I'd add them to a property group anyway. Mar 3, 2011 at 2:30
  • 1
    Inputs="..." should be removed. In VS 2012 I get an error that "The "AfterBuild" target is missing its output specification. If a target declares inputs, it must also declare outputs."
    – angularsen
    Nov 20, 2013 at 9:16
  • $(TargetPath) is the filepath to the output .DLL file, so should probably also remove \** from the SourceFiles value.
    – angularsen
    Nov 20, 2013 at 9:17
  • If you remove Inputs="" you lose build tracking. This is a two year old question, if you want to update it, feel free. Nov 20, 2013 at 12:48
15

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

6

You could use robocopy:

robocopy "$(TargetPath)" "$(SolutionDir)Prism4Demo.Shell\$(OutDir)Modules" /E
4
  • +1: I didn't know about the robocopy command available in Vista and Win7. Thanks. Don't think it will work here, though. I'm copying a file, rather than a folder. Mar 3, 2011 at 15:47
  • I didn't know about robocopy either. Good to know, but unfortunately, it fails with "Access is denied" message if copying to a folder under Program Files. (xcopy doesn't have a problem with this.)
    – Tom Bogle
    Oct 7, 2016 at 18:36
  • Upon further study, it looks like xcopy may be happy to copy new files into existing folders under Program Files, but it apparently can't create new folders either (without admin access).
    – Tom Bogle
    Oct 7, 2016 at 18:46
  • This is how to use Robocopy in a batch file to copy a file only (requires SET file=%1 and SET filepath=%~dp1 and SET filename=%~n1 and SET destination=c:\path\folder ) : ROBOCOPY "%filepath% " "%destination%" "%filename%" /B /COPY:DT /XJ /SL /R:0 /W:0 /V (the whitespace in "%filepath% " is essential, not a mistake).
    – Ed999
    May 2, 2021 at 22:59
2

Simple short answer is this:

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

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\\"
1

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

Try /E

To get a full list of options: xcopy /?

1
  • Thanks, but /e is for copying an existing subdirectory structure in the source. I'm copying a file at the source root to a new subdirectory in the destination. Mar 3, 2011 at 15:54
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.