Windows 7的批处理脚本'为'命令错误/缺陷 [英] Windows 7 Batch Script 'For' Command Error/Bug
问题描述
有似乎是'为'命令的Windows 7的批处理文件中的错误。该命令可以通过源目录走路,在同一时间返回1名。但我发现,如果我的命令来修改该源目录例如文件。
There seems to be a bug with the Windows 7 batch file 'for' command. This command can walk through a source directory and return one filename at a time. But I found that if my command modify the files in that source directory e.g.
for /R %1 %%s in (*.*) do call :do1file %%s
@goto :EOF
:do1file
@echo es > tmp_x2932.tmp
move /y tmp_x2932.tmp %1
@goto :EOF
在'为'命令可以使用相同的文件名超过1倍以上调用做命令。 (需要注意的是,为了说明这个问题,目的的回响ES> tmp_x2932.tmp'只是一个简单的替换其他一些合法的命令,如sed的,即编辑原始的源文件。)
the 'for' command could invoke the do command with the same file name more than 1 times. (Note that, for the purpose of illustrating the issue, the 'echo es > tmp_x2932.tmp' is just a simple replacement of some other legitimate command, like 'sed', that edit the original source file.)
例如,9个文件的目录
D:\build-release\dump>dir /on
Volume in drive D has no label.
Volume Serial Number is 1972-268D
Directory of D:\build-release\dump
12/03/2011 05:13 PM <DIR> .
12/03/2011 05:13 PM <DIR> ..
12/03/2011 05:40 PM 5 f1
12/03/2011 05:40 PM 5 f2
12/03/2011 05:40 PM 5 f3
12/03/2011 05:40 PM 5 f4
12/03/2011 05:40 PM 5 f5
12/03/2011 05:40 PM 5 f6
12/03/2011 05:40 PM 5 f7
12/03/2011 05:40 PM 5 f8
12/03/2011 05:40 PM 5 f9
9 File(s) 45 bytes
2 Dir(s) 31,200,313,344 bytes free
会产生这样的结果(testdir.bat使用批处理文件名):
will produce this result (testdir.bat is the batch file name used):
d:\test>testdir D:\build-release\dump
d:\test>for /R D:\build-release\dump %s in (*.*) do call :do1file %s
d:\test>call :do1file D:\build-release\dump\f4
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f4
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f5
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f5
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f6
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f6
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f7
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f7
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f8
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f8
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f9
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f9
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f1
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f1
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f2
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f2
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f3
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f3
1 file(s) moved.
d:\test>call :do1file D:\build-release\dump\f4
d:\test>move /y tmp_x2932.tmp D:\build-release\dump\f4
1 file(s) moved.
文件D:\\ build中释放\\转储\\ F4被称为错误地两次
file D:\build-release\dump\f4 is called twice erroneously.
此行为不是在Windows XP中观察到。有什么办法解决它在Windows 7中不改变老段子?我知道我可以一直使用的临时目录中,而不是存储在地方修改它们的所有中间文件,但我的老段子在Windows XP只是做到这一点。
This behaviour is not observed in Windows XP. Is there any way to fix it in Windows 7 without changing the old scripts? I know I can always use a temporary directory to store all the intermediate files instead of modifying them in place, but my old scripts in Windows XP just do that.
推荐答案
到目前为止,我只能建议更换 FOR / R
循环与 FOR /˚F
使用 DIR / S
输出:
So far I can only suggest replacing the FOR /R
loop with FOR /F
that uses the output of DIR /S
:
FOR /F "delims=" %%s IN ('DIR %1 /S /B') DO CALL :do1file %%s
…
这篇关于Windows 7的批处理脚本'为'命令错误/缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!