ForFiles - FOR loop through files

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 11153
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

ForFiles - FOR loop through files

Post by ChrisGreaves »

forum?

I discovered ForFiles last night and was/remain astonished to learn that it has been around since Vista (which I think I did not experience).
Forfiles is a useful windows command to select a set of files and then run a command on each of the files.

I was looking for a batch/command-line process to delete unwanted files and while I can do a pretty good job with the DOS FOR command I wanted a means of visiting each SubDirectiry in a neater manner.
Here is my early draft, which works for now.

Code: Select all

TITLE FFile.bat - runs the ForFiles command under Win10
::6:09 PM 9/10/2020 first version
:: Parameter 1 is the mask e.g. eras*.* OR ~*.* OR *.BAK

set pause=pause
set mask=%1
if "%1"=="" set mask=ERAS*.*
echo %mask%
%pause%

:: forfiles /p t:\ /m %mask% /s /c "cmd /c del @file"
forfiles /p t:\ /m %mask% /s 
%pause%
REM end of FFile.bat
ForFile.png
Cheers
Chris
P.S. I am in the habit of saving transient files with names like "Eraseme.txt" and "eraseme2.txt"
C
You do not have the required permissions to view the files attached to this post.
Entropy never sleeps

User avatar
John Gray
PlatinumLounger
Posts: 4722
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: ForFiles - FOR loop through files

Post by John Gray »

I just looked up my historic BATch files and found one using FORFILES (NT4 Resource Kit) from 2000!
I never really liked the command because it was a bit flaky: it doesn't (didn't?) support URLs but just drive letters, and it uses less than or equal to the number of days.
You end up with nasty looking commands like
forfiles -p%1 -d-%nod% -v -c"cmd /C del 0x22@FILE0x22"
for a DeleteOlder BATch file, where %1 is a folder name and %nod% the number of days
John Gray

Acronyms are useful, initially.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 11153
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: ForFiles - FOR loop through files

Post by ChrisGreaves »

John Gray wrote:
11 Sep 2020, 17:45
I just looked up my historic BATch files and found one using FORFILES (NT4 Resource Kit) from 2000!
Hello John, I posted a reply yesterday but it got sucked into a black hole, so here goes, again …
“Looked up”? You don’t use them any more? I was using FF.COM (9KB, 1990) until 16-bit was taken away from me :sad:. It was my fatigue from driving EveryThing.exe manually that prompted me to see if utilities had moved on. Now I see that they had, twenty years ago mea culpa
… and it uses less than or equal to the number of days.
I can see that this would be aggravting for anything above my simple requirements which are to remove trivial transient files based on Filenames.
I have forgotten what else I wrote, so this reply is now mercifully short :laugh: :rofl:
Cheers
Chris
Entropy never sleeps

JoeP
BronzeLounger
Posts: 1546
Joined: 25 Jan 2010, 02:12

Re: ForFiles - FOR loop through files

Post by JoeP »

You ought to consider learning PowerShell. It is extremely versatile and powerful. See PowerShell Documentation to get started.
Joe

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 11153
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: ForFiles - FOR loop through files

Post by ChrisGreaves »

JoeP wrote:
13 Sep 2020, 02:19
You ought to consider learning PowerShell.
Hi Joe; I had PowerShell - no, its successor OpenShell I believe - installed until the past two days when I System Restored to an earlier date.
It is on my list to re-install.
However the bulk of my old BATch files work just fine during the boot sequence, to load Word2003, for nightly and weekly backup runs etc.
It was this newer cleanup-routine i was looking to tweak.
Cheers
Chris
Entropy never sleeps

JoeP
BronzeLounger
Posts: 1546
Joined: 25 Jan 2010, 02:12

Re: ForFiles - FOR loop through files

Post by JoeP »

This is from the first paragraph in the Overview section at the article i linked previously:
"PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language. Unlike most shells, which accept and return text, PowerShell is built on top of the .NET Common Language Runtime (CLR), and accepts and returns .NET objects. This fundamental change brings entirely new tools and methods for automation."

I think you are referring to a start menu replacement.
Joe

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 11153
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: ForFiles - FOR loop through files

Post by ChrisGreaves »

JoeP wrote:
14 Sep 2020, 04:43
...This fundamental change brings entirely new tools and methods for automation." I think you are referring to a start menu replacement.
Hi Joe; you are right; I was mistaken.
On the surface (that is, at my cursory view about two months ago) PowerShell appeared to be an enhanced, even greatly enhanced, version of the good old DOS Command prompt.

Yes, I did install OpenShell (née Classic Shell) to inspect its presentation of programs and menus.

Right now I am firm in my opinion that the batch files that have been working for around twenty years are working well at what they were designed to do. Once I have stabilized my system (and my life!) I will be taking a look at PowerShell.
Thanks
Chris
Entropy never sleeps

User avatar
John Gray
PlatinumLounger
Posts: 4722
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: ForFiles - FOR loop through files

Post by John Gray »

My view of PowerShell is that, unless you have another 20 years of life left, you don't have time to learn how to use PowerShell properly...
John Gray

Acronyms are useful, initially.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 11153
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: ForFiles - FOR loop through files

Post by ChrisGreaves »

John Gray wrote:
15 Sep 2020, 08:25
My view of PowerShell is that, unless you have another 20 years of life left, you don't have time to learn how to use PowerShell properly...
Well, yes John. I agree. :thankyou: That thought was in the back of my mind with a Big Stick poking at me to grab my attention.
What I really need is Power Life, and since moving to Bonavista that seems to have overtaken me. There is just not enough time in the day.
Cheers
Chris
Last edited by ChrisGreaves on 15 Sep 2020, 14:12, edited 1 time in total.
Entropy never sleeps

JoeP
BronzeLounger
Posts: 1546
Joined: 25 Jan 2010, 02:12

Re: ForFiles - FOR loop through files

Post by JoeP »

You could also look at it as an opportunity to keep your mind sharp learning something new.

BTW, you can become reasonably proficient in PowerShell very quickly. All you favorite CMD shell commands exist in PS. You can use the CMD name or the PS name.
Joe

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 11153
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: ForFiles - FOR loop through files

Post by ChrisGreaves »

JoeP wrote:
15 Sep 2020, 14:06
You could also look at it as an opportunity to keep your mind sharp learning something new.
Hi Joe,

I know and support what you mean by keeping my mind sharp. And that I do. I have no TV, I have a vast collection of movies (1,600+ titles) on VHS/DVD which I will get around to watching once I have the VCR player plugged in and running; maybe this winter!

I confess to being fatigued by the chronic release of "new" products which are just yesterday's products with slightly larger (or smaller) tail fins!
BTW, you can become reasonably proficient in PowerShell very quickly. All you favorite CMD shell commands exist in PS. You can use the CMD name or the PS name.
Now this sounds like "you already know the basic old CMD.EXE stuff", and that being the case, that sounds to me as if all my basic CMD.EXE stuff will continue to work, which further suggests that all my BATch files will continue to function. Right?

Cheers
Chris
Entropy never sleeps

JoeP
BronzeLounger
Posts: 1546
Joined: 25 Jan 2010, 02:12

Re: ForFiles - FOR loop through files

Post by JoeP »

I make no assumptions when it comes to what Microsoft will or won't support. Nor do I pretend to know how long Microsoft will continue to have the CMD shell work. Based on the vast numbers there are, I would suremise that it will be for a very long time. And even if sometime Microsoft ceases to support the CMD shell the batch files will run in PowerShell because the CMD commands exist as aliases in PowerShell. I suspect that the CMD.exe shell will not get any new functionality.

PowerShell gets all the attention and funding. I suggest that if you want to do something more complex PowerShell is the way to go. There is an extensive library of scripts available at the link I sent before which you can use as is or to get a good start on something you need.
Joe