Windows 7 批处理脚本“为"命令错误/错误 [英] Windows 7 Batch Script 'For' Command Error/Bug

查看:34
本文介绍了Windows 7 批处理脚本“为"命令错误/错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 7 批处理文件for"命令似乎存在错误.此命令可以遍历源目录并一次返回一个文件名.但我发现如果我的命令修改了该源目录中的文件,例如

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

'for' 命令可以多次调用具有相同文件名的 do 命令.(请注意,为了说明问题,echo 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 个文件的目录

For example, a directory with 9 files

D:uild-releasedump>dir /on
 Volume in drive D has no label.
 Volume Serial Number is 1972-268D

 Directory of D:uild-releasedump

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:	est>testdir D:uild-releasedump
d:	est>for /R D:uild-releasedump %s in (*.*) do call :do1file %s
d:	est>call :do1file D:uild-releasedumpf4
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf4
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf5
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf5
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf6
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf6
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf7
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf7
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf8
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf8
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf9
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf9
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf1
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf1
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf2
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf2
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf3
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf3
        1 file(s) moved.
d:	est>call :do1file D:uild-releasedumpf4
d:	est>move /y tmp_x2932.tmp  D:uild-releasedumpf4
        1 file(s) moved.

文件 D:uild-releasedumpf4 被错误调用两次.

file D:uild-releasedumpf4 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆