34

I'm trying to create a folder named Design in the build output folder using th following commandline in the PostBuildEvent in visual studio

mkdir $(TargetDir)Design  ....Runs Successfully but folder is not created
mkdir "$(TargetDir)Design" ....Runs Successfully but folder is not created
MD $(TargetDir)Design  ....Runs Successfully but folder is not created
MD "$(TargetDir)Design"  ....Runs Successfully but folder is not created

Can anyone tell me what I'm doing wrong

5
  • What's the value of your TargetDir ?
    – Tung
    May 9, 2012 at 1:43
  • Its the relative path of the output saved in the buildserver for example \\build\Sol Build\Build_1120943.7
    – Deepak
    May 9, 2012 at 1:54
  • Your syntax looks good. Can you try adding an echo statement after your mkdir command as a sanity check to be sure that the computed path is where you think it is?
    – Tung
    May 9, 2012 at 2:28
  • When you say "Run successfully" what do you mean? I explain, you see the compiler runs and that it compiles right, but how you can prove that the postbuild event is run? Do you set "Run Allways" on the "Run Post-Build event" combo?
    – Steve
    May 9, 2012 at 12:18
  • I made it to run on successful build. The postbuildevent is running because there are other commandlines that deletes some files and rename some folders. But the problem is creating a new folder.
    – Deepak
    May 9, 2012 at 16:14

3 Answers 3

52

You need to do something like:

if not exist DirToCreate mkdir DirToCreate
2
  • 2
    if possible, please try to elaborate your answer with some explanations, etc. Oct 29, 2014 at 3:16
  • 1
    This is an example that you enter in the "Post Build Events" dialog in the project settings. Substitute your directory of choice for DirToCreate.
    – Eric
    Oct 29, 2014 at 15:36
25

This worked for me (where Design is the folder you want to create):

mkdir $(TargetDir)\Design

If you want to check for existence first:

if not exist $(TargetDir)\Design mkdir $(TargetDir)\Design
1
  • 9
    The Directory Path should be enclosed in Double Quotes as below: if not exist "$(TargetDir)\Design" mkdir "$(TargetDir)\Design" Jul 10, 2017 at 16:30
5

In addition to the two previous answers, you could use a variable like this :

SET path=$(TargetDir)\Design
if not exist "%path%" mkdir "%path%"

That way, you'll avoid any duplication. (Tested with VS2019)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.