11

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!

TResponse
  • 3,940
  • 7
  • 43
  • 63
ashlar64
  • 1,054
  • 1
  • 9
  • 24
  • It is not a function that takes parentheses, its name is exist. Use double-quotes. if exist "path" del "path" – Hans Passant Feb 01 '16 at 17:18
  • Are you talking inside the actual xml file itself? Or the editor window in the Properties page in VS? I have tried this over a dozen ways and am having no luck in getting this to work. – ashlar64 Feb 01 '16 at 18:10
  • Another way: ignore the exit code, e.g. http://stackoverflow.com/questions/7912726/how-to-modify-return-code-in-visual-studio-build-events/7913356#7913356 – stijn Feb 01 '16 at 19:08

2 Answers2

16

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.

TResponse
  • 3,940
  • 7
  • 43
  • 63
ashlar64
  • 1,054
  • 1
  • 9
  • 24
4

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.

PerryC
  • 1,233
  • 2
  • 12
  • 28