Search code examples
windowsbatch-filecall

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?


Solution

  • 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