windows 批量创建文件夹,但前提是它不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4165387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create folder with batch but only if it doesn't already exist
提问by Bill
Can anybody tell me how to do the following in in a Windows batch script? (*.bat
):
谁能告诉我如何在 Windows 批处理脚本中执行以下操作?( *.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.
更详细地说,我想创建一个VTS
在C:\
驱动器上命名的文件夹,但前提是该文件夹不存在。如果文件夹已经存在并且批处理被执行,我不想覆盖文件夹的内容。
回答by The Answerer
You just use this: if not exist "C:\VTS\" mkdir C:\VTS
it wll create a directory only if the folder does not exist.
您只需使用它:if not exist "C:\VTS\" mkdir C:\VTS
仅当文件夹不存在时,它才会创建一个目录。
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.
请注意,仅当 VTS 存在且是目录时,此存在性测试才会返回 true。如果它不存在,或者作为文件存在,则 mkdir 命令将运行,并且应该会导致错误。您可能还想检查 VTS 是否作为文件存在。
回答by Martin Schapendonk
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
另请参阅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)
(2018 年 3 月 7 日更新;Microsoft 文章已关闭,存档于https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994)
回答by Agent_9191
Just call mkdir C:\VTS
no matter what. It will simply report that the subdirectory already exists.
随便打个电话mkdir C:\VTS
就行。它只会报告子目录已经存在。
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.
编辑:正如其他人所指出的,如果文件夹已经存在,这会设置 %ERRORLEVEL%。如果您的批处理(或任何调用它的进程)不关心错误级别,则此方法效果很好。由于该问题未提及避免错误级别,因此此答案完全有效。如果文件夹不存在,它满足创建文件夹的需要,并且它不会覆盖现有文件夹的内容。否则请遵循Martin Schapendonk 的回答。
回答by 3years2late
mkdir C:\VTS 2> NUL
create a folder called VTS
and output A subdirectory or file TEST already exists
to NUL
.
创建一个名为的文件夹VTS
并输出A subdirectory or file TEST already exists
到NUL
.
or
或者
(C:&(mkdir "C:\VTS" 2> NUL))&
change the drive letter to C:
, mkdir
, output error to NUL
and run the next command.
将驱动器号更改为C:
, mkdir
,输出错误NUL
并运行下一个命令。
回答by Robie
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
回答by D3F4ULT
I use this way, you should put a backslash at the end of the directory nameto 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.
我用这种方式,你应该在目录名的末尾加上一个反斜杠,以避免在没有扩展名的文件中存在与你指定的目录相同的名称,永远不要使用“C:\VTS”,因为它可以是一个文件存在名称为“VTS”的保存在“C:”分区中,正确的方法是使用“C:\VTS\”,查看VTS后面的反斜杠,正确的方法是。
@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
回答by D3F4ULT
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"
回答by Winston
i created this for my script I use in my work for eyebeam.
我为我在工作中用于 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\"