windows 在批处理脚本中创建文件夹并忽略它是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44931623/
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 in batch script and ignore if it exists
提问by steavy
How do I create folder(and any subfolders) in batch script? What is important, if folder(or any subfolders) already exists, it should not return error.
如何在批处理脚本中创建文件夹(和任何子文件夹)?重要的是,如果文件夹(或任何子文件夹)已经存在,则不应返回错误。
For example, something like this:
例如,这样的事情:
mkdir mydir
- success(directory is now created)mkdir mydir\subdir
- success(nowmydir
containssubdir
)mkdir mydir
- success(folder already exists, should notthrow an error)mkdir mydir\subdir
- success(folders already exists, should notthrow an error)
mkdir mydir
- 成功(目录现已创建)mkdir mydir\subdir
- 成功(现在mydir
包含subdir
)mkdir mydir
-成功(文件夹已经存在,应该不会引发错误)mkdir mydir\subdir
-成功(文件夹已经存在,应该不会引发错误)
What I actually need is just to ensurethat folders structure exists.
我真正需要的只是确保文件夹结构存在。
回答by phuclv
You need to check for path and create if it doesn't exist
您需要检查路径并创建它是否不存在
if not exist mydir\subdir md mydir\subdir
Or you can also redirect stderr
或者你也可以重定向 stderr
md mydir\subdir 2>NUL
You don't need to run mkdir mydir
first because
你不需要先跑,mkdir mydir
因为
MD creates any intermediate directories in the path, if needed.
如果需要,MD 在路径中创建任何中间目录。
when command extensions are enabled
启用命令扩展时
回答by Mofi
A standard method to create a directory structure is:
创建目录结构的标准方法是:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Directory=mydir\subdir 1\subdir 2"
md "%Directory%" 2>nul
if not exist "%Directory%\*" (
echo Failed to create directory "%Directory%"
pause
goto :EOF
)
rem Other commands after successful creation of the directory.
endlocal
By default command extensions are enabled and delayed expansion is disabled. The batch code above explicitly sets up this environment.
默认情况下启用命令扩展并禁用延迟扩展。上面的批处理代码明确地设置了这个环境。
The command MDcreates the complete directory structure to specified directory with enabled command extensions.
命令MD使用启用的命令扩展创建指定目录的完整目录结构。
MDoutputs an error if the directory already exists. This might be useful to inform a user entering the command manually about a possible mistake in entered directory path as it could be that the user wanted to create a new directory and has just by mistake entered the name of an already existing directory.
如果目录已经存在,MD 会输出错误。这可能有助于通知用户手动输入命令时输入的目录路径中可能存在错误,因为这可能是因为用户想要创建新目录并且错误地输入了现有目录的名称。
But for scripted usage of command MDit is often a problem that this command outputs an error message if the directory to create already exists. It would be really helpful if command MDwould have an option to not output an error message in case of directory to create already existing and exiting with return code 0 in this case. But there is no such option.
但是对于命令MD 的脚本使用,如果要创建的目录已经存在,则该命令输出错误消息通常是一个问题。如果命令MD可以选择在创建目录时不输出错误消息并在这种情况下以返回代码 0 退出,那将非常有帮助。但是没有这样的选择。
The solution above creates the directory and suppress a perhaps output error message with redirecting it from handle STDERRto device NUL.
上面的解决方案创建目录并通过将其从句柄STDERR重定向到设备NUL来抑制可能的输出错误消息。
But the creation of the directory could fail because of invalid character in directory path, drive not available (on using full path), there is anywhere in path a file with name of a specified directory, NTFS?permissions don't permit creation of the directory, etc.
但是由于目录路径中的字符无效,驱动器不可用(使用完整路径时),路径中的任何位置都存在具有指定目录名称的文件,NTFS?权限不允许创建目录,因此目录的创建可能会失败目录等
So it is advisable to verify if the directory really exists which is done with:
因此,建议验证目录是否真的存在,这是通过以下方式完成的:
if not exist "%Directory%\*"
It is important that the directory path ends now with \*
or at least with a backslash. Otherwise it could be possible for the example that there is a file with name subdir 2
in directory mydir\subdir 1
which on usage of the condition if not exist "%Directory%"
would evaluate to false although there is no directory subdir 2
.
目录路径现在以\*
或至少以反斜杠结尾很重要。否则,示例中可能存在subdir 2
目录mydir\subdir 1
中具有名称的文件,if not exist "%Directory%"
尽管没有目录,但在使用条件时会评估为 false subdir 2
。
It is of course also possible to do the directory check first and create the directory if not already existing.
当然也可以先进行目录检查,如果目录不存在则创建该目录。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Directory=mydir\subdir 1\subdir 2"
if not exist "%Directory%\*" (
md "%Directory%"
if errorlevel 1 (
pause
goto :EOF
)
)
rem Other commands after successful creation of the directory.
endlocal
The user can see now the error message output by command MDif the directory structure could not be created explaining briefly the reason.
如果无法创建目录结构,用户现在可以看到命令MD输出的错误消息,并简要说明原因。
This batch code could be written more compact using operator ||
:
使用 operator 可以更紧凑地编写此批处理代码||
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Directory=mydir\subdir 1\subdir 2"
if not exist "%Directory%\*" md "%Directory%" || pause & goto :EOF
rem Other commands after successful creation of the directory.
endlocal
For details about the operators ||
and &
read the answer on Single line with multiple commands using Windows batch file.
有关运算符的详细信息||
并&
阅读使用 Windows 批处理文件的单行多命令的答案。
The command ENDLOCALis not used before goto :EOFbecause this command requires also enabled command extensions. Windows command interpreter executes this command implicit on?leaving execution of the batch file.
在goto :EOF之前不使用命令ENDLOCAL,因为此命令还需要启用命令扩展。Windows 命令解释器在不执行批处理文件时隐式执行此命令。
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
echo /?
endlocal /?
goto /?
if /?
md /?
pause /?
set /?
setlocal /?
echo /?
endlocal /?
goto /?
if /?
md /?
pause /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators.
另请阅读有关使用命令重定向运算符的 Microsoft 文章。