Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last modified date on Windows command line for a set of files?

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?

like image 560
Ricky Supit Avatar asked Jan 21 '10 17:01

Ricky Supit


People also ask

How do I find the last modified date of a file?

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.

Which command gives information about time of last modification done on file?

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.

How do I print the last modified time of a directory?

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.


2 Answers

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= 
like image 100
Andy Morris Avatar answered Sep 28 '22 05:09

Andy Morris


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:? ) 
like image 30
PodTech.io Avatar answered Sep 28 '22 06:09

PodTech.io