cmd:将wmic输出保存到变量

时间:2018-02-07 22:01:50

标签: batch-file cmd wmic

我正在尝试将文件的时间戳记放入批处理文件中的变量中。

我的批处理文件imagetime.bat包含以下内容:

-lssl -lcrypto

我得到了这个输出:

set targetfile=%~1
set targetfile=%targetfile:\=\\%
echo %targetfile%
for /f "usebackq delims=" %%i in ( `wmic datafile where name^="%targetfile%" get creationdate` ) do ( echo %%i )
echo %timestamp%

当然,最后我想看到" 20110412103858.000000 + ***",以便我可以将其格式化为" 04/12 / 2011,10:38:58& #34;使用cmd的字符串操作。

我做错了什么?

更新

我尝试过Hackoo的解决方案。它适用于calc.exe但不适用于V:\ setup.exe。关闭了@echo并暂停后我得到了这个输出(我将指向calc.exe的行更改为:C:\>imagetime.bat V:\setup.exe C:\>set targetfile=V:\setup.exe C:\>set targetfile=V:\\setup.exe C:\>echo V:\\setup.exe V:\\setup.exe C:\>for /F "usebackq delims=" %i in (`wmic datafile where name="V:\\setup.exe" g et creationdate`) do (echo %i ) ) (echo CreationDate reationDate ) (echo 20110412103858.000000+*** 0110412103858.000000+*** ) (echo ECHO is on. C:\>echo "" "" C:\> ):

set "targetfile=V:\setup.exe"

3 个答案:

答案 0 :(得分:1)

以下是可以使用此方法存储WMIC变量Timestamp的批处理文件示例:

@echo off
set "targetfile=%windir%\system32\calc.exe"
set targetfile=%targetfile:\=\\%
for /f "usebackq delims=" %%i in ( `wmic datafile where name^="%targetfile%" get creationdate` ) do (
    for %%b in (%%i) do (
        set "timestamp=%%b"
    )   
)
echo TimeStamp : "%timestamp%"
pause

编辑:添加将WMIDate转换为日期的功能:

@echo off
set "targetfile=%windir%\system32\calc.exe"
set targetfile=%targetfile:\=\\%
for /f "skip=1 usebackq delims=" %%i in ( `wmic datafile where name^="%targetfile%" get creationdate` ) do (
    for %%b in (%%i) do (
        set "TS=%%b"
    )   
)
echo TimeStamp : "%TS%"
Call :ConvertTS2Date "%TS%" ConvertedDate
echo Converted TimeStamp : "%ConvertedDate%"
pause & exit
::***********************************************************************
:ConvertTS2Date <WMIDate> <Date2Set>
(
echo Wscript.echo WMIDateStringToDate("%~1"^)
echo Function WMIDateStringToDate(dtmDate^) 
echo     WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2^) ^& "/" ^& _ 
echo         Mid(dtmDate, 7, 2^) ^& "/" ^& Left(dtmDate, 4^) _ 
echo             ^& " " ^& Mid (dtmDate, 9, 2^) ^& ":" ^& _ 
echo                 Mid(dtmDate, 11, 2^) ^& ":" ^& Mid(dtmDate, _ 
echo                     13, 2^)^) 
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('cscript //nologo "%tmp%\%~n0.vbs"') do (set %2=%%a)
Del "%tmp%\%~n0.vbs"
Exit /b
::***********************************************************************

对于我的情况,输出结果是这样的:

TimeStamp : "20170929125013.781773+060"
Converted TimeStamp : "29-09-2017 12:50:13"

答案 1 :(得分:1)

这会做你想做的吗?

@Echo Off
Set "TF=%~f1"
Set "TS="
For /F "Skip=1" %%A In ('WMIC DataFile Where^
 "Name='%TF:\=\\%'" Get CreationDate') Do For %%B In (%%~nA) Do Set "TS=%%B"
If Not Defined TS Exit /B
Set "TS=%TS:~4,2%/%TS:~6,2%/%TS:~,4%,%TS:~-6,2%:%TS:~-4,2%:%TS:~-2%"
Echo %TS%
Pause

<强> [编辑]

这里没有使用正常命令行样式的插入符号:

使用UseBackQ

@Echo Off
Set "TF=%~f1"
Set "TS="
For /F "UseBackQ Skip=1" %%A In (`
    "WMIC DataFile Where Name="%TF:\=\\%" Get CreationDate"
`) Do For %%B In (%%~nA) Do Set "TS=%%B"
If Not Defined TS Exit /B
Set "TS=%TS:~4,2%/%TS:~6,2%/%TS:~,4%,%TS:~-6,2%:%TS:~-4,2%:%TS:~-2%"
Echo %TS%
Pause

没有UseBackQ(首选)

@Echo Off
Set "TF=%~f1"
Set "TS="
For /F "Skip=1" %%A In ('
    "WMIC DataFile Where Name="%TF:\=\\%" Get CreationDate"
') Do For %%B In (%%~nA) Do Set "TS=%%B"
If Not Defined TS Exit /B
Set "TS=%TS:~4,2%/%TS:~6,2%/%TS:~,4%,%TS:~-6,2%:%TS:~-4,2%:%TS:~-2%"
Echo %TS%
Pause

答案 2 :(得分:0)

wmic输出多行,因此您的变量会被覆盖几次(最后一个带有empy字符串)。但是只有一行=,所以只有一行在=之后带有第二个令牌。使用"tokens=2 delims=="可以获得所需的值:

@echo off
set "targetfile=%~f0"
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%targetfile:\=\\%" get CreationDate /value') do set "timestamp=%%I"
echo [%timestamp%]