When building with Visual Studio we copy a bunch of dlls through the post build event step.
Currently the build step looks like such:
xcopy <source/file> <target/file>
if errorlevel 1 goto VCEnd
xcopy <...> <...>
if errorlevel 1 goto VCEnd
...
The problem with that is that if xcopy doesn't find a file because it has a wrong name or it doesn't exist the build will still pass. The reason is that although xcopy prints File not found - <some_file>
it does not set the errorlevel
.
My task is to make sure the build actually fails to make the developer aware of this issue more quickly.
My idea is to create a new subroutine that checks if the build log contains "File not found" and call this subroutine after every step:
xcopy <source/file1> <target/file1>
call :check_xcopy_success
xcopy <source/file2> <target/file2>
call :check_xcopy_success
...
goto :end
:check_xcopy_success
type $(Configuration)\$(AssemblyName).log | findstr /i /c:"File not found" > nul
if %errorlevel%==0 ( echo "Postbuild: error 1234: Unable to copy lib" && goto VCEnd )
exit /b
:end
However that doesn't do anything. At first I thought that the file couldn't be opened for reading because VS is still writing to it but I am able to get the file contents through a for loop:
:check_xcopy_success
setlocal enabledelayedexpansion
set content=
for /f "delims=" %%i in ('type $(Configuration)\$(AssemblyName).log') do set content=!content! %%i
echo !content!
endlocal
exit /b
This will show the content of the file in a single line, which would be fine for me to use with findstr
but using it in that way doesn't do anything either:
(echo !content! | findstr /i /c:"File not found" > nul)
echo %errorlevel%
will output 0
no matter the search string or the content of the file so conditionally outputting an error that signals VS that the build failed this way doesn't work either.
Where am I going wrong? Do you have any other idea of what I could do?