Page 1 of 1

How do I display a list of files with last-modified dates?

Posted: 16 Apr 2008 10:25
by davelr
I need to output the files of a directory/sub-directory along with the last-modified date (either before or after with a delimiter separating the two) to a text file. The closest I could get is:
dir *.* /s/b/a/t:w > filelist.txt
The problem is, I cannot see the last-modified date when using the "/b" switch to turn off header/summary info (which I do not want). Obviously it will take a little dos programming to do what I need. Can anyone help?

Thanks,
Dave

Answered my own question!

Posted: 17 Apr 2008 17:39
by davelr
To do this I had to create a .bat file with the following command:

for /r "%userprofile%\Desktop\" %%F in (*.xl*) do (echo.%%~ftF >> "%userprofile%\Desktop\excel_results.txt")

If I simply write to the output file each time (rather than append, as I do above with >>) it overwrites the previous file, resulting in one file at the end. My final version of the batch file will delete the output file first before executing the search.

Using the /r switch causes the search to start at the root directory (in this case the desktop) and search through all subdirectorys.

List of files with dates & date/time stamp output file n

Posted: 18 Apr 2008 11:44
by davelr
To add further to my last reply, I added the functionality of date/time-stamping the output file name, eliminating the need to delete the output file each time:

@echo off

:: May or may not need to pad month and hour, so better to be safe.
:: You may need to change the positioning for the PADMTH or PADHOUR,
:: depending on your system settings. Just echo.%date% & echo.%time%

set PADMTH=0%DATE:~5,2%
set MTH=%PADMTH:~-2%

set PADHOUR=0%TIME:~0,2%
set HOUR=%PADHOUR:~-2%

set datename=%DATE:~0,4%%MTH%%DATE:~8,2%_%HOUR%%TIME:~3,2%%TIME:~6,2%

for /r %%F in (*.xl*) do (echo.%%~ftF >> Search_results_%datename%.txt)


I'm by no means a DOS guy, so if anyone knows any errors with the above, or a better way to do it, feel free to reply!

Dave

Posted: 29 Apr 2008 01:03
by DosItHelp
davelr,

Well done. I'm sure you are happy with your timestamped filenames.

To eliminate the need to delete the output file each time you can optionally redirect the output of the whole FOR command into a file instead of appending to the file from within the body of the loop:

Code: Select all

(
   for /r "%userprofile%\Desktop\" %%F in (*.xl*) do echo.%%~ftF
) > "%userprofile%\Desktop\excel_results.txt"

Hope this tip is useful :wink: