I am trying to retrieve the timestamp of file in YYYYMMDDHHMMSS format using WMI in a batch script as below:
@echo off
::if "%~1" == "" goto :EOF
setlocal EnableDelayedExpansion
::for %%V in (%1) do (
rem Get name of file with full path.
set FileName='C:\scot\logs\marker.txt'
rem Escape each backslash in file path with one more backslash.
rem set 'FileName=!FileName:\=\\!'
echo FilePath=%FileName%
rem Get creation date and time of file in format YYYYMMDDHHMMSS.
::for /F "usebackq skip=1 tokens=1 delims=." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='!FileName!'" get CreationDate') do set FileDateTime=%%T
for /f "usebackq tokens=2 delims=." %%T in ('%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='!FileName!'" get CreationDate') do set FileDateTime=%%T
echo FileCreationTime1=%FileDateTime%
rem The first 8 characters are file creation date.
set "FileDate=!FileDateTime:~0,8!"
rem The next 6 characters are file creation time.
set "FileTime=!FileDateTime:~8,6!"
echo FileCreationTime2=%FileTime%
if exist "!FileDate!_!FileTime!-%%~nV%%~xV" (
echo Cannot rename file: %%~fV
) else (
ren "%%~fV" "!FileDate!-!FileTime!-%%~nV%%~xV"
echo FileCreationTime=%FileTime%
)
::)
endlocal
but it is returning saying "No Instance(s) Available".
When I run the same WMIC command from command line I am able to get the time stamp value. Please help me?
You have usebackq
but in the code backquotes are not used.And the doubling of backslashes is commented. Check this example:
@echo off
setlocal enableDelayedExpansion
set "FileName=%SystemRoot%\System32\wbem\wmic.exe"
set "FileName=!FileName:\=\\!"
for /f "usebackq tokens=2 delims=." %%T in (`"%SystemRoot%\System32\wbem\wmic.exe DATAFILE where name='!FileName!' get CreationDate"`) do set "FileDateTime=%%T"
echo %FileDateTime%
As I have no your file wmic.exe is used to get the time.
EDIT:
@echo off
setlocal enableDelayedExpansion
set "FileName=%SystemRoot%\System32\wbem\wmic.exe"
set "FileName=!FileName:\=\\!"
for /f "usebackq delims=" %%T in (`"%SystemRoot%\System32\wbem\wmic.exe DATAFILE where name='!FileName!' get CreationDate /format:value"`) do (
for /f "tokens=2 delims==" %%# in ("%%T") do set "FileDateTime=%%#"
)
echo %FileDateTime%