Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm a unix guy but I have to write a system in windows, and I'm trying to write a script to move some files around. I'm trying to have the parent batch file CALL a child batch file which contains:

set REPORTFILE=c:\report.txt

and then I want the parent to be able to use the %REPORTFILE% variable. Apparently the CALL command creates a new context. In unix, you just source the script, is that possible in windows?

like image 216
stu Avatar asked Oct 10 '12 18:10

stu


People also ask

Can Windows batch file run on Linux?

No. The bat files are windows shell scripts, which probably execute windows commands and expect to run in a windows environment. You need to convert them to shell scripts in order to run them on linux, as your bash shell can not understand dos commands.

What is the equivalent of source in Windows?

Specific to your question "What is the alternative for source command in cmd?" The equivalent command to "source" is "call".. but it is only used from another batch file.

What language do Windows batch files use?

bat file is a DOS/Windows shell script executed by the DOS/Windows command interpreter. When a batch script is saved to a . bat file, it is just called a batch file. The language is simply batch script .


1 Answers

if I understand...this seems to work for me in Vista:

caller.bat

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

setter.bat

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

C:\temp>caller

C:\temp>echo this is the caller
this is the caller
C:\temp>echo initial value is:
initial value is:
C:\temp>call setter.bat

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

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

updated to use goto instead of 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
like image 167
Stanley Avatar answered Sep 18 '22 15:09

Stanley