Reputation: 115
I am trying to redirect command output to variable using file as intermediary. I am able to redirect command output to file and but unable to read content of file into variable.
Batch File Script
echo off
pushd C:\Software
set TEMP_DIR=C:\Users\prade\Softwares
for /D %%i in (*) do (
pushd "%%i"
echo %TIME% > %TEMP_DIR%\a.log
type %TEMP_DIR%\a.log
for /f "delims=" %%x in (%TEMP_DIR%\a.log) do set duration=%%x
echo %%i %duration%
popd
)
popd
echo on
I gives me below output
C:\Users\prade>echo off
7:36:47.86
AutoHotKey
7:36:47.86
cygwin64
7:36:47.86
ffmpeg-20150518-git-451be67-win64-static
7:36:47.86
mtn-200808a-win32
7:36:47.86
ProcessExplorer
7:36:47.86
tools
As you see there is nothing after directory name but type
giving me content of file.
If I echo %duration%
after batch file terminate in same cmd window, it gives me last time back.
What I am doing wrong?
Upvotes: 0
Views: 283
Reputation: 6032
Add SETLOCAL EnableDelayedExpansion
just below ECHO OFF
and access duration
with !duration!
instead of %duration%.
Upvotes: 1
Reputation: 57252
@echo off
pushd C:\Software
set "TEMP_DIR=C:\Users\prade\Softwares"
setlocal enableDelayedExpansion
for /D %%i in (*) do (
pushd "%%i"
echo %TIME% > "%TEMP_DIR%\a.log"
type %TEMP_DIR%\a.log
for /f "usebackq delims=" %%x in ("%TEMP_DIR%\a.log") do set "duration=%%x"
echo %%i !duration!
popd
)
popd
echo on
Upvotes: 1