使用批处理创建文件夹,但前提是该文件夹尚不存在
谁能告诉我如何在 Windows 批处理脚本中执行以下操作? (*.bat
):
- 仅当文件夹尚不存在时才创建该文件夹
更详细地说,我想在 C: 上创建一个名为
驱动器,但前提是该文件夹尚不存在。如果文件夹已经存在并且批处理已执行,我不想覆盖该文件夹的内容。VTS
的文件夹\
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您只需使用以下命令:
if not exit "C:\VTS\" mkdir C:\VTS
仅当该文件夹不存在时,它才会创建一个目录。请注意,仅当 VTS 存在并且是目录时,此存在测试才会返回 true。如果它不存在,或者作为文件存在,则 mkdir 命令将运行,并且应该会导致错误。您可能还想检查 VTS 是否也作为文件存在。
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.
另请参阅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 )
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 )
无论如何,只需调用
mkdir C:\VTS
即可。它只会报告子目录已经存在。编辑:正如其他人所指出的,如果文件夹已存在,这确实会设置%ERRORLEVEL%。如果您的批处理(或调用它的任何进程)不关心错误级别,则此方法效果很好。由于问题没有提到避免错误级别,所以这个答案是完全有效的。它满足创建文件夹(如果文件夹不存在)的需要,并且不会覆盖现有文件夹的内容。否则请遵循Martin Schapendonk 的回答。
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.
创建一个名为
VTS
的文件夹,并将A subdirectory or file TEST hasexists
输出到NUL
。或者
将盘符更改为
C:
、mkdir
,将错误输出为NUL
并运行下一个命令。create a folder called
VTS
and outputA subdirectory or file TEST already exists
toNUL
.or
change the drive letter to
C:
,mkdir
, output error toNUL
and run the next command.我使用这种方式,你应该在目录名末尾加一个反斜杠以避免该位置存在于没有扩展名且与你指定的目录同名的文件中,切勿使用” C:\VTS" 因为"C:"分区中可能存在名为"VTS"的文件,正确的方法是使用< strong>“C:\VTS\”,检查一下VTS后面的反斜杠,这样才是正确的方法。
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.
您可以使用:
您还可以扩展代码来替换任何丢失的预期文件。
You can use:
You can also expand the code to replace any missing expected files.
如果文件夹不存在,您需要创建一个文件夹吗?好吧,这里有一个如何做到这一点的示例。
首先,我通过输入以下代码来检查该文件夹是否尚不存在:
So if I run the code.如果该文件夹已经存在,它将什么也不做。如果文件夹已经存在,这就是我们要做的事情:
现在,如果我运行代码,它将重新创建该文件夹(如果它已经存在)。这是示例代码:
现在
if exit
部分是可选的。如果该文件夹已经存在,您可以跳转到一个标签,如下所示:希望这可以帮助解决您的问题。
You need to create a folder if it doesn't exist eh? Well, here is an example of how to do it.
First, I check to see if the folder doesn't already exist by entering this code:
So if I run the code. And if the folder already exists, It will do nothing. This is what we do if the folder already exists:
Now if I run the code, It will re-create the folder if it already exists. This is the example code:
Now the
if exist
part is Optional. If the folder already exists, you can jump to an label instead like this:Hopefully, this could help with your problem.
这应该对您有用:
但是,还有另一种方法,但它可能不是 100% 有用:
此方法创建文件夹,但如果文件夹存在,则不会显示错误输出。我强烈建议您使用第一个。第二个是如果你和另一个有问题。
This should work for you:
However, there is another method, but it may not be 100% useful:
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.
我正在从 Dockerfile 在 docker 上下文中创建一个文件夹,以避免如果 mkdir 命令上已存在文件夹,则构建错误停止,并且这里的任何答案都不适合我,但有些启发我试试这个,它有效(
RUN
是 Dockerfile 命令,默认情况下在 Windows docker 上,在 cmd 上下文中运行):I'm creating a folder in the context of docker from a Dockerfile to avoid build stopping in error if folder already exists on a
mkdir
command, and none of answer here worked for me, but some inspired me to try this, that works (theRUN
is Dockerfile command that, by default on Windows docker, runs in context of cmd):就我个人而言,我会这样做:
让我们分解一下:
如果不存在“C:\YourFolderPathHere\”
:这会检查文件夹。路径后面的反斜杠(\)非常重要,否则它将查找文件而不是文件夹。mkdir C:\YourFolderPathHere\
:如果上述语句成立,则创建目录。echo 文件夹已存在!
:打印到控制台它已经存在。这是相同的代码,但带有注释:
一行版本:
如果不存在 "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\
不过还没有测试过,所以不要引用我的话。
Personally, I would do this:
Let's break that down:
if not exist "C:\YourFolderPathHere\"
: this checks for the folder. The Backslash (\) after the path is very important, else it will look for a file rather than a folder.mkdir C:\YourFolderPathHere\
: Creates the directory if the above statement is true.echo Folder already exists!
: prints to console that it already exists.Here's the same code, but with comments:
One-line version:
if not exist "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\
Haven't tested it though, so don't quote me on it.
我为我在 Eyebeam 工作中使用的脚本创建了这个。
i created this for my script I use in my work for eyebeam.
您可以使用以下批处理脚本来执行此操作
此外,您还可以根据创建或不创建文件夹来执行更多命令。
You can do this using the following batch script
Also, you can execute more commands due to creating or not creating the folder.
您可以将
/I
选项添加到 mkdir 命令中。/I
选项告诉mkdir
仅在目录尚不存在时创建该目录。如果该目录已存在,则 mkdir 命令将不执行任何操作并继续执行下一个命令。You can add the
/I
option to the mkdir command. The/I
option tellsmkdir
to only create the directory if it doesn't already exist. If the directory already exists, themkdir
command will do nothing and continue with the next command.