Page 1 of 1

CALL & Data Flow Between Child and Parent Command Files

Posted: 21 Apr 2011 22:37
by WarGod
I am confused about something regarding the CALL function and how data is transferred between child and parent processes.

I have the following command files:
file_a.cmd

Code: Select all

@echo off
  set file=C:\temp\FilewithText.txt
  CALL file_d.cmd :HEADER
  <command to write data to %file%>
  <do some other commands>
  cmd /c file_b.cmd
  <code to handle errorlevel etc..>
  PAUSE
  EXIT


file_b.cmd

Code: Select all

  @echo off
  CALL file_d.cmd :HEADER
  <do some commands>
  cmd /c file_c.cmd
  <code to handle errorlevel etc..>
  PAUSE
  EXIT


file_c

Code: Select all

@echo off
  CALL file_d.cmd :HEADER
  CALL file_d.cmd :READFILE RESULT %file%
  echo Data string is: %RESULT%
  pause
  EXIT


file_d.cmd

Code: Select all

@echo off
  :HEADER
  cls
  echo.
  echo ------------------------------------------
  echo                      HEADER
  echo ------------------------------------------
  EXIT /b

  :READFILE
  FOR /F "usebackq tokens=* delims=*" %%i in (%2) do set %1=%%i
  EXIT /b



This CALL works exactly like I want it too in file_a/b/c.cmd. The screen is cleared of previous data and a new header is displayed along with any echo statements in the new command file.

Code: Select all

CALL file_d.cmd :HEADER


However, in file_c.cmd, when I execute this command:

Code: Select all

CALL file_d.cmd :READFILE RESULT %file%

no data is returned to %RESULT% in file_c.cmd.

I don't understand why does the CALL command returns data to each of the files for the HEADER function, but not for the READFILE function? It seems inconsistant.

Is there a way to do what I want or am I just going to have to include each of the Functions in file_d.cmd in all of the other command files?

Any help would be appreciated.

Re: CALL & Data Flow Between Child and Parent Command Files

Posted: 22 Apr 2011 19:12
by dbenham
CALL expects either
1) a batch file - execution starts at the top of the file
or
2) a :label (within the current batch) - execution starts immediately after the label.

It does not natively support both arguments in the same call. The :HEADER and :READFILE arguments in your call statements are simply treated as arguments to the file_d.cmd, not as instructions to jump to the labels within it. Your "calls" to :HEADER only work because the :HEADER routine happens to be at the top of the file, and it does not take any arguments.

Assuming that you will always pass a label to file_d.cmd, you can achieve what you want by modifying the top of file_d.cmd as follows:

Code: Select all

@echo off
shift /1 & goto %1
<continue with rest of your code>


dbenham