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

ashlar64 picture ashlar64 · Feb 1, 2016 · Viewed 10.2k times · Source

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!

Answer

ashlar64 picture ashlar64 · Feb 1, 2016

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.