Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if variable is defined in PostBuildEvent

I am using the following PostBuildEvent

<PostBuildEvent>
  IF DEFINED $(MyEnvVar) (
   mkdir "C:\tmp\"
   copy "$(TargetPath)" "$(MyEnvVar)/Addins/Software/bin/$(PlatformName)/$(TargetFileName)"
 )
</PostBuildEvent>

This event works if my MyEnvVar is defined. However, I get an error MSB3073 (exited with code 255) if the variable is not defined.

How can I define my PostBuild task to do some cmd operations, (create folders copy files as shown above), if the variable exists, or do nothing if it not?

like image 587
Sergioet Avatar asked Oct 20 '25 03:10

Sergioet


1 Answers

You should not really be using post build events, at all, ever. Just create a target and have it run after the build target. In that target, have it copy the files you want. Something like this:

<Target Name="CopyMyStuff" AfterTargets="Build" Condition="exists('$(MyEnvVar)')" >
   <Copy SourceFiles="$(TargetPath)" DestinationFolder="$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\" SkipUnchangedFiles="true" />
</Target>

I think this copy task will create the directory if it doesn't exist. A nice bonus.

like image 194
C Johnson Avatar answered Oct 22 '25 02:10

C Johnson