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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
davelr
Posts: 3
Joined: 15 Apr 2008 10:54

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

#1 Post by davelr » 16 Apr 2008 10:25

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

davelr
Posts: 3
Joined: 15 Apr 2008 10:54

Answered my own question!

#2 Post by davelr » 17 Apr 2008 17:39

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.

davelr
Posts: 3
Joined: 15 Apr 2008 10:54

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

#3 Post by davelr » 18 Apr 2008 11:44

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 29 Apr 2008 01:03

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:

Post Reply