如何直接从cmd运行/调用批处理文件中的一个函数? 我有一个包含一些功能的test.bat文件,我想在不运行整个文件的情况下调用/运行其中一个功能。有可能吗?
@echo off
:plus
set /p input1=first input:
set /p input2=second input:
set /a n1=input1
set /a n2=input2
set /a ans=%n1%+%n2%
echo %input1%+%input2% = %ans%
REM goto :eof
:sub
set /p input1=first input:
set /p input2=second input:
set /a n1=input1
set /a n2=input2
set /a ans=%n1%-%n2%
echo %input1% - %input2% = %ans%
REM goto :eof
:mul
set /p input1=first input:
set /p input2=second input:
set /a n1=input1
set /a n2=input2
set /a ans=%n1%*%n2%
echo %input1% * %input2% = %ans%
goto :eof
答案 0 :(得分:0)
我猜你想要这样的东西吗?
(请注意,如果您仍想访问标签,可以将clsWorkbook
更改为call :%~1
,将其替换为goto :%~1
goto :eof
要使用它,请打开@echo off
:: This portion will use the paramter sent from cmd window.
call :%~1
goto :eof
:plus
echo Welcome to additions (n+n=n)
set /p input1=first input:
set /p input2=second input:
set /a n1=input1
set /a n2=input2
set /a ans=%n1%+%n2%
echo %input1%+%input2% = %ans%
goto :eof
:sub
echo Welcome to subtractions (n-n=n)
set /p input1=first input:
set /p input2=second input:
set /a n1=input1
set /a n2=input2
set /a ans=%n1%-%n2%
echo %input1% - %input2% = %ans%
goto :eof
:mul
echo Welcome to muptiplications (n*n=n)
set /p input1=first input:
set /p input2=second input:
set /a n1=input1
set /a n2=input2
set /a ans=%n1%*%n2%
echo %input1% * %input2% = %ans%
:eof
pause
并键入(假设您的文件名为cmd
:
mybat.cmd
随后将批量调用标签mybat.cmd mul
我们使用:mul
而不是%~1
的事实是它也可以运行为:
%1
结果完全相同。
这是我测试该脚本时的屏幕截图。
作为旁注,您还可以使用mybat.cmd "mul"
来提示用户要运行的内容,然后相应地调用标签。然后,您只需要运行批处理文件,而不必每次都从cmd运行它。
答案 1 :(得分:0)
按照格哈德提到的最一般的方式,我个人将其简化如下:
@echo off
if "%1" == "" (echo The file was either run regardless or from cmd without arguments. & exit /b 1)
:: This portion will use the paramter sent from cmd window.
call :%~1
goto :END
:plus
echo Welcome to additions (n+n=n)
set /p "input1=first input: "
set /p "input2=second input: "
set /a "ans=input1+input2"
echo %input1% + %input2% = %ans%
:sub
echo Welcome to subtractions (n-n=n)
set /p "input1=first input: "
set /p "input2=second input: "
set /a "ans=input1-input2""
echo %input1% - %input2% = %ans%
:mul
echo Welcome to muptiplications (n*n=n)
set /p "input1=first input: "
set /p "input2=second input: "
set /a "ans=input1*input2"
echo %input1% * %input2% = %ans%
:END
pause
exit /b 0
我认为这是很多分类器,还有另一种方法可以做到:
additions.bat
的批处理文件,其内容如下:@call yourfile.bat "plus"
subtractions.bat
的批处理文件,其内容如下:@call yourfile.bat "sub"
muptiplications.bat
的批处理文件,其内容如下:
@call yourfile.bat "mul"
我认为,只需双击正确的批处理文件,就会容易得多。