Create folder while publishing with MSBuild

I am Trying to create a Temp folder while doing the publish of my Web Application Project with file system location in Visual Studio 2015.I have tried adding the code below in .csfile but its not Creating folder in the target location of the Publish Directory.If given Static location to create folder its working fine.How can get Publish Directory dynamically?

<Target Name="CustomCollectFiles" AfterTargets="Build">
    <MakeDir Directories="$(PublishDirectory)$(OutputDirectoryTemp)" />
  </Target>
  <PropertyGroup>   
    <OutputDirectoryTemp>\Temp\</OutputDirectoryTemp> 
  </PropertyGroup>

Solution 1:

Create folder while publishing with MSBuild

You need to declare attributes PublishDirectory by creating an element with the attribute name as a child of the PropertyGroup element, because there is no such MSBuild Macros for this, you can check the Common Macros for Build Commands and Properties.

As test, I set PublishDirectory to the path D:\Publish:

  <PropertyGroup>
    <PublishDirectory>D:\Publish</PublishDirectory>
  </PropertyGroup>

Then I add a output command line <Message Test="xxx" /> in the target to output the content of $(PublishDirectory)$(OutputDirectoryTemp):

  <Target Name="CustomCollectFiles" AfterTargets="Build">
    <MakeDir Directories="$(PublishDirectory)$(OutputDirectoryTemp)" />

    <Message Text="$(PublishDirectory)$(OutputDirectoryTemp)">
    </Message>
  </Target>
  <PropertyGroup>
    <OutputDirectoryTemp>\Temp\</OutputDirectoryTemp>
  </PropertyGroup>

  <PropertyGroup>
    <PublishDirectory>D:\Publish</PublishDirectory>
  </PropertyGroup>

In the output window, you will see following log:

enter image description here

And the folder Temp would be created:

enter image description here

If you have already defined the variable of PublishDirectory, you can try to use the output command line <Message Test="xxx" /> check if the path is correct.

Update:

I will like to get the target location of the Publish Directory dynamically not static or hard coded

Since you want to get the target location of the Publish Directory dynamically, as we know, the location of the Publish Directory was stored in the .pubxml file, in the node <publishUrl>D:\Publish\Test</publishUrl>, to get this value dynamically, we could use $(publishUrl) to get this value in the target, However, the publishing process is after the build, we could not get this value in the build process, so we need to change the order of this target from AfterTargets="Build" to AfterTargets="PipelineTransformPhase". The target should be:

  <Target Name="CustomCollectFiles" AfterTargets="PipelineTransformPhase">
    <MakeDir Directories="$(publishUrl)$(OutputDirectoryTemp)" />
    <Message Text="$(publishUrl)$(OutputDirectoryTemp)" />
  </Target>
  <PropertyGroup>
    <OutputDirectoryTemp>\Temp\</OutputDirectoryTemp>
  </PropertyGroup>

In this case, when you publish your project to the system location, the publish directory will be stored in the publishUrl, we could get it in that target.

Hope this helps.