0

I have a batch file that calls a php script. The php script returns a value that I want to use in the batch script. The php script echoes the correct return value however I have not been able to find a way to capture the value to use in the batch file.

I have tried a number of variations on for loops without success. My batch script is as follows:

@echo off
Setlocal

"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\hello.php

endlocal

This of course just returns "Hello." How can I set a variable in the batch file (a variable named retVar for example) to contain what is returned from this php call?

thank you, Shimon

3
  • Have you tried "php -f script.php | set bla= ? Commented Jun 26, 2014 at 0:07
  • If it is printed to standard out use for. for /f %A in ('time /t') do echo %A Commented Jun 26, 2014 at 0:17
  • Print or return? echo 33 is not the same as exit(33). Commented Jun 29, 2014 at 16:45

1 Answer 1

1
@echo off
    setlocal enableextensions
    for /f "delims=" %%a in (
       '"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\hello.php'
    ) do set "retVar=%%a"

    echo %retVar%

Use for /f to execute a command (in the in clause) and for each line in the command output, execute a block of code (in the do clase). For each line, the variable/replaceable parameter (%%a in sample code) will hold the line. In this case, as the output of the php command is only one line, it will be executed only one time, storing the php output line into the retVar variable.

for /f will, by default, try to tokenize the lines it reads, splitting in fields using spaces a delimiters. To avoid this behaviour, a empty list of delimiters is used, so all the line contents will be stored in %%a

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.