0

I'm currently trying to create a post-build script in Visual Studio to clean up the directory I build releases to. The batch file executes fine and all expected behaviour takes place but in Visual Studio I get a message saying "Call post-build.bat" exited with code 1.

My batch script is as follows:

@echo off
GOTO :CleanFiles
:CleanFiles
set direct = %CD%
IF EXIST *.pdb (
    DEL *.pdb
)
IF EXIST *.vshost.exe.config (
    DEL *.vshost.exe.config
)
IF EXIST *.WPF4.xml (
    DEL *.WPF4.xml
)
IF EXIST System.Windows.Interactivity.xml (
    DEL System.Windows.Interactivity.xml
)
IF ERRORLEVEL 0 GOTO MakeDirectories

:MakeDirectories
IF EXIST %direct%lib (
    rmdir /s %direct%lib
    rmdir /s %direct%config
    rmdir /s %direct%output
)
mkdir %direct%lib
mkdir %direct%config
mkdir %direct%output
IF ERRORLEVEL 0 GOTO MoveFiles
:MoveFiles
move /y %direct%*.dll %direct%lib
move /y %direct%*.xml %direct%config
move /y %direct%*.xlsx %direct%output
IF ERRORLEVEL 0 GOTO DelBatch
:DelBatch
DEL "%~f0"

EDIT:

It's the DEL command at the bottom of the batch file. Is there any way I can fix this?

DanteTheEgregore
  • 1,530
  • 5
  • 23
  • 44
  • `IF ERRORLEVEL 0 GOTO DelBatch` and the next line `:delbatch` doesn't make any sense. You have such code more often in your script. – Endoro Jul 19 '13 at 18:51
  • @Endoro The GOTOs were an effort to debug it as it won't go to the next section if the error level jumps to 1. I was hoping I'd be able to at least pin down what was causing it. – DanteTheEgregore Jul 19 '13 at 18:53
  • I know it's not really something to worry about as everything else works fine but I can't use the debugger unless I remove that line or disable the post-build event for debug. – DanteTheEgregore Jul 19 '13 at 18:58
  • You can't delete the running batch without an error message. What do you think? Why can't you live with that message? – Endoro Jul 19 '13 at 19:02
  • Because Visual Studio assumes there's build errors if I try to build the application and get that error. – DanteTheEgregore Jul 19 '13 at 19:24
  • I'm just going to remove it and delete from the build events. – DanteTheEgregore Jul 19 '13 at 19:27
  • @Endoro - There are ways for a batch file to delete itself without error. My answer below is the simplest way for the OP's situation, but see also http://stackoverflow.com/a/17006630/1012053 – dbenham Jul 19 '13 at 21:42
  • @dbenham yes, you are right, this is a good idea! But unfortunately the cmd window is closed. – Endoro Jul 19 '13 at 22:42
  • 1
    @Endoro - Yes, it is closed, which I believe is fine for a Visual Studio post-build script. But the link I gave shows how to delete self without error and keep the console open. – dbenham Jul 19 '13 at 22:56

1 Answers1

5

You get the error because the batch process is looking for the next line to execute, but the file is gone.

I believe all you need to do is explictly EXIT after deleting the file. It is important that the EXIT be parsed and executed in the same block as the DEL command.

del "%~f0"&exit

or

(
  del "%~f0"
  exit
)
dbenham
  • 127,446
  • 28
  • 251
  • 390