1

I have a batch script that generates a date stamped file name for creating a log file via php. That filename is stored in a windows batch file variable called logFile.

I would like to reuse the variable in my piped tee command however it appears that the sub command created by the pipe doesn't inherit or have the variables from the parent cmd console in context.

Can anyone show how to resolve this issue without having to use temporary files or the creation of another batch script?

Thanks!

execJobs.bat

@echo off

SET logFile=| php -r "$d = new DateTime(); $dateString = $d->format('Ymd-His');  
                     echo 'C:\\temp\\logs\\' . $dateString . '-exec.log';"

:: this is Ok
:: outputs c:\temp\logs\20150109-111031-exec.bat
%logFile%

:: This doesn't work
:: %logFile% in subCmd process is empty
:: how to inherit or pass environment/variable context to child processes?
call execJobs.bat | tee %logFile%

:: workaround, this doesn't work either
:: %logFile% is still empty in move command
:: call execJobs.bat | tee tmp
:: move /Y tmp %logFile%
:: del /F /Q tmp

Outputs:

C:\temp\logs\20150109-114443-exec.log
execJobs.bat | tee
mark
  • 13
  • 4
  • 1
    `tee` isn't a native batch command. Are you using the GnuWin32 tools? – SomethingDark Jan 09 '15 at 04:48
  • yes I'm using the `tee.exe` command from [GnuWin32Tools](http://unxutils.sourceforge.net/). [BatchTee](http://stackoverflow.com/questions/11239924/windows-batch-tee-command) also works – mark Jan 10 '15 at 05:24

3 Answers3

1

The problem is that this

SET logFile=| php -r "$d = new DateTime(); $dateString = $d->format('Ymd-His');  
                     echo 'C:\\temp\\logs\\' . $dateString . '-exec.log';"

doesn't actually do what you want it to; the pipe operator divides it into two commands, one of which deletes the logFile environment variable and the other of which prints the filename.

This

%logFile%

does nothing at all, since logFile isn't defined and hence expands to an empty string.

The output you think is coming from the expansion of logFile is actually coming from the previous command.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
1

In batch files, the "proper" way to execute a command and retrieve its output into a variable is to use a for /f command

@echo off
    setlocal enableextensions disabledelayedexpansion
    set "logFile="

    for /f "usebackq" %%a in (`
        php -r "$d=new DateTime(); echo $d->format('Ymd-His');"
    `) do set "logFile=C:\temp\logs\%%a-exec.log"

    echo %logFile%

The for command will execute the code in the do clause for each line in the output of the execute comamnd. The replaceable parameter (%%a is used in this case) will hold the contents of the line being processed.

MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Thanks for the comment Harry, MC you beat me to it, posting the solution. That makes sense. Here is the solution if anyone else is interested. Adding the output from a command to a shell variable is not very intuitive :)

:: this command places the output from the php script into a shell variable: logFile
for /f %%i in ('php -r "$d = new DateTime(); $dateString = $d->format('Ymd-His'); 
    echo 'C:\\temp\\logs\\' . $dateString . '-exec.log';"') do set logFile=%%i
echo Logging execJobs.bat output to: %logFile%
echo.
call execJobs.bat | tee %logFile%

I also pieced together some information from this answer as well: Windows batch assign output of a program to a variable

Community
  • 1
  • 1
mark
  • 13
  • 4