循环的批处理文件只在一台机器上执行
Batch file for loop executes on one machine only
我已经编写了以下 .bat 文件,它 运行 在我的 Windows 2000 机器上非常完美,但不会 运行 在我的 Windows 7 或者Windows XP 机器。基本上它只是循环遍历当前目录和 运行 一个校验和程序,其中 returns 校验和。程序的输出被保存到一个文本文件,然后格式化以去除输出文件的校验和。
@Echo Off
for /r %%f in (*.txt) do crc32sum.exe %%f >> all_checksums.txt
ren all_checksums.txt old.txt
findstr /v /e /c:"all_checksums.txt" old.txt > all_checksums.txt
del old.txt
当我 运行 我的 Win2k PC 上的这个文件有一堆文本文件和文件夹中的 crc32sum.exe 时,它输出文件。在其他机器上它输出一个空白文件。我打开 Echo 并只保留 for 循环行,发现执行 crc32sum.exe 的输出什么也没有。如果您手动 运行 crc32sum.exe 文件,它会输出校验和没问题。
关于如何解决这个问题有什么想法吗?
编辑:这是软件的 link:http://www.di-mgt.com.au/src/digsum-1.0.1.zip
EDIT2:新开发,如果文件夹路径中没有空格,即 C:\temp 或 C:\inetpub\ftproot 或 C:\users\admin\Desktop\temp,该文件似乎有效。有谁知道我如何使用有空格的路径进行这项工作? %%~f 不起作用它说意外。
试试这个在 Windows XP SP3 x86:
上有效的修改批处理代码
@echo off
goto CheckOutput
rem Command DEL does not terminate with an exit code greater 0
rem if the deletion of a file failed. Therefore the output to
rem stderr must be evaluated to find out if deletion was
rem successful or (for a single file) the file existence is
rem checked once again. For details read on Stack Overflow
rem the answer
rem The deletion of the file was successful if file created
rem from output message has size 0 and therefore the temp
rem file can be deleted and calculation of the CRC32 sums
rem can be started.
:DeleteOutput
del /F "all_checksums.txt" >nul 2>"%TEMP%\DelErrorMessage.tmp"
for %%E in ("%TEMP%\DelErrorMessage.tmp") do set "FileSize=%%~zE"
if "%FileSize%" == "0" (
set "FileSize="
del "%TEMP%\DelErrorMessage.tmp"
goto CalcCRC32
)
set "FileSize="
echo %~nx0: Failed to delete file %CD%\all_checksums.txt
echo.
type "%TEMP%\DelErrorMessage.tmp"
del "%TEMP%\DelErrorMessage.tmp"
echo.
echo Is this file opened in an application?
echo.
set "Retry=N"
set /P "Retry=Retry (N/Y)? "
if /I "%Retry%" == "Y" (
set "Retry="
cls
goto CheckOutput
)
set "Retry="
goto :EOF
:CheckOutput
if exist "all_checksums.txt" goto DeleteOutput
:CalcCRC32
for /R %%F in (*.txt) do (
if /I not "%%F" == "%CD%\all_checksums.txt" (
crc32sum.exe "%%F" >>"all_checksums.txt"
)
)
如果前一个 运行 中已经存在当前目录中的输出文件,则删除该输出文件。添加了额外的代码来验证删除是否成功,并通知用户删除失败,如果这是删除失败的原因,则允许用户在应用程序中关闭文件后重试。
FOR 命令因为选项 /R
recursive 在当前目录及其所有子目录中搜索扩展名为 txt
。对于在当前目录或任何子目录中找到的任何文本文件,每个找到的具有完整路径且始终不带双引号的文件的名称保存在循环变量 F
中。
CRC32 和由 32 位控制台应用程序 crc32sum
在当前目录中为当前目录中找到的所有文本文件计算,输出文件 all_checksums.txt
除外。这个小应用程序的输出被重定向到文件 all_checksums.txt
,并将单个输出行附加到该文件。
有必要用双引号将文件名和路径括起来,因为即使没有包含 space 字符或名称中的特殊字符 &()[]{}^=;!'+,`~
之一的 *.txt 文件,文件的路径可以包含 space 或这些字符之一。
对于文件
C:\Temp\test 1.txt
C:\Temp\test 2.txt
C:\Temp\test_3.txt
C:\Temp\TEST3-9.txt
C:\Temp\TEST\abc.txt
C:\Temp\TEST\hello.txt
C:\Temp\TEST\hellon.txt
C:\Temp\Test x\test4.txt
C:\Temp\Test x\test5.txt
批量执行后文件C:\Temp\all_checksums.txt
包含:
f44271ac *test 1.txt
624cbdf9 *test 2.txt
7ce469eb *test_3.txt
cbf43926 *123-9.txt
352441c2 *abc.txt
0d4a1185 *hello.txt
38e6c41a *hellon.txt
1b4289fa *test4.txt
f44271ac *test5.txt
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
cls /?
del /?
echo /?
for /?
goto /?
if /?
rem /?
set /?
type /?
在 运行ning for /?
上输出的帮助页面之一通知了 %~I
、%~fI
、%~dI
、%~pI
、%~nI
, %~xI
, %~sI
, %~aI
, %~tI
, %~zI
.
在批处理文件中使用 f
(小写)作为循环变量并用 %%~f
引用它是一个语法错误,因为命令处理器期望下一个循环变量。 %%~ff
是正确的,但与 %%~I
(不带引号的字符串)相比,%%~fI
(具有完整路径和扩展名且不带引号的 file/folder 的名称)可能有所不同.
不建议使用(那些)小写字母作为循环变量。最好使用大写字母或字符 #
作为循环变量。循环变量和那些修饰符区分大小写,而批处理文件中的几乎所有其他内容都不区分大小写。
我已经编写了以下 .bat 文件,它 运行 在我的 Windows 2000 机器上非常完美,但不会 运行 在我的 Windows 7 或者Windows XP 机器。基本上它只是循环遍历当前目录和 运行 一个校验和程序,其中 returns 校验和。程序的输出被保存到一个文本文件,然后格式化以去除输出文件的校验和。
@Echo Off
for /r %%f in (*.txt) do crc32sum.exe %%f >> all_checksums.txt
ren all_checksums.txt old.txt
findstr /v /e /c:"all_checksums.txt" old.txt > all_checksums.txt
del old.txt
当我 运行 我的 Win2k PC 上的这个文件有一堆文本文件和文件夹中的 crc32sum.exe 时,它输出文件。在其他机器上它输出一个空白文件。我打开 Echo 并只保留 for 循环行,发现执行 crc32sum.exe 的输出什么也没有。如果您手动 运行 crc32sum.exe 文件,它会输出校验和没问题。
关于如何解决这个问题有什么想法吗?
编辑:这是软件的 link:http://www.di-mgt.com.au/src/digsum-1.0.1.zip
EDIT2:新开发,如果文件夹路径中没有空格,即 C:\temp 或 C:\inetpub\ftproot 或 C:\users\admin\Desktop\temp,该文件似乎有效。有谁知道我如何使用有空格的路径进行这项工作? %%~f 不起作用它说意外。
试试这个在 Windows XP SP3 x86:
上有效的修改批处理代码@echo off
goto CheckOutput
rem Command DEL does not terminate with an exit code greater 0
rem if the deletion of a file failed. Therefore the output to
rem stderr must be evaluated to find out if deletion was
rem successful or (for a single file) the file existence is
rem checked once again. For details read on Stack Overflow
rem the answer
rem The deletion of the file was successful if file created
rem from output message has size 0 and therefore the temp
rem file can be deleted and calculation of the CRC32 sums
rem can be started.
:DeleteOutput
del /F "all_checksums.txt" >nul 2>"%TEMP%\DelErrorMessage.tmp"
for %%E in ("%TEMP%\DelErrorMessage.tmp") do set "FileSize=%%~zE"
if "%FileSize%" == "0" (
set "FileSize="
del "%TEMP%\DelErrorMessage.tmp"
goto CalcCRC32
)
set "FileSize="
echo %~nx0: Failed to delete file %CD%\all_checksums.txt
echo.
type "%TEMP%\DelErrorMessage.tmp"
del "%TEMP%\DelErrorMessage.tmp"
echo.
echo Is this file opened in an application?
echo.
set "Retry=N"
set /P "Retry=Retry (N/Y)? "
if /I "%Retry%" == "Y" (
set "Retry="
cls
goto CheckOutput
)
set "Retry="
goto :EOF
:CheckOutput
if exist "all_checksums.txt" goto DeleteOutput
:CalcCRC32
for /R %%F in (*.txt) do (
if /I not "%%F" == "%CD%\all_checksums.txt" (
crc32sum.exe "%%F" >>"all_checksums.txt"
)
)
如果前一个 运行 中已经存在当前目录中的输出文件,则删除该输出文件。添加了额外的代码来验证删除是否成功,并通知用户删除失败,如果这是删除失败的原因,则允许用户在应用程序中关闭文件后重试。
FOR 命令因为选项 /R
recursive 在当前目录及其所有子目录中搜索扩展名为 txt
。对于在当前目录或任何子目录中找到的任何文本文件,每个找到的具有完整路径且始终不带双引号的文件的名称保存在循环变量 F
中。
CRC32 和由 32 位控制台应用程序 crc32sum
在当前目录中为当前目录中找到的所有文本文件计算,输出文件 all_checksums.txt
除外。这个小应用程序的输出被重定向到文件 all_checksums.txt
,并将单个输出行附加到该文件。
有必要用双引号将文件名和路径括起来,因为即使没有包含 space 字符或名称中的特殊字符 &()[]{}^=;!'+,`~
之一的 *.txt 文件,文件的路径可以包含 space 或这些字符之一。
对于文件
C:\Temp\test 1.txt
C:\Temp\test 2.txt
C:\Temp\test_3.txt
C:\Temp\TEST3-9.txt
C:\Temp\TEST\abc.txt
C:\Temp\TEST\hello.txt
C:\Temp\TEST\hellon.txt
C:\Temp\Test x\test4.txt
C:\Temp\Test x\test5.txt
批量执行后文件C:\Temp\all_checksums.txt
包含:
f44271ac *test 1.txt
624cbdf9 *test 2.txt
7ce469eb *test_3.txt
cbf43926 *123-9.txt
352441c2 *abc.txt
0d4a1185 *hello.txt
38e6c41a *hellon.txt
1b4289fa *test4.txt
f44271ac *test5.txt
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
cls /?
del /?
echo /?
for /?
goto /?
if /?
rem /?
set /?
type /?
在 运行ning for /?
上输出的帮助页面之一通知了 %~I
、%~fI
、%~dI
、%~pI
、%~nI
, %~xI
, %~sI
, %~aI
, %~tI
, %~zI
.
在批处理文件中使用 f
(小写)作为循环变量并用 %%~f
引用它是一个语法错误,因为命令处理器期望下一个循环变量。 %%~ff
是正确的,但与 %%~I
(不带引号的字符串)相比,%%~fI
(具有完整路径和扩展名且不带引号的 file/folder 的名称)可能有所不同.
不建议使用(那些)小写字母作为循环变量。最好使用大写字母或字符 #
作为循环变量。循环变量和那些修饰符区分大小写,而批处理文件中的几乎所有其他内容都不区分大小写。