String 带多个命令的循环批处理

String 带多个命令的循环批处理,string,batch-file,for-loop,String,Batch File,For Loop,你知道为什么这行不通吗?我正在尝试创建一个文件,扫描当前目录中的所有批处理文件以查找特定字符串(123456),如果找到,则继续:end。如果未找到,文件应将自身复制到扫描的文件中,然后继续扫描下一个文件。任何想法和提示都将不胜感激!干杯 for %%f in (*.bat) do ( set A=%%f set file=%A% findstr "123456" %file% if %errorlevel%==0 goto

你知道为什么这行不通吗?我正在尝试创建一个文件,扫描当前目录中的所有批处理文件以查找特定字符串(123456),如果找到,则继续:end。如果未找到,文件应将自身复制到扫描的文件中,然后继续扫描下一个文件。任何想法和提示都将不胜感激!干杯

    for %%f in (*.bat) do (
        set A=%%f
        set file=%A%
        findstr "123456" %file%
        if %errorlevel%==0 goto end
        copy %0 %A%
        )
    :end
我测试了以下代码:

    SETLOCAL EnableExtensions EnableDelayedExpansion
    for %%f in (*.bat) do (
         set A=%%~f
         set file=%A%
        findstr "123456" %file%
        if %errorlevel%==0 goto end
        copy %0 %A%
        )
    :end
代码没有执行goto end命令。输出如下所示:

    C:\Users\Epidex98\Desktop\routine>(
    set A=ir.bat
     set file=
      findstr "123456"
     if 0 == 0 goto end
     copy "C:\Users\Epidex98\Desktop\routine\ir.bat"
    )
或者更简单

SETLOCAL EnableExtensions EnableDelayedExpansion
for %%f in (*.bat) do (
    findstr "123456" "%%~f"
    if !errorlevel!==0 goto :end
    copy %0 "%%~f"
    )
:end
但是,如果由于某种原因无法应用延迟扩展:

SETLOCAL EnableExtensions DisableDelayedExpansion
for %%f in (*.bat) do (
    set A=%%f
    call :proc
    if errorlevel 0 goto :end
    copy %0 %%f
    )
rem next command skips :proc subroutine 
goto :end

:proc
  set file=%A%
  findstr "123456" %file%
  set /A myerror=%errorlevel%-1
exit /B %myerror%

:end 
资源(必读):

  • (命令参考)
  • (有用的特殊性)
  • %~f
    等特殊页面)
  • (专页)

请记住在一个文件夹中测试这一点,该文件夹只保存您需要的其他脚本的副本。
SETLOCAL EnableExtensions DisableDelayedExpansion
for %%f in (*.bat) do (
    set A=%%f
    call :proc
    if errorlevel 0 goto :end
    copy %0 %%f
    )
rem next command skips :proc subroutine 
goto :end

:proc
  set file=%A%
  findstr "123456" %file%
  set /A myerror=%errorlevel%-1
exit /B %myerror%

:end