Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 从Windows命令中的另一个.bat中调用.bat文件中的函数_Batch File_Cmd - Fatal编程技术网

Batch file 从Windows命令中的另一个.bat中调用.bat文件中的函数

Batch file 从Windows命令中的另一个.bat中调用.bat文件中的函数,batch-file,cmd,Batch File,Cmd,我正在尝试调用一个函数,例如:dosomething,它位于我的1.bat文件中。该函数存在于2.bat文件中。如何在那里调用它?可以在1.bat中调用函数存根,然后在不调用的情况下调用2.bat :: 1.bat @Echo off Echo This is 1.bat Call :dosomething args Echo back in 1.bat pause Exit /b :dosomething Echo Sub %0 in 1.bat 2.bat %* 样本运行: This i

我正在尝试调用一个函数,例如:dosomething,它位于我的1.bat文件中。该函数存在于2.bat文件中。如何在那里调用它?

可以在1.bat中调用函数存根,然后在不调用的情况下调用2.bat

:: 1.bat
@Echo off
Echo This is 1.bat
Call :dosomething args
Echo back in 1.bat
pause
Exit /b
:dosomething
Echo Sub %0 in 1.bat
2.bat %*

样本运行:

This is 1.bat
Sub :dosomething in 1.bat
this is 2.bat args
back in 1.bat
Press any key to continue . . .

可以在1.bat中调用函数存根,然后在不调用的情况下调用2.bat

:: 1.bat
@Echo off
Echo This is 1.bat
Call :dosomething args
Echo back in 1.bat
pause
Exit /b
:dosomething
Echo Sub %0 in 1.bat
2.bat %*

样本运行:

This is 1.bat
Sub :dosomething in 1.bat
this is 2.bat args
back in 1.bat
Press any key to continue . . .

这里的正常方法是,在
1.bat中

call 2.bat something realparameterlist
2.bat的结构如下:

@echo off
shift&goto %1

:something
(whatever)
goto :eof
当然,这可以变得更聪明:

@echo off
set "junkvar=%1"
if "%junkvar:~0,1%"==":" shift&goto %1
:: processing if first arg doesn't begin ':'

:something
(whatever)
goto :eof
其中提供的第一个参数必须以
开头:


应该注意的是,
%*
仍将包含
[:]routinename
,无论采用的是更简单的方法还是更复杂的方法。

这里的正常方法是
1.bat

call 2.bat something realparameterlist
2.bat的结构如下:

@echo off
shift&goto %1

:something
(whatever)
goto :eof
当然,这可以变得更聪明:

@echo off
set "junkvar=%1"
if "%junkvar:~0,1%"==":" shift&goto %1
:: processing if first arg doesn't begin ':'

:something
(whatever)
goto :eof
其中提供的第一个参数必须以
开头:


需要注意的是,
%*
仍将包含
[:]routinename
,无论采用的是更简单的方法还是更复杂的方法。

您必须修改2.bat。有可能吗?是的,完全有可能!我似乎无法找到如何从外部调用特定函数!您必须修改2.bat。有可能吗?是的,完全有可能!我似乎无法找到如何从外部调用特定函数!谢谢这个很好用!谢谢这个很好用!