简体   繁体   中英

Calling batch script from another batch script

Ok I have found a few questions on this, but each are saying make sure the use CALL and exit \\b or goto eof in the 2nd bat file but for some reason I am not getting this to work, I have tried both, the batch file exits every time after executing the first call statement:

batch file 1 (myscript.bat):

:@echo off
del files
dir /B /O-D | find "test2" > tmp
dir /B /O-D | find "test3" > tmp2
CALL head 1 tmp > files
CALL head 1 tmp2 >> files

head.bat:

@echo off

if [%1] == [] goto usage
if [%2] == [] goto usage

call :print_head %1 %2
goto :eof

REM
REM print_head
REM Prints the first non-blank %1 lines in the file %2.
REM
:print_head
setlocal EnableDelayedExpansion
set /a counter=0

for /f ^"usebackq^ eol^=^

^ delims^=^" %%a in (%2) do (
        if "!counter!"=="%1" goto :eof
        echo %%a
        set /a counter+=1
)

goto :eof

:usage
echo Usage: head.bat COUNT FILENAME

Execution:

C:\\Users\\ots>myscript.bat

C:\\Users\\ots>del files

C:\\Users\\ots>dir /B /OD | find "test2" 1>tmp

C:\\Users\\ots>dir /B /OD | find "test3" 1>tmp2

C:\\Users\\ots>CALL head 1 tmp 1>files

C:\\Users\\ots>

How can I get it to run the second "tmp2" Call line?

Thanks!

Your code is fine, both calls are indeed made.

The issue is that you are setting echo to OFF in head.bat, so after the first call, your command does not get echoed on the console, but that does not mean the file is not called.

To verify this, remove the @echo off from head.bat, and you will see your second CALL command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM