Kristian
Kristian

Reputation: 15

Search files according to extension and date using Windows command prompt

I'm trying to run a search through my files looking for HTML and the latest modified date, the format I'm trying to output is:

path to file and filename, date last modified - sorted in alphabetical order. (X:\mydirectory\myfile.html, 18-09-2014 18:15)

I have used the following string but for some reason I cannot get the last modified date of the file and even if I moved around the attributes I don't understand where I'm failing.

dir X:\*.html /s /o:n /t:w /b /a:-d   >> "X:\list-all-html-files.txt"  

any help is appreciated

Upvotes: 0

Views: 1605

Answers (2)

Vinay Mishra
Vinay Mishra

Reputation: 46

Please use the below command to get the files which are modified a day before today.

Z:>FORFILES /s /M *.html /C "cmd /c echo @path was changed on @fdate @ftime" /D -1 >> C:\PerlCompiler\list-all-html-new-999.txt

For more reference please go through the link http://ss64.com/nt/forfiles.html

FORFILES.exe (Native command in Vista/Windows7/2008, via Resource Kit for XP)

- Vinay Mishra

Upvotes: 2

Torqane
Torqane

Reputation: 146

The /b output is just a plain filename with path so there's no date information there. Also your syntax is a bit odd (don't need colons, for example) though it'll work. Meanwhile let's take it a step at a time.

My approach to get just the filenames (first test step) would be:

dir x:\*.html /s | find ".html" | sort > X:\list-all-html-files.txt

You don't need the quotes around the output filename in this case because there are no spaces; otherwise that's fine and a good idea. The last modified date is the date provided by default in the DIR output so you don't need to specify /tw either. You're going to want to sort the output because it's going to be scanning through various directories, rather than just making sure the names within each directory are sorted with /on. Assuming you aren't really appending to another file, you'd want to use > instead of >> so each run starts fresh, or use the code below to delete the output before starting. You also don't need /a-d (I don't think) because you're specifying an extension of .html, unless you think you could have an unlikely directory name that has an .html extension.

But you say indeed you need the full paths plus the details, so you need to use a FOR loop and extract the name, then individually go get the rest of the information. Assuming it's in a batch file (which is why I used %%I instead of %I). The %%~ti provides the timestamp and %%~dpnxI produces a concatenated drive + path + filename + extension:

@echo off
setlocal
set OUTFILE=list-all-html-files.txt
if exist %OUTFILE% del %OUTFILE%
for /f "tokens=*" %%I in ('dir x:\*.html /s /b ^| sort') do echo %%~dpnxI   %%~tI >> %OUTFILE%
endlocal

I used a local variable OUTFILE so it could be defined once but used in several places in the code. There's a TAB character between the %%~dpnxI and %%~tI to make the output something you could paste into e.g. Excel.

Upvotes: 0

Related Questions