Create folder with batch but only if it doesn'

2019-01-29 16:06发布

Can anybody tell me how to do the following in in a Windows batch script? (*.bat):

  • Create a folder only if it doesn't already exist

In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn't already exist. I don't want to overwrite the contents of the folder if it already exists and the batch is executed.

9条回答
The star\"
2楼-- · 2019-01-29 17:03

Just call mkdir C:\VTS no matter what. It will simply report that the subdirectory already exists.

Edit: As others have noted, this does set the %ERRORLEVEL% if the folder already exists. If your batch (or any processes calling it) doesn't care about the error level, this method works nicely. Since the question made no mention of avoiding the error level, this answer is perfectly valid. It fulfills the needs of creating the folder if it doesn't exist, and it doesn't overwrite the contents of an existing folder. Otherwise follow Martin Schapendonk's answer.

查看更多
男人必须洒脱
3楼-- · 2019-01-29 17:07
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
查看更多
放荡不羁爱自由
4楼-- · 2019-01-29 17:07

You can use:

if not exist "C:\VTS\" mkdir "C:\VTS"

You can also expand the code to replace any missing expected files.

if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"
查看更多
登录 后发表回答