18

If I try to run mkdir build to create a build directory, if the directory already exists, this error is thrown: A subdirectory or file build already exists.. I need to delete and overwrite this directory. What is the command for that?

5 Answers 5

13

You can delete the build directory with

rd /s /q build

or

if exist build rd /s /q build
1
  • For best results, run this command twice. It will sometimes fail if, e.g., Windows Search happens to be indexing that directory at just the wrong time. May 14, 2019 at 5:28
8

I wanted to create directory only if it does not exist
If it exists, nothing to do

Below worked great in the bat file:

if not exist someDir1 mkdir someDir1
3

I don't think it is possible to use the mkdir command to do that natively (though if you were will to do a bit more scripting, it would be possible).

A simple alternative is the following command in powershell:

New-Item path -type directory -force

Where path is something like C:\users\name\build

For more on New-Item see: http://technet.microsoft.com/en-us/library/ee176914.aspx

1
  • I can't execute powershell as I'm calling this from a rake script. Aug 19, 2012 at 21:29
0

You can try the rd command to remove the directory. You have to ensure the directory is empty first though.

1
  • This throws an error if there are elements in the directory. Aug 19, 2012 at 21:27
-3

This command can help:

mkdir -p a & rm -r a & mkdir a

This my answer in stackoverflow

1
  • 1
    Please read the question again carefully. Your answer does not answer the original question. OP is using Windows not Unix.
    – DavidPostill
    Aug 27, 2016 at 7:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .