visual-studiovisual-studio-2022post-build-event

How to test if a folder exists in a post build event?


In a database project in Visual Studio 2022, I would like to copy files only if the target folder exists. Assume the target folder is called c:\files

I did something like:

if exist "c:\files\"
xcopy /Y  $(ProjectDir)$(OutputPath)$(TargetDatabase).dacpac c:\files

but the 'if exist' line gives an error "...... exited with code 255.". I am not sure 'if exist' is even correct. The xcopy line copies fine without the if statement.


Solution

  • The if exist is fine, just the whitespace syntax seems problematic. It should work if you put it all on one line or use brackets like this:

    if exist "c:\files\" (
    xcopy /Y  $(ProjectDir)$(OutputPath)$(TargetDatabase).dacpac c:\files
    )