cmd:将wmic输出保存到变量 [英] cmd: save wmic output to variable
问题描述
我正在尝试将文件的时间戳转换为批处理文件中的变量.
I am trying to get the timestamp of a file into a variable in a batch file.
我的批处理文件imagetime.bat包含以下内容:
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%
我得到以下输出:
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:\>
当然,最后我希望看到"20110412103858.000000 + ***",以便可以使用cmd的字符串操作将其格式化为"04/12/2011,10:38:58".
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.
我在做什么错了?
更新
我尝试了Hackoo的解决方案.它适用于calc.exe,但不适用于V:\ setup.exe.删除@echo并暂停后,我得到以下输出(我将引用calc.exe的行更改为: set"targetfile = V:\ setup.exe"
):
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:\>
推荐答案
这会做什么?
@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
使用普通的命令行样式,这里再次没有插入符号:
Here it is again without the caret using your normal commandline style:
使用 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
这篇关于cmd:将wmic输出保存到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!