批处理文件获取命令的输出存储在变量中 [英] Batch file get command's output stored in a variable

查看:70
本文介绍了批处理文件获取命令的输出存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此WMIC输出存储到变量中.当我回声VAL时,我什么也没得到!我要达到的目的只是获取文件的最后修改日期.这个WMIC命令的问题是它返回一个长整数的日期,而我想操纵该输出

i'm trying to store this WMIC output into a variable. when i echo VAL i get nothing ! all i'm trying to achieve is getting a file's last modification date. the problem with this WMIC command is that it returns a date as a long number and i want to manipulate that output

这是我遇到此问题的脚本的一部分

this is the part of my script where i have this issue

:: these lines are at the top of the script

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

...
...
...
...

:: a function:

set COMM="WMIC DataFile WHERE Name='C:\\Program Files (x86)\\folder\\folder\\folder\\container.npp' Get InstallDate"

set VAL=1

for /f "skip=1" %%A in ('%COMM%') do (set VAL=%%A)
echo %VAL%

推荐答案

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "file=c:\\Program Files (x86)\\Internet Explorer\\iexplore.exe"
    for /f %%a in (
        'wmic DataFile where "Name='%file%'" get InstallDate ^| find "+" '
    ) do set "val=%%a"

    echo [%val%]

所有问题是正确引用字符串.对于 wmic ,包含文件名的字符串必须用单引号引起来,并且 for where 条件为双引号没有问题封闭.

All the problem is proper quoting of the string. For wmic the string containing the name of the file needs to be single quote enclosed and to have no problems with for the where condition is double quote enclosed.

这篇关于批处理文件获取命令的输出存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆