XCOPY 开关创建指定的目录,如果它不存在?

我在一个构建后事件中使用 XCOPY 将已编译的 DLL 从它们的输出文件夹复制到主应用程序的输出文件夹。DLL 被复制到主应用程序输出文件夹中的“ Module”子文件夹中,如下所示:

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

如果 Module 文件夹存在,那么该命令可以正常工作,但是我在测试期间发现,如果该文件夹不存在,XCOPY 不会创建它,那么该命令将失败。

是否有一个 XCOPY 开关,如果文件夹不存在,它将导致创建该文件夹?如果没有,那么如果文件夹不存在,我应该在生成后事件中添加什么来创建该文件夹?谢谢你的帮助。

167537 次浏览

Try /E

To get a full list of options: xcopy /?

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

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>

You could use robocopy:

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

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

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

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

Simple short answer is this:

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

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