如果另一个已存在,则创建文件夹的批处理文

时间:2014-07-22 00:38:17

标签: batch-file

如果父位置中存在folder1,则尝试创建folder2 ..似乎无法使其工作。尝试使用powershell,但由于某些原因它不会从服务器运行,因此我试图完成批处理。

for /f "delims=|" %%s in ('dir /ad/b') 
(
if exists "%%s\Folder1" do md "%%s\Folder2"
)

1 个答案:

答案 0 :(得分:1)

for /f "delims=|" %%s in ('dir /ad/b') DO (
 if exist "%%s\Folder1" md "%%s\Folder2"
)

注意:缺少DO;开括号必须与DO在同一物理线上; EXIST,而不是EXISTSDO错误地将IF错误地包含在@ECHO Off SETLOCAL PUSHD U:\sourcedir ECHO ======before...VVV DIR /ad U:\sourcedir\err12 :: Ensure folder1 is missing... RD err12\Folder1 2>nul for /f "delims=|" %%s in ('dir /ad/b') DO ( if exist "%%s\Folder1" md "%%s\Folder2" ) ECHO ======after...VVV (no folder1 so folder2 NOT created) DIR /ad U:\sourcedir\err12 :: Make folder1 MD err12\Folder1 2>nul for /f "delims=|" %%s in ('dir /ad/b') DO ( if exist "%%s\Folder1" md "%%s\Folder2" ) ECHO ======after...VVV (folder1 so folder2 IS created) DIR /ad U:\sourcedir\err12 :: Remove both folder1 and folder2 RD err12\Folder1 2>nul RD err12\Folder2 2>nul for /f "delims=|" %%s in ('dir /ad/b') DO ( if exist "%%s\Folder1" ECHO(md "%%s\Folder2" ) ECHO ======after...VVV (no folder1 so folder2 NOT created AGAIN) DIR /ad U:\sourcedir\err12 popd GOTO :EOF 声明

----------注意修改上面的文字! ^^^^^^^^

演示批次:

U:\sourcedir

注意:我使用U:\sourcedir\err12进行测试。 ======before...VVV Volume in drive U has no label. Volume Serial Number is 0460-0000 Directory of U:\sourcedir\err12 22/07/2014 10:11 <DIR> . 22/07/2014 10:11 <DIR> .. 0 File(s) 0 bytes 2 Dir(s) 2,126,249,984 bytes free ======after...VVV (no folder1 so folder2 NOT created) Volume in drive U has no label. Volume Serial Number is 0460-0000 Directory of U:\sourcedir\err12 22/07/2014 10:11 <DIR> . 22/07/2014 10:11 <DIR> .. 0 File(s) 0 bytes 2 Dir(s) 2,126,249,984 bytes free ======after...VVV (folder1 so folder2 IS created) Volume in drive U has no label. Volume Serial Number is 0460-0000 Directory of U:\sourcedir\err12 22/07/2014 10:11 <DIR> . 22/07/2014 10:11 <DIR> .. 22/07/2014 11:33 <DIR> Folder1 22/07/2014 11:33 <DIR> Folder2 0 File(s) 0 bytes 4 Dir(s) 2,126,184,448 bytes free ======after...VVV (no folder1 so folder2 NOT created AGAIN) Volume in drive U has no label. Volume Serial Number is 0460-0000 Directory of U:\sourcedir\err12 22/07/2014 10:11 <DIR> . 22/07/2014 10:11 <DIR> .. 0 File(s) 0 bytes 2 Dir(s) 2,126,249,984 bytes free 已经存在。

结果(插入一点空格)

do

所以 - 对我有用 - 在if语句中的do is not recognised ...被移除后,但会产生%%s错误,而不是{{1}},因为您已经报告