使用“call”函数调用时将变量传递给批处理文件

时间:2021-04-13 23:32:14

i want to call a batch file from another batch file and also want to pass a variable to it. lets say i want to call b.bat from a.bat. my b.bat copies files. so while calling it from a.bat , i want to pass the path of the destination folder to b.bat.
well to b more clear, the destination path will b entered by user, so it will b stored in a variable say 'x'. how do i pass the path now?

我想从另一个批处理文件调用批处理文件,并且还想将变量传递给它。让我说我想从a.bat调用b.bat。我的b.bat复制文件。所以当从a.bat调用它时,我想将目标文件夹的路径传递给b.bat。更清楚的是,目标路径将由用户输入,因此它将存储在变量中,表示为'x'。我现在如何通过这条路?

1 个解决方案

#1


0  

rem --- a.bat ---
set /p TargetPath=Please enter destination path: 
call b.bat "%TargetPath%"

rem --- b.bat ---
echo Copying to: %~1

As an alternative, you can simply use %TargetPath% in b.bat: environment variables are inherited by subprocesses. But passing a parameter explicitly is probably better from the flexibility and supportability point of view.

作为替代方案,您只需在b.bat中使用%TargetPath%:环境变量由子进程继承。但是从灵活性和可支持性的角度来看,明确地传递参数可能更好。

#1


0  

rem --- a.bat ---
set /p TargetPath=Please enter destination path: 
call b.bat "%TargetPath%"

rem --- b.bat ---
echo Copying to: %~1

As an alternative, you can simply use %TargetPath% in b.bat: environment variables are inherited by subprocesses. But passing a parameter explicitly is probably better from the flexibility and supportability point of view.

作为替代方案,您只需在b.bat中使用%TargetPath%:环境变量由子进程继承。但是从灵活性和可支持性的角度来看,明确地传递参数可能更好。