I have been using the following command to get the file date. However, the fileDate
variable has been returning blank value ever since we moved to a different server (Windows Server 2003).
FOR /f %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a
Is there any other more reliable way to get the file date?
The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.
ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.
Using the stat command, we can also control the output by the -c FORMAT option. There are two formats to display the mtime: %y – displays time of last data modification in a human-readable format. %Y – displays time of last data modification in number of seconds since Epoch.
In the code that follows, change %
to %%
for use in batch file, for %~ta
syntax enter call /?
for %a in (MyFile.txt) do set FileDate=%~ta
Sample output:
for %a in (MyFile.txt) do set FileDate=%~ta set FileDate=05/05/2020 09:47 AM for %a in (file_not_exist_file.txt) do set FileDate=%~ta set FileDate=
Useful reference to get file properties using a batch file, included is the last modified time:
FOR %%? IN ("C:\somefile\path\file.txt") DO ( ECHO File Name Only : %%~n? ECHO File Extension : %%~x? ECHO Name in 8.3 notation : %%~sn? ECHO File Attributes : %%~a? ECHO Located on Drive : %%~d? ECHO File Size : %%~z? ECHO Last-Modified Date : %%~t? ECHO Drive and Path : %%~dp? ECHO Drive : %%~d? ECHO Fully Qualified Path : %%~f? ECHO FQP in 8.3 notation : %%~sf? ECHO Location in the PATH : %%~dp$PATH:? )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With