Search code examples
batch-filecmdwmic

cmd: save wmic output to variable


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

Solution

  • 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