Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 循环的批处理文件仅在一台计算机上执行_Windows_Batch File_Scripting - Fatal编程技术网

Windows 循环的批处理文件仅在一台计算机上执行

Windows 循环的批处理文件仅在一台计算机上执行,windows,batch-file,scripting,Windows,Batch File,Scripting,我已经编写了以下.bat文件,它在我的Windows 2000计算机上运行良好,但不会在我的Windows 7或Windows XP计算机上运行。基本上,它只是在当前目录中循环并运行一个校验和程序,该程序返回校验和。程序的输出保存到文本文件中,然后格式化以删除输出文件的校验和 @Echo Off for /r %%f in (*.txt) do crc32sum.exe %%f >> all_checksums.txt ren all_checksums.txt old.txt

我已经编写了以下.bat文件,它在我的Windows 2000计算机上运行良好,但不会在我的Windows 7或Windows XP计算机上运行。基本上,它只是在当前目录中循环并运行一个校验和程序,该程序返回校验和。程序的输出保存到文本文件中,然后格式化以删除输出文件的校验和

@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文件,则输出校验和没有问题

有没有办法解决这个问题

编辑:以下是该软件的链接:


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 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"
    )
)
如果当前目录中的输出文件已存在于上一次运行中,则会将其删除。添加额外代码以验证删除是否成功,并通知用户删除失败,如果这是删除失败的原因,则允许用户在关闭应用程序中的文件后重试

由于当前目录及其所有子目录中的选项
/R
recursive,FOR命令搜索扩展名为
txt
的文件。对于在当前目录或任何子目录中找到的任何文本文件,具有完整路径且始终不带双引号的每个找到的文件的名称都是hold in loop变量
F

CRC32总和由当前目录中的32位控制台应用程序
crc32sum
为所有文本文件计算,当前目录中的输出文件
all_checksums.txt
除外。这个小应用程序的输出被重定向到文件
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.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
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • cls/?
  • del/?
  • echo/?
  • 获取/?
  • goto/?
  • 如果/?
  • rem/?
  • 设置/?
  • 键入/?
运行
for/?
时输出的一个帮助页面通知有关
%~I
%~fI
%~dI
%~pI
%~nI
%~xI
%sI
%aI
%tI
%zI

在批处理文件中使用
f
(小写)作为循环变量,并使用
%%~f
引用它是一个语法错误,因为命令处理器希望循环变量位于下一个
%%~ff
是正确的,但可能与
%%~fI
(具有完整路径和扩展名且不带引号的文件/文件夹名称)不同于
%%~I
(不带引号的字符串)


不建议使用(那些)小写字母作为循环变量。最好使用大写字母或字符
#
作为循环变量。循环变量和这些修饰符区分大小写,而批处理文件中几乎所有其他内容都不区分大小写。

请尝试此在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 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"
    )
)
如果当前目录中的输出文件已存在于上一次运行中,则会将其删除。添加额外代码以验证删除是否成功,并通知用户删除失败,如果这是删除失败的原因,则允许用户在关闭应用程序中的文件后重试

由于当前目录及其所有子目录中的选项
/R
recursive,FOR命令搜索扩展名为
txt
的文件。对于在当前目录或任何子目录中找到的任何文本文件,具有完整路径且始终不带双引号的每个找到的文件的名称都是hold in loop变量
F

CRC32总和由当前目录中的32位控制台应用程序
crc32sum
为所有文本文件计算,当前目录中的输出文件
all_checksums.txt
除外。这个小应用程序的输出被重定向到文件
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.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
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • cls/?
  • del/?
  • echo/?
  • for/?