-2

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
  • 31,767
  • 12
  • 51
  • 89
sar12089
  • 45
  • 3
  • 12
  • What have you tried? SO is not a code writing service, it's a platform where people can help out with specific code issues. – stevieb Jun 21 '17 at 16:57
  • 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

1

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.
goto :EOF

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

Please note date the format of date and time string assigned to environment variable NewestFileTime depends on Windows Region and Language settings as set for the used account.

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

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

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

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

Mofi
  • 42,677
  • 15
  • 72
  • 128