Package "Windows 7" Script "For a command error / error

It seems that there is an error in the command line of the Windows 7 batch file. This command can go through the source directory and at the same time return one file name. But I found that if my command modifies the files in this source directory, for example.

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 

the 'for' command can invoke the do command with the same name more than 1 time. (Note that to illustrate the problem, "echo es> tmp_x2932.tmp" is simply a replacement for some other legit command, such as "sed", which edits the original source file.)

For example, a directory with 9 files

 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 

will produce this result (testdir.bat is the name of the batch file):

 d:\test>testdir D:\build-release\dump d:\test>for /RD:\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. 

the file D: \ build-release \ dump \ f4 is called twice erroneously.

This behavior is not observed in Windows XP. Is there a way to fix it in Windows 7 without changing old scripts? I know that I can always use a temporary directory to store all intermediate files instead of modifying them, but my old scripts in Windows XP just do this.

+4
source share
1 answer

So far, I can only suggest replacing the FOR /R loop with FOR /F , which uses the DIR /S output:

 FOR /F "delims=" %%s IN ('DIR %1 /S /B') DO CALL :do1file %%s … 
+4
source

Source: https://habr.com/ru/post/1384458/


All Articles