cmd: save wmic output to variable

↘锁芯ラ 提交于 2021-02-04 21:05:30

问题


I am trying to get the timestamp of a file into a variable in a batch file.

My batch file, imagetime.bat contains the following:

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%

And I get this output:

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:\>

Of course, at the end I would like to see "20110412103858.000000+***", so that I can format it as "04/12/2011,10:38:58" with cmd's string manipulation.

What am I doing wrong?

Update

I tried Hackoo's solution. It works for calc.exe but not for V:\setup.exe. Having removed @echo off and pause I get this output (I changed the line referring to calc.exe to: set "targetfile=V:\setup.exe"):

C:\>hackoo_calc.bat

C:\>set "targetfile=C:\Windows\system32\calc.exe"

C:\>set targetfile=C:\\Windows\\system32\\calc.exe

C:\>for /F "usebackq delims=" %i in (`wmic datafile where name="C:\\Windows\\sys
tem32\\calc.exe" get creationdate`) do (for %b in (%i) do (set "timestamp=%b" )
)

) do (set "timestamp=%b" ) )

C:\>(set "timestamp=CreationDate" )

) do (set "timestamp=%b" ) ) 1.688252+120

C:\>(set "timestamp=20090714015711.688252+120" )

) do (set "timestamp=%b" ) )

C:\>echo TimeStamp : "20090714015711.688252+120"
TimeStamp : "20090714015711.688252+120"

C:\>hackoo_setup.bat

C:\>set "targetfile=V:\setup.exe"

C:\>set targetfile=V:\\setup.exe

C:\>for /F "usebackq delims=" %i in (`wmic datafile where name="V:\\setup.exe" g
et creationdate`) do (for %b in (%i) do (set "timestamp=%b" ) )

) do (set "timestamp=%b" ) )

C:\>(set "timestamp=CreationDate" )

) do (set "timestamp=%b" ) ) 8.000000+***

) do (set "timestamp=%b" ) )

C:\>echo TimeStamp : "CreationDate"
TimeStamp : "CreationDate"

C:\>

回答1:


Will this do what you want?

@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

[Edit]

Here it is again without the caret using your normal commandline style:

With 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

Without UseBackQ: (preferred)

@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:


Here is an example of batch file that can store the variable Timestamp from WMIC using this method :

@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

Edit : Adding function to Convert WMIDate to Date :

@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
::***********************************************************************

The output result is like this for my case :

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



回答3:


wmic outputs more than one line, so your variable gets overwritten several times (last one with an empy string). But there is just one line with a =, so just one line with a second token after =. Using "tokens=2 delims==" gives you just the desired value:

@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%]


来源:https://stackoverflow.com/questions/48674006/cmd-save-wmic-output-to-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!