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条回答
forever°为你锁心
2楼-- · 2019-01-29 16:44

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\"
查看更多
家丑人穷心不美
3楼-- · 2019-01-29 16:48

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
查看更多
可以哭但决不认输i
4楼-- · 2019-01-29 16:51

Try this

ROBOCOPY C:\Users\ADMIN\Pictures c:%date:~4,2%%date:~7,2%%date:~10,4% /MOV

This batch will make a new folder and set its name as the current date. (03222013) It then will move any files in the C:\Users\ADMIN\Pictures folder to that newly created folder.

If today is March 22 2013.... All files in C:\Users\ADMIN\Pictures will be moved to a folder named 03222013.

Only files in C:\Users\ADMIN\Pictures will be moved. Any folder in C:\Users\ADMIN\Pictures will not be touched.

查看更多
beautiful°
5楼-- · 2019-01-29 16:55

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.

查看更多
狗以群分
6楼-- · 2019-01-29 16:55
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.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-29 17:01
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 )

查看更多
登录 后发表回答