我编写了以下.bat文件,它在我的Windows2000机器上运行得很好,但不会在我的Windows7或Windows机器上运行。基本上,它只是遍历当前目录并运行一个校验和程序,该程序返回校验和。将程序的输出保存到文本文件中,然后格式化以删除输出文件的校验和。
@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运行这个文件时,它会输出这个文件。在其他机器上,它输出一个空白文件。我打开Echo,只保留for循环行,发现执行crc32sum.exe的输出是空的。如果手动运行crc32sum.exe文件,它将输出校验和,没有问题。
有什么办法解决这个问题吗?
编辑:这是到软件的链接:http://www.di-mgt.com.au/src/digsum-1.0.1.zip
EDIT2:新开发,如果文件夹的路径中没有空格,比如C:\temp或C:\inetpub\ftproot或C:\user\admin\Desktop\temp,那么文件就能工作。有人知道我如何在有空格的路径下工作吗?%%~f不起作用,它说出乎意料。
发布于 2015-12-09 14:21:08
试试在Windows 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 http://stackoverflow.com/a/33403497/3074564
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 递归选项及其所有子目录中扩展名为txt的文件。对于当前目录或任何子目录中的任何文本文件,始终不带双引号的完整路径的每个找到的文件的名称都保存在循环变量F中。
CRC32和由当前目录中的32位控制台应用程序crc32sum计算,除当前目录中的输出文件all_checksums.txt外,所有文本文件均为crc32sum。这个小应用程序的输出被重定向到文件all_checksums.txt中,并在该文件中附加一个输出行。
必须用双引号将文件名用路径括起来,因为即使没有包含空格字符的*.txt文件或其名称中的一个特殊字符&()[]{}^=;!'+,`~,文件的路径也可能包含一个空格或其中一个字符。
为了文件
C:\Temp\test 1.txt
C:\Temp\test 2.txt
C:\Temp\test_3.txt
C:\Temp\TEST\123-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.txtC:\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要了解所使用的命令及其工作方式,请打开命令提示符窗口,在那里执行以下命令,并非常仔细地读取为每个命令显示的所有帮助页。
cls /?del /?echo /?for /?goto /?if /?rem /?set /?type /?运行for /?时的帮助页输出通知%~I、%~fI、%~dI、%~pI、%~nI、%~xI、%~sI、%~aI、%~tI、%~zI。
在批处理文件中,使用f (小写)作为循环变量并使用%%~f引用它是语法错误,因为命令处理器期望下一个循环变量。%%~ff是正确的,但可能与%%~fI (具有完整路径和扩展名而没有引号的文件/文件夹的名称)不同,与%%~I (没有周围引号的字符串)不同。
使用(那些)小写字母作为循环变量是不可取的。最好使用大写字母或字符#作为循环变量。循环变量和这些修饰符是区分大小写的,而批处理文件中的几乎所有内容都是不区分大小写的。
https://stackoverflow.com/questions/34162748
复制相似问题