Windows 中的 mkdir 单个命令中的多个路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21224917/
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
mkdir in windows multiple path in single command
提问by m.k.frenky
@echo off
set "var=string"
set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%"
set "path_backup=\SGSINWPDFS01v\SG\OTHERS\IT\OTHERS\WORKSTATIONS\SCHEDULE"
set "path_sourcepst01=AppData\Local\Microsoft\Outlook"
set "path_sourcepst02=Desktop"
set "path_sourcepst03=My Documents\PST"
set "path_sourcepst04=My Documents\Outlook"
set "path_sourcepst05=My Documents\Outlook Files"
mkdir "%path_backup%\%username%\%today%"
mkdir "%path_backup%\%username%\%today%\PST"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst01%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst02%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst03%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst04%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst05%"
I modified the scripts as follow above. I still the system still can't create the folder at sourcepst01, 03, 04 and 05. The sourcepst02 is working fine.
我修改了上面的脚本。我仍然无法在 sourcepst01、03、04 和 05 处创建文件夹。 sourcepst02 工作正常。
It seem I can't MKDIR whole path, the system confused and must do one by one. Am I missing something here?
看来我不能MKDIR整个路径了,系统糊涂了,必须一一做。我在这里错过了什么吗?
回答by Michael Burr
Add the line:
添加行:
setlocal enableextensions
just after the @echo off
line of the batch file. That will enable mkdir
to create any intermediate directories.
就在@echo off
批处理文件的行之后。这将能够mkdir
创建任何中间目录。
An excerpt of output from help mkdir
:
输出的摘录help mkdir
:
If Command Extensions are enabled MKDIR changes as follows:
MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:
mkdir \a\b\c\d
is the same as:
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
which is what you would have to type if extensions were disabled.
回答by John Deters
Put quote marks around the paths.
在路径周围加上引号。
mkdir "%foo%\%bar%\%somename%"
The problem is that the names have spaces in them, and mkdir is interpreting them as two arguments instead of a single name. The quote marks will force it to interpret everything as a single path.
问题是名称中有空格,并且 mkdir 将它们解释为两个参数而不是单个名称。引号将强制它将所有内容解释为单个路径。
回答by Magoo
John Deters has nailed the problem - but I'd suggest
John Deters 已经解决了这个问题——但我建议
set "path_sourcepst4=My Documents\Outlook"
mkdir "%path_backup%\%username%\PST-%date:~10,4%-%date:~7,2%-%date:~4,2%\%path_sourcepst4%"
Using the set "var=string"
format will ensure that the value set into the variable does not include any stray (and largely invisible) trailing spaces on the line. You only need to be caught out by that one once... It also reduces the number of "being resolved.
使用该set "var=string"
格式将确保设置到变量中的值不包括行上的任何杂散(并且基本上不可见)的尾随空格。只需要被那个人抓到一次……也减少了"被解决的次数。
And why not set a variable called say yyyymmdd
to %date:~10,4%-%date:~7,2%-%date:~4,2%
so that thatstring isn't repeated?
为什么不设置一个名为 say 的变量yyyymmdd
,%date:~10,4%-%date:~7,2%-%date:~4,2%
以便该字符串不重复?
回答by m.k.frenky
@echo off
set "var=string"
set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%"
set "path_backup=\SGSINWPDFS01v\SG\OTHERS\IT\OTHERS\WORKSTATIONS\SCHEDULE"
set "path_sourcepst01=AppData\Local\Microsoft\Outlook"
set "path_sourcepst02=Desktop"
set "path_sourcepst03=My Documents\PST"
set "path_sourcepst04=My Documents\Outlook"
set "path_sourcepst05=My Documents\Outlook Files"
subst b: "%path_backup%"
mkdir "%path_backup%\%username%\%today%"
mkdir "%path_backup%\%username%\%today%\PST"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst01%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst02%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst03%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst04%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst05%"
subst b: /D
I found the issue, it seem on network drive can't be created multiple level subfolders. So map to local drive solve the issue.
我发现了这个问题,在网络驱动器上似乎无法创建多级子文件夹。所以映射到本地驱动器解决了这个问题。
回答by foxidrive
The problem may be due to the nested level of folder depth.
问题可能是由于文件夹深度的嵌套级别。
I believe that 8 levels of directory was the supported maximum, with a total length of 260 characters being another limit.
我相信 8 级目录是支持的最大值,总长度为 260 个字符是另一个限制。