-1

I want to get the time of the last modified file in a folder using command prompt.

For example, a folder has 10 files. In that I want the time of the recently modified file. So it should return only one entry and that should be timestamp.

Thanks in advance

aschipfl
  • 33,626
  • 12
  • 54
  • 99
sar12089
  • 55
  • 4
  • 12
  • Start by using the command [`dir`](http://ss64.com/nt/dir.html), which provides filter (file/directory) and sorting (name/date/...) capabilities; then you may be interested in [`for /F`](http://ss64.com/nt/for_cmd.html) to capture the output... – aschipfl Jun 21 '17 at 17:02

1 Answers1

2

This is very easy to achieve:

@echo off
for /F "delims=" %%I in ('dir * /A-D /B /O-D 2^>nul') do set "NewestFileTime=%%~tI" & goto NewestFileTime
echo There is no file in current directory.
exit /B

:NewestFileTime
echo Last modification time of newest file is: %NewestFileTime%

Please note that the format of the date and time string assigned to the environment variable NewestFileTime depends on Windows Region and Language settings set for the current user account respectively the account used on running this batch file.

There is also the possibility to get the last modification time of the newest file in a region-independent format, see answer on Find out if file is older than 4 hours in batch file for details.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • dir /?
  • echo /?
  • exit /?
  • for /?
  • goto /?
  • set /?

See also single line with multiple commands using Windows batch file for an explanation of the unconditional AND operator & used in this batch code.

Mofi
  • 46,139
  • 17
  • 80
  • 143