从另一个批处理脚本调用批处理脚本

问题描述:

好吧我已经找到了一些关于此问题的问题,但每个都说确保在第二个bat文件中使用CALLexit \bgoto eof但由于某种原因,我没有得到这个工作,我曾经尝试都,批处理文件执行第一次调用语句后退出每次:从另一个批处理脚本调用批处理脚本

批处理文件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 

执行:

C:\用户\ OTS> myscript.bat

C:\用户\ OTS>德尔文件

C:\用户\ OTS> DIR/B/OD |找到“test2”1> tmp

C:\ Users \ ots> dir/B/O-D |找到 “TEST3” 1> TMP2

C:\用户\ OTS> CALL头1 TMP 1>文件

C:\用户\ OTS>

我怎样才能得到它运行第二个“tmp2”呼叫线?

谢谢!

你的代码没问题,两次调用都确实发生了。

问题是您在head.bat中将echo设置为OFF,所以在第一次调用后,您的命令没有在控制台上得到回显,但这并不意味着该文件未被调用。

要验证这一点,请从head.bat中删除@echo off,然后您将看到第二个CALL命令。

+0

你是对的!谢谢! –