batch file echo on/off not working properly

前端 未结 1 1521
予麋鹿
予麋鹿 2020-12-30 16:13

I\'m not really used to making batch files, so I\'m probably doing something wrong, but I\'m really having trouble with echo off/on. (That said, if you see any other error

相关标签:
1条回答
  • 2020-12-30 16:35

    I see your problem now - and it is an interesting one. You are calling a batch file from within a batch file without using CALL.

    If batch A executes batch B without CALL, then the entire batch processing is terminated once batch B finishes - Batch A will not resume where it left off. Once batch processing is terminated the ECHO state is returned to the original state that existed before batch processing began, normally ON.

    But your scenario is a bit more complicated because your "call" to the 2nd batch is within a FOR loop. So the FOR loop continues to execute the DO commands that are cached in memory, even though batch processing has terminated. The commands are now executing in a command line context with ECHO ON.

    In your 2nd scenario you explicitly turn ECHO OFF within the FOR DO block. After the 1st "call" you are again in command line context, and then you turn ECHO OFF. Hence your need to turn ECHO ON manually.

    I'm not sure how the lines after the FOR loop are getting executed. You said you have simplified the code, so I am assuming the code you are actually running has those additional lines in some kind of code block as well. The lines would then be buffered and would still execute even after batch processing terminates. This would also explain why the line that prints "Finished!" is not echoed even though ECHO is ON. All commands within a block of code use the ECHO state that existed prior to entering the outer most block of code. The one exception is a FOR DO block uses the ECHO state that existed prior to entering the DO block.

    Both sets of code you posted do not run because each has an unbalanced quote in the line that "calls" the 2nd batch file.

    The fix is to simply add CALL to the statement that executes the 2nd batch file. Then batch processing will continue, the ECHO state will remain OFF, and the parent batch will resume where it left off properly.

    I also improved the argument parsing logic and the use of quotes. You should strip quotes from each argument using ~ before explitly adding your own. The argument may already have its own quotes. And any time a file name or path is passed as an argument, it should be quoted in case it contains space or special characters. You need to keep track of whether the value in a variable is quoted or not. I generally find it easier to keep my variable values unquoted and then explicitly add quotes when I need them.

    @echo off
    setlocal
    set args=
    set "dir=."
    
    :getargs
    IF "%~2"=="" (
        if "%~1" neq "" set "dir=%~1"
        goto callbatch
    )
    set args=%args% %1
    shift
    goto getargs
    
    :callbatch
    for %%f in ("%dir%\*.txt") do (
        echo processing %%f
        call "%BATCHHOME%\batch.bat" %args% "%%f"
        echo
    )
    
    0 讨论(0)
提交回复
热议问题