简体   繁体   English

批量读取文件到变量不起作用

[英]Batch Reading File into Variable not working

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. 正如你看到的没有什么目录名之后,但type给我文件的内容。

If I echo %duration% after batch file terminate in same cmd window, it gives me last time back. 如果批处理文件在同一cmd窗口中终止后我echo %duration% ,则它使我上次返回。

What I am doing wrong? 我做错了什么?

@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

Add SETLOCAL EnableDelayedExpansion just below ECHO OFF and access duration with !duration! ECHO OFF下方添加SETLOCAL EnableDelayedExpansion ,并使用!duration!访问duration !duration! instead of %duration%. 而不是%duration%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM