cmd:将 wmic 输出保存到变量
cmd: save wmic output to variable
我正在尝试将文件的时间戳放入批处理文件中的变量中。
我的批处理文件 imagetime.bat 包含以下内容:
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”。
我做错了什么?
更新
我尝试了 Hackoo 的解决方案。它适用于 calc.exe 但不适用于 V:\setup.exe。删除 @echo 关闭并暂停后,我得到了这个输出(我将引用 calc.exe 的行更改为: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:\>
这是一个批处理文件示例,它可以使用此方法存储来自 WMIC 的变量时间戳:
@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"
这会如你所愿吗?
@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
wmic
输出多行,因此您的变量被多次覆盖(最后一个带有空字符串)。但是只有一行带有 =
,所以在 =
之后只有一行带有第二个标记。使用 "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%]
我正在尝试将文件的时间戳放入批处理文件中的变量中。
我的批处理文件 imagetime.bat 包含以下内容:
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”。
我做错了什么?
更新
我尝试了 Hackoo 的解决方案。它适用于 calc.exe 但不适用于 V:\setup.exe。删除 @echo 关闭并暂停后,我得到了这个输出(我将引用 calc.exe 的行更改为: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:\>
这是一个批处理文件示例,它可以使用此方法存储来自 WMIC 的变量时间戳:
@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"
这会如你所愿吗?
@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
wmic
输出多行,因此您的变量被多次覆盖(最后一个带有空字符串)。但是只有一行带有 =
,所以在 =
之后只有一行带有第二个标记。使用 "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%]