Question

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?

Was it helpful?

Solution

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
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top