20

i want to make VS copy the .lib-file it created after the build process to a specific folder. So i went to the project config, post-build event, and entered the following command:

if exist $(TargetPath)
xcopy "$(TargetPath)" "C:\Users\Incubbus\Documents\Visual Studio 2010\My Libraries\z.lib" /Y

But instead of copying the process fails after i click "build" and i receive the following error:

error MSB3073: The command "if exist C:\Users\Incubbus\Documents\Visual Studio 2010\My Libraries\MyNetWorkProject\Debug\IncNetworkLibD.lib xcopy "C:\Users\Incubbus\Documents\Visual Studio 2010\My Libraries\MyNetWorkProject\Debug\IncNetworkLibD.lib" "C:\Users\Incubbus\Documents\Visual Studio 2010\My Libraries\z.lib" /Y

:VCEnd" exited with code 2.

I am also wondering about the :VCEnd in the command-string of the error message <- Maybe this is the reason? How to get this solved?

Any help and hints would be happily consumed :)...

partial solution:

EDIT: it looks like the renaming part (Inc.lib to z.lib) makes trouble, when xcopy asks whether this is a file or a directory...it works when i just copy the originally named file to a directory instead of copying renamed

Juarrow
  • 2,232
  • 5
  • 42
  • 61
  • 1
    Copy/paste the PostBuildEvent from your project file into your question. Don't edit it, make it look exactly the same way. – Hans Passant Dec 23 '12 at 21:48
  • it looks like the renaming part (Inc.lib to z.lib) makes trouble, when xcopy asks wether this is a file or a directory... this works when i just copy the lib-file to a directory without renaming it... – Juarrow Dec 23 '12 at 21:56
  • 1
    http://stackoverflow.com/a/4283533/532647 one possible way to resolve it. By `echo f | xcopy ...` you would just say 'file' to xcopy. – Iarek Dec 24 '12 at 08:19
  • works, thx... if you post it as an answer i can mark the question as answered... – Juarrow Dec 24 '12 at 09:52

3 Answers3

38

Xcopy documentation says the following:

Specifying whether Destination is a file or directory If Destination does not contain an existing directory and does not end with a backslash (\), the following message appears:

Does destination specify a file name 
or directory name on the target 
(F = file, D = directory)? 

Press F if you want the file or files to be copied to a file. Press D if you want the file or files to be copied to a directory.

You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.

You need the opposite, but there is no such switch.

The solution is proposed here: https://stackoverflow.com/a/4283533/532647.

It is suggested to prepend the xcopy command with echo f | prefix, which basically does the following: it simulates a user pressing f key when xcopy asks.

So your command should look like:

if exist $(TargetPath)
echo f | xcopy "$(TargetPath)" "C:\Users\Incubbus\Documents\Visual Studio 2010\My Libraries\z.lib" /Y

Operator | just pipes the output of echo f (== f) into xcopy command and it is read when appropriate. More information about output redirection here: http://ss64.com/nt/syntax-redirection.html.

UPDATE: As Govert points out, this hack won't work under a localized version of Windows. However, another hack will work:

xcopy D:\file.zip c:\renamedFile.zip*

Appending destination file name with an asterisk * makes xcopy not ask whether destination is a file or a directory.

Community
  • 1
  • 1
Iarek
  • 1,220
  • 15
  • 40
  • 14
    This does not work under localised versions of Windows, where the prompt words might be different. An alternative trick is to add an asterisk '*' to the end of the destination, then xcopy won't prompt for File/Directory. – Govert Jan 28 '13 at 19:40
  • Why does xcopy not recognize the source and/or target as a file when they have a file extension and why does the asterisk work? @Govert – xr280xr Sep 30 '14 at 19:04
  • @xr280xr I have not idea how xcopy works. It might be a good question to ask Raymond Chen: http://blogs.msdn.com/b/oldnewthing – Govert Sep 30 '14 at 21:10
  • @xr280xr Having a dot and some characters after it doesn't make a path a file. Folders can have dot in name, i.e. aaa.txt is a valid folder name. – user3285954 Oct 08 '14 at 17:21
  • @user3285954 Regardless, why does xcopy fail to identify a file as a file, and why does using an asterisk resolve that? (Thanks for the link, Govert) – xr280xr Oct 08 '14 at 17:35
  • 1
    @xr280xr There's no file yet only a path and can't decide only based on path what is the target because folder names can have "extensions" a file names may not have extensions. Copying a file to a folder is a valid operation so can't decide on source either. xcopy seems to ignore wildcards for directories probably this is why a path with wildcard is considered to be a file. To avoid confusion and hacks use copy for files. – user3285954 Oct 08 '14 at 22:33
12

Why don't you use copy instead of xcopy? copy is specifically for files so there will be no confusion.

user3285954
  • 4,499
  • 2
  • 27
  • 19
1

Did you try wrapping the $(TargetPath) in quotes? The ever-so-popular-space-characters-instead-of-underscores-in-all-MS-products tend to mess things up at every corner... Dunno why those dumbos keep doing it...

Like so: if exist "$(TargetPath)"

Csimbi
  • 195
  • 1
  • 3