繁体   English   中英

是否可以像在 unix 中一样在 windows cmd 中获取批处理文件?

[英]Is it possible to source a batch file in windows cmd like you can in unix?

我是一个 unix 人,但我必须在 Windows 中编写一个系统,我正在尝试编写一个脚本来移动一些文件。 我正在尝试让父批处理文件调用包含以下内容的子批处理文件:

set REPORTFILE=c:\report.txt

然后我希望父级能够使用 %REPORTFILE% 变量。 显然 CALL 命令创建了一个新的上下文。 在 unix 中,您只需获取脚本的源代码,这在 Windows 中可行吗?

如果我明白……这在 Vista 中似乎对我有用:

调用者.bat

echo this is the caller
echo initial value is: %reportfile%
call setter.bat
echo value is: %reportfile%

二传手.bat

echo this is the value setter
set reportfile=c:\report.txt

C:\\temp>来电者

C:\\temp>echo 这是调用者
this is the caller
C:\\temp>echo 初始值为:
initial value is:
C:\\temp> 调用 setter.bat

C:\\temp>echo 这是值设置器
this is the value setter
C:\\temp>set reportfile=c:\\report.txt

C:\\temp>echo 值是:c:\\report.txt
value is: c:\\report.txt

更新为使用 goto 而不是 parens:

if not exist file.txt goto doit
goto notfound
:doit 
echo this is the caller 
echo initial value is: %reportfile% 
call setter.bat
echo value is: %reportfile%
goto end
:notfound
 echo file found 
:end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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