How do I fix MSB3073 error in my post-build event?
Asked Answered
F

16

44

I'm working on a project that requires that DLLs generated by building my solution to be copied from the bin folder to another folder, both of which are on my machine, in my C drive. I've written a batch file that uses xcopy to accomplish this, which you can see here:

xcopy /s /y /q "C:\Users\scogan\Documents\Visual Studio 2012\Projects\Organizr\Server\bin\Debug\Organizr.Services.dll" "C:\inetpub\wwwroot\AppServer\bin\"
xcopy /s /y /q "C:\Users\scogan\Documents\Visual Studio 2012\Projects\Organizr\Server\bin\Debug\Organizr.Services.pdb" "C:\inetpub\wwwroot\AppServer\bin\"

Now, I've tried numerous iterations of this file, which is located at:

C:\Users\scogan\Desktop\CopyFiles.bat

so my post-build event command line looks like this:

call C:\Users\scogan\Desktop\CopyFiles.bat

I've run this batch file on its own with two text files in folders on my desktop, and it works fine. I've also run it as it is with the files I need to copy on its own, and that works fine, too. However, when I try to run this as a post-build event, I get this output:

1>  Organizr -> C:\Users\scogan\Documents\Visual Studio 2012\Projects\Organizr\Client\bin\Debug\Organizr.exe
1>  File not found - Organizr.Services.dll
1>  0 File(s) copied
1>  0 File(s) copied
1>  File not found - Organizr.Services.pdb
1>c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(4291,5): error MSB3073: The command "call C:\Users\scogan\Desktop\CopyFiles.bat" exited with code 4.

I've done some research, and found that error code 4 means that "Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line."

I've also looked up what MSB3073 is, and haven't really found much that can help me there. So, my question is what am I doing wrong? Are the absolute paths messing it up? Any help here is appreciated.

Ferocity answered 12/6, 2013 at 21:1 Comment(0)
F
34

Playing around with different project properties, I found that the project build order was the problem. The project that generated the files I wanted to copy was built second, but the project that was running the batch file as a post-build event was built first, so I simply attached the build event to the second project instead, and it works just fine. Thanks for your help, everyone, though.

Ferocity answered 13/6, 2013 at 13:33 Comment(1)
This fixed the problem for me. A new module was added that used the same files as the one with the post build event. I added new dependencies and it worked again.Marlee
J
18

Prefer the MsBuild "Copy" task in an AfterBuild target over a post-build event.

Append this Target into your project file and remove the PostBuildEvent.

<Target Name="AfterBuild">
    <Copy SourceFiles="C:\Users\scogan\Documents\Visual Studio 2012\Projects\Organizr\Server\bin\Debug\Organizr.Services.*" 
          DestinationFolder="C:\inetpub\wwwroot\AppServer\bin\" 
          OverwriteReadOnlyFiles="true" 
          SkipUnchangedFiles="false" />
</Target>
Jackelinejackelyn answered 12/6, 2013 at 21:43 Comment(6)
Cool, I'll give that a shot tomorrow when I get back to work. I'm just stumped because this only didn't work with these two files. I tried it with two files not related to the project on my desktop, and that worked fine as a post-build event.Ferocity
Even better when you specify the file using the metadata rather than a hardcoded path e.g. SourceFiles="$(TargetPath)".Galimatias
@Nick Carlson where do I actually append this? What do you mean by the project file? Like, MainWindow.xaml?Ferocity
Actually, I figured out why my post-build event wasn't working. I'll post it as an answer, but thank you for your help.Ferocity
@SeanCogan <Target /> is an msbuild element you can append to your .csproj file.Jackelinejackelyn
@NickCarlson Gotcha, thanks. I'll keep that in mind for the future, but for this instance I figured out what my problem was and fixed it.Ferocity
H
12

For what it's worth, the problem in my case was caused by using '/' as the directory separator in a copy command. Must use backslashes.

Hypothyroidism answered 7/9, 2015 at 0:23 Comment(0)
T
6

In my case, the dll I was creating by building the project was still in use in the background. I killed the application and then xcopy worked fine as expected.

Teri answered 1/11, 2016 at 5:3 Comment(0)
R
6

The specified error is related to the post built event. Somehow VS tool is not able to copy the files to the destination folder. There can be many reasons for it. To check the exact error cause go to Tools > Option> Project and Solution > Built and run, and change "MsBuild project build output verbosity" to "Diagnostic". It will give you enough information to detect the actual problem.

Regina answered 1/6, 2018 at 9:29 Comment(1)
Not necessarily. I just ran a build with logging set to Diagnostic, and the messages offered no actionable clues.Drucilladrucy
C
4

This is too late but posting my experience for people looking at it later:-

In MS VS 2010 I had the same issue. It got resolved by putting quotes to post build copy command args which contained spaces!

In Project Properties --> Configuration Properties --> Build Events --> Post-Build Event --> Command Line change:

copy $(ProjectDir)a\b\c $(OutputPath)

to

copy "$(ProjectDir)a\b\c" "$(OutputPath)"

Corporative answered 13/4, 2017 at 20:36 Comment(0)
U
1

If the problem still persists even after putting the after build in the correct project try using "copy" instead of xcopy. This worked for me.

Utilitarianism answered 5/10, 2016 at 16:52 Comment(0)
G
1

I've found the issue happens when you have multiple projects building in parallel and one or more of the projects are attempting to copy the same files, creating race conditions that will result in occasional errors. So how to solve it?

There's a lot of options, as above just changing things around could solve the issue for some people. More robust solutions would be...

  • Restrict the files being copied i.e. instead of xcopy $(TargetDir)*.*"... instead do xcopy "$(TargetDir)$(TargetName).*"...

  • Catch the error and retry i.e:

    :loop
    xcopy /Y /R /S /J /Q  "$(TargetDir)$(TargetName).*" "somewhere"
    if ErrorLevel 1 goto loop
    
  • Use robocopy instead of xcopy

  • You probably won't want to do this as it will increase your build times, but you could reduce the maximum number of parallel project builds to 1 ...

    enter image description here

Gast answered 13/9, 2017 at 2:23 Comment(0)
F
1

The Post-Build Event (under Build Events, in the properties dialog) of an imported project, had an environment variable which was not defined.
Navigated to Control Panel\All Control Panel Items\System\Advanced system settings to add the appropriate environment variable, and doing no more than restarting VS2017 resolved the error.
Also, following on from @Seans and other answers regarding multiple project races/contentions, create a temp folder in the output folder like so,

like so

and select the project producing the preferred output:

enter image description here

and build (no rebuild/clean) is a speedy solution.

Farouche answered 11/1, 2018 at 5:34 Comment(0)
H
1

I solved it by doing the following:

In Visual studio I went in Project -> Project Dependencies

I selected the XXX.Test solution and told it that it also depends on the XXX solution to make the post-build events in the XXX.Test solution not generate this error (exit with code 4).

Hysteroid answered 5/10, 2018 at 13:31 Comment(0)
M
1

Following thing you should do before to run copy command if you facing some issue with copy command

  1. open solution as a administrator and build the solution.
  2. if you have problem like "0 File(s) copied" check you source and destination path. might you are using wrong path. it would be better if you run the same command in "command prompt" to check whether it is working fine or not.
Marra answered 27/11, 2018 at 7:1 Comment(0)
G
0

I had the same problem for my Test project. I found out why my post build event wasn't working and that's because I was copying files before running the $(ProjectName).exe command and some of these files were required for Test project itself. Hence, by just moving $(ProjectName).exe as the first command fix the issue.

Gilley answered 13/10, 2016 at 2:59 Comment(0)
I
0

I faced this issue recently and surprisingly only i was having this problem and none of my team members were facing this issue when building the project code.

On debugging i found that my code directory had spacing issue , It was D:\GIT Workspace\abc\xyz.

As a quick fix i changed it to D:\GITWS\abc\xyz and it solved the problem.

Infantilism answered 13/2, 2020 at 10:1 Comment(0)
T
0

I was getting this error after downloading some source code from Github. Specifically the rust oxide development framework. My problem is that the Steam.ps1 script file, that's used to update some of the dlls from Steam was blocked by the OS. I had to open the files properties an UNBLOCK it. I had not realized this was done to ps1 files as well as exes.

Tinnitus answered 10/5, 2020 at 15:35 Comment(0)
T
0

In my case a setting mismatch between Project's Configuration Properties->General->Output Directory setting and Linker->General->Output Directory.

There was a warning about it during linking.

Tavy answered 1/8, 2022 at 13:55 Comment(0)
H
0

I was facing a similar issue where it said it cannot copy a DLL from my build location to destination. The issue was my project path contained spaces, removing them the error was gone.

Heddle answered 22/2, 2023 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.