I copied your script and met the same issue. According to the error message The syntax of the command is incorrect, something is wrong with the syntax, maybe a space, a new-line which I'm not certainly sure:(
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?
As a workaround: Maybe you can remove the Judgment statement from inside the PostBuildEvent. Then use msbuild condition to judge if the variable is defined or not, try using a script like this:
<!--<PropertyGroup>
<MyEnvVar>C:\Test</MyEnvVar>
</PropertyGroup>-->
<PropertyGroup>
<PostBuildEvent Condition="$(MyEnvVar)!=''">
IF NOT EXIST "C:\tmp\" mkdir "C:\tmp\"
IF NOT EXIST "$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\" mkdir "$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\"
copy "$(TargetPath)" "$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\"
</PostBuildEvent>
</PropertyGroup>
In vs, right-click project and add the script into it, the location should be:
<Project...>
...
<!--<PropertyGroup>
<MyEnvVar>C:\Test</MyEnvVar>
</PropertyGroup>-->
<PropertyGroup>
<PostBuildEvent Condition="$(MyEnvVar)!=''">
IF NOT EXIST "C:\tmp\" mkdir "C:\tmp\"
IF NOT EXIST "$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\" mkdir "$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\"
copy "$(TargetPath)" "$(MyEnvVar)\Addins\Software\bin\$(PlatformName)\"
</PostBuildEvent>
</PropertyGroup>
</Project>
Then right-click the project and choose reload project and check if it helps to achieve your original goal.
1.The PostBuildEvent will work only when MyEnvVar is defined and has its value.
2.The two mkdir command will be called when the C:\tmp\ and $(MyEnvVar)\Addins\Software\bin\$(PlatformName)\ directory not exists
3.Then the copy command will copy the output of your project to Destination Folder, I remove the $(TargetFileName) because it represents xxx.exe or xxx.dll, I think it's not necessary or maybe what you really want is $(AssemblyName). Please let me know if it helps resolve your issue:)