Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio pre-build event check to see if a directory (and file) exists and delete it if it does

Every time I do a build I would like for this Pre-build event to occur:

del  $(ProjectDir)\obj\Debug\Package\PackageTmp\web.config

This works fine if the directory is there. But if the directory is not there then it will cause the build to fail. I tried doing something like this to check if the directory was there:

if Exists('$(ProjectDir)\obj\Debug\Package\PackageTmp\')   
del  $(ProjectDir)\obj\Debug\Package\PackageTmp\web.config

But I believe my syntax is wrong because I get a exit code of 255. What would be the proper way to get this to work?

Thanks!

like image 420
ashlar64 Avatar asked Feb 01 '16 17:02

ashlar64


People also ask

How do I debug a post-build event in Visual Studio?

Another way is to check the bin\debug dir for 'PreBuildEvent. bat' or 'PostBuildEvent. bat' which are the file that Visual Studio creates and run during the build events, if there is an error the files remain in the output dir and you can run them manually and spot the error.

What is PostBuildEvent?

PostBuildEvent. This event executes after the build finishes. The following table lists each use-in-build element: XML Element. Description.

How to use post-build event in Visual Studio?

In the Post-build event command line box, specify the syntax of the build event. Add a call statement before all post-build commands that run . bat files. For example, call C:\MyFile.

How do I get build command in Visual Studio?

To compile source files from within the Visual Studio IDE, choose the Build command from the Build menu. When you build project files by using the Visual Studio IDE, you can display information about the associated vbc command and its switches in the output window.


2 Answers

Apparently this works:

if EXIST "$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config" (
del  "$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config"
)

The above piece of code was one of the first ways I tried doing this. But it kept failing. After many more attempts I ended up restarting Visual Studio 2015 and entering that code again and then it started working.

like image 173
ashlar64 Avatar answered Sep 22 '22 17:09

ashlar64


I would use a target to accomplish this. Specifically, I would suggest overriding a BeforeBuild target. There are a couple different ways to do this, but the simplest is to modify your .vcxproj file IMHO.

At the bottom of your project file (you can edit it by right-clicking on your project in Visual Studio -> Unload Project, then right-click again and choose to edit that project) you should see an <Import ... line. Add a target after that line that's something like this:

<Target Name="BeforeBuild" Condition="Exists('$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config')">
  <Delete Files="$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config" />
</Target>

See How to: Extend the Visual Studio Build Process for more information on overriding Before and After targets.

like image 42
PerryC Avatar answered Sep 21 '22 17:09

PerryC