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

前端 未结 9 1690
佛祖请我去吃肉
佛祖请我去吃肉 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:42
    mkdir C:\VTS 2> NUL
    

    create a folder called VTS and output A subdirectory or file TEST already exists to NUL.

    or

    (C:&(mkdir "C:\VTS" 2> NUL))&
    

    change the drive letter to C:, mkdir, output error to NUL and run the next command.

    0 讨论(0)
  • 2020-12-12 09:42

    This should work for you:

    IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder
    

    However, there is another method, but it may not be 100% useful:

    md \path\to\your\folder >NUL 2>NUL
    

    This one creates the folder, but does not show the error output if folder exists. I highly recommend that you use the first one. The second one is if you have problems with the other.

    0 讨论(0)
  • 2020-12-12 09:42

    i created this for my script I use in my work for eyebeam.

    :CREATES A CHECK VARIABLE
    
    set lookup=0
    
    :CHECKS IF THE FOLDER ALREADY EXIST"
    
    IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)
    
    :IF CHECK is still 0 which means does not exist. It creates the folder
    
    IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"
    
    0 讨论(0)
  • 2020-12-12 09:44
    set myDIR=LOG
    IF not exist %myDIR% (mkdir %myDIR%)
    
    0 讨论(0)
  • 2020-12-12 09:49

    You just use this: if not exist "C:\VTS\" mkdir C:\VTS it wll create a directory only if the folder does not exist.

    Note that this existence test will return true only if VTS exists and is a directory. If it is not there, or is there as a file, the mkdir command will run, and should cause an error. You might want to check for whether VTS exists as a file as well.

    0 讨论(0)
  • 2020-12-12 09:52
    if exist C:\VTS\NUL echo "Folder already exists"
    
    if not exist C:\VTS\NUL echo "Folder does not exist"
    

    See also https://support.microsoft.com/en-us/kb/65994

    (Update March 7, 2018; Microsoft article is down, archive on https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )

    0 讨论(0)
提交回复
热议问题