@gjacobse said in How To: for files:
That said, from what you are saying; is my ‘failure’ was omitting the /M which specifies the ‘searchmask’. And because of that omission, it didn’t have the comparative variable, and deleted everything.
There were two things that tripped you up.
The lack of /M to just find *.tmp files
Using del *.tmp as the command
First forfiles is a for-loop that executes whatever command you want for each file it finds.
As @JasGot mentioned you will get one hit for each file it will find.
With /D -15 you are specifying that you only want to find files older than 15 days.
With /S you are telling forfiles to look in all subdirectories as well. You may or may not want that.
With /M you can specify a filter which matching filenames you want to search for. Without /Mit will find all the files regardless if it's named 123.tmp or installation.txt
Your initial command would find all files in all subdirectories regardless if it what name it had. That's why you needed /M.
Secondly, forfiles will execute whatever command you want for each and every file it finds.
Since your command was del *.tmp it would delete all tmp files every time it found a file.
What you really wanted was to just delete the file it found.
The @file will have the name of the file so del @file would only delete the file that was found.