1

Any idea why this wouldn't work? I'm trying to create a file scanning all batch files in it's current directory for a specific string (123456) and if found proceed to :end. If not found, the file should copy itself into the scanned file and proceed scanning the next file. Any ideas and tips are appreciated! Cheers!

    for %%f in (*.bat) do (
        set A=%%f
        set file=%A%
        findstr "123456" %file%
        if %errorlevel%==0 goto end
        copy %0 %A%
        )
    :end

I tested the following code:

    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

and the code didnt execute the goto end command. The output looks like this:

    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"
    )
1
  • 1
    Remember to test this in a folder that holds the only copies of the other scripts you need. Commented Mar 29, 2016 at 22:48

1 Answer 1

3
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

or simpler

SETLOCAL EnableExtensions EnableDelayedExpansion
for %%f in (*.bat) do (
    findstr "123456" "%%~f"
    if !errorlevel!==0 goto :end
    copy %0 "%%~f"
    )
:end

However, if you can't apply delayed expansion for some reason:

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 

Resources (required reading):

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.