55} How to get the creation, last modified and last access of a file?
@echo off & setlocal enableextensions
::
:: Get the file name
if ["%~1"]==[""] (echo Usage: %~f0 FileName & goto :EOF)
if not exist "%~f1" (echo File "%~f1" not found & goto :EOF)
set fname_=%~f1
::
:: Display the information
for /f %%f in ('dir "%fname_%" /s /b /-c /a:-d-s-h') do echo %%~ff
echo Created:
for /f "skip=5 tokens=*" %%f in (
'dir "%fname_%" /tc /-c /a:-d-s-h^|find /v "(s)"') do echo %%f
echo Last written:
for /f "skip=5 tokens=*" %%f in (
'dir "%fname_%" /tw /-c /a:-d-s-h^|find /v "(s)"') do echo %%f
echo Last accessed:
for /f "skip=5 tokens=*" %%f in (
'dir "%fname_%" /ta /-c /a:-d-s-h^|find /v "(s)"') do echo %%f
endlocal & goto :EOF
Note that depending on the date/time format specified in your
computer's regional settings the output may vary. The output might
be something like
D:\TEST>cmdfaq batfaq.bat
D:\TEST\BATFAQ.BAT
Created:
14.11.2003 04:06 118 BATFAQ.BAT
Last written:
19.02.2004 13:11 118 BATFAQ.BAT
Last accessed:
21.02.2004 16:52 118 BATFAQ.BAT
Depending on what exactly we want, the above solution is not
necessarily satisfactory. The VBS assisted solution below put the
information first into accessible environment variables.
Furthermore, it includes the seconds in the file time stamp.
@echo off & setlocal enableextensions
::
:: Get the file name
if ["%~1"]==[""] (echo Usage: %~f0 FileName & goto :EOF)
if not exist "%~f1" (echo File "%~f1" not found & goto :EOF)
set fname_=%~f1
::
:: Build a Visual Basic Script
set skip=
set vbs_=%temp%\tmp$$$.vbs
findstr "'%skip%VBS" "%~f0" > "%vbs_%"
::
:: Run the Visual Basic Script
cscript //nologo "%vbs_%"
::
:: Call the command line script wchich the script host built
call tmp$$$.cmd
::
:: Clean up
for %%f in ("%vbs_%" tmp$$$.cmd) do if exist %%f del %%f
::
:: Demonstrate the result
echo %fname_%
echo Size: %~z1 bytes
echo Created: %fdc%
echo Last modified: %flm%
echo Last accessed: %fla%
::
endlocal & goto :EOF
'
'................................................................
'The Visual Basic Script
'
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim FileName, FSO, fin, fout 'VBS
Set WshShell = WScript.CreateObject("WScript.shell") 'VBS
Set FSO = CreateObject("Scripting.FileSystemObject") 'VBS
'
FileName=WshShell.ExpandEnvironmentStrings("%fname_%") 'VBS
Set fin = FSO.GetFile(FileName) 'VBS
'
Set fout = FSO.OpenTextFile("tmp$$$.cmd", ForWriting, true) 'VBS
fout.WriteLine "@set fdc=" & fin.DateCreated 'VBS
fout.WriteLine "@set flm=" & fin.DateLastModified 'VBS
fout.WriteLine "@set fla=" & fin.DateLastAccessed 'VBS
fout.Close 'VBS
The output might be something like
D:\TEST>cmdfaq batfaq.bat
D:\TEST\BATFAQ.BAT
Size: 118 bytes
Created: 14.11.2003 04:06:54
Last modified: 19.02.2004 13:11:32
Last accessed: 21.02.2004 16:52:49
Incidentally, would you have wanted the file size via the VBS
script then e.g.
fout.WriteLine "@set fsz=" & fin.size 'VBS
The same VBS could be written slightly differently as:
@echo off & setlocal enableextensions
::
:: Get the file name
if ["%~1"]==[""] (echo Usage: %~f0 FileName & goto :EOF)
if not exist "%~f1" (echo File "%~f1" not found & goto :EOF)
set fname_=%~f1
::
:: Build a Visual Basic Script
set skip=
set vbs_=%temp%\tmp$$$.vbs
findstr "'%skip%VBS" "%~f0" > "%vbs_%"
::
:: Run the Visual Basic Script
for /f "tokens=1-6 delims= " %%a in ('
cscript //nologo "%vbs_%" "%fname_%"') do (
set fdc=%%a %%b
set flm=%%c %%d
set fla=%%e %%f
)
::
:: Clean up
for %%f in ("%vbs_%") do if exist %%f del %%f
::
:: Demonstrate the result
echo %fname_%
echo Created: %fdc%
echo Last modified: %flm%
echo Last accessed: %fla%
::
endlocal & goto :EOF
'
'................................................................
'The Visual Basic Script
'
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim arg, FileName, FSO, f 'VBS
Set WshShell = WScript.CreateObject("WScript.shell") 'VBS
Set FSO = CreateObject("Scripting.FileSystemObject") 'VBS
'
Set arg = WScript.Arguments 'VBS
FileName = arg(0) 'VBS
Set f = FSO.GetFile(FileName) 'VBS
'
Wscript.Echo f.DateCreated & " " &
f.DateLastModified & " " & f.DateLastAccessed 'VBS
The output might be something like
C:\_M>C:\_D\TEST\CMDFAQ.CMD "C:\_D\TEST\My test file.txt"
C:\_D\TEST\My test file.txt
Created: 07.05.2008 17:20:59
Last modified: 23.06.2008 11:03:57
Last accessed: 06.07.2008 19:04:43
Most often one is after the "Last modified" information.
Now what if one wants the last modified date and time elements
separately into environment variables?
@echo off & setlocal enableextensions
::
:: Get the file name
if ["%~1"]==[""] (echo Usage: %~f0 FileName & goto :EOF)
if not exist "%~f1" (echo File "%~f1" not found & goto :EOF)
set fname_=%~f1
::
:: Build a Visual Basic Script
set skip=
set vbs_=%temp%\tmp$$$.vbs
findstr "'%skip%VBS" "%~f0" > "%vbs_%"
::
:: Run the Visual Basic Script
for /f "tokens=1-6 delims= " %%a in ('
cscript //nologo "%vbs_%" "%fname_%"') do (
set yyyy_=%%a
set mm_=%%b
set dd_=%%c
set hh_=%%d
set mn_=%%e
set ss_=%%f
)
::
:: Clean up
for %%f in ("%vbs_%") do if exist %%f del %%f
::
:: Demonstrate the result
echo %fname_%
echo yyyy_=%yyyy_%
echo mm_=%mm_%
echo dd_=%dd_%
echo hh_=%hh_%
echo mn_=%mn_%
echo ss_=%ss_%
::
endlocal & goto :EOF
'
'................................................................
'The Visual Basic Script
'
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim arg, FileName, FSO, f, d, s 'VBS
Set WshShell = WScript.CreateObject("WScript.shell") 'VBS
Set FSO = CreateObject("Scripting.FileSystemObject") 'VBS
'
Set arg = WScript.Arguments 'VBS
FileName = arg(0) 'VBS
Set f = FSO.GetFile(FileName) 'VBS
'
d = f.DateLastModified 'VBS
s = Year(d) 'VBS
s = s & " " & Z2(Month(d)) 'VBS
s = s & " " & Z2(Day(d)) 'VBS
s = s & " " & Z2(Hour(d)) 'VBS
s = s & " " & Z2(Minute(d)) 'VBS
s = s & " " & Z2(Second(d)) 'VBS
Wscript.Echo s 'VBS
'
Function Z2(x) 'VBS
Z2 = Right("0" & x, 2) 'VBS
End Function 'VBS
The output might be something like
C:\_M>C:\_D\TEST\CMDFAQ.CMD "C:\_D\TEST\My test file.txt"
C:\_D\TEST\My test file.txt
yyyy_=2008
mm_=06
dd_=23
hh_=11
mn_=03
ss_=57
Since all the ingredients are there one now has the tools for questions
like e.g. "
How does one plug files' modified
date/time into a set of file names?" See
Item #178.
Also see
FILEINFO.CMD "CMD shell for FILEINFO.VBS"
FILEINFO.VBS "FileInfo Visual Basic Script"