Create folder with batch but only if it doesn't already exist

前端 未结 9 1744
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 09:03

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
9条回答
  •  佛祖请我去吃肉
    2020-12-12 09:56

    I use this way, you should put a backslash at the end of the directory name to avoid that place exists in a file without extension with the same name as the directory you specified, never use "C:\VTS" because it can a file exists with the name "VTS" saved in "C:" partition, the correct way is to use "C:\VTS\", check out the backslash after the VTS, so is the right way.

    @echo off
    @break off
    @title Create folder with batch but only if it doesn't already exist - D3F4ULT
    @color 0a
    @cls
    
    setlocal EnableDelayedExpansion
    
    if not exist "C:\VTS\" (
      mkdir "C:\VTS\"
      if "!errorlevel!" EQU "0" (
        echo Folder created successfully
      ) else (
        echo Error while creating folder
      )
    ) else (
      echo Folder already exists
    )
    
    pause
    exit
    

提交回复
热议问题