You are not logged in.

#1 24 Dec 2010 08:53

Jason404
Member
From: London, UK
Registered: 23 Apr 2009
Posts: 32

How do I execute a command on all files except certain files?

How do I script a command (function?) to process all files except certain ones?

For example, say I want to use ATTRIB on every file in a directory, apart from desktop.ini and thumbs.db?

Also, how would I then make it do the same thing in every contained directory, recursively?

Thanks.

Last edited by Jason404 (24 Dec 2010 08:57)

Offline

#2 24 Dec 2010 15:36

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 362

Re: How do I execute a command on all files except certain files?

Jason,
Of course the first thing to do when making changes like this that will affect an entire folder is to make a copy of the folder and play with that first!

Since desktop.ini and thumbs.db are system files the ATTRIB command will not work on them so there is nothing to do to exclude them.  The /S parameter will perform the operation on the specified folder and all subfolders.  So you can do what you want with 1 line.  The example below sets the RO attribute for all files in YourFolder and subfolders.

ATTRIB +R YourFolder\*.* /S

If you want to do this and exclude some other files that are not system files you could do somethng like this:

FOR /F "tokens=*" %%A IN ('DIR /B /S MyDir ^| FIND /V "SomeFile.xxx" ^| FIND /V "OtherFile.yyy"') DO ECHO.%%A

I do 'ECHO.%%A' first to verify that I am selecting the desired files.  When you have the desired results, replace ECHO.%%A with 'ATTRIB ... %%A' or whatever you desire.

Good luck and let us know how it works out or if you need further explanation.


Windows Shell Scripting and InstallShield

Offline

#3 24 Dec 2010 18:11

Jason404
Member
From: London, UK
Registered: 23 Apr 2009
Posts: 32

Re: How do I execute a command on all files except certain files?

Thanks a lot, RG.  All of that is very useful information for me.  Cheers.

Offline

#4 24 Dec 2010 19:24

Jason404
Member
From: London, UK
Registered: 23 Apr 2009
Posts: 32

Re: How do I execute a command on all files except certain files?

Just a little question:  when you typed

ECHO.%%A

, what is the dot for?  Does it do anything?

I've also seen people type slashes after echo, and I am not sure what that does either.

eg.

echo/Today is: %year%-%month%-%day%

Offline

#5 24 Dec 2010 20:00

Jason404
Member
From: London, UK
Registered: 23 Apr 2009
Posts: 32

Re: How do I execute a command on all files except certain files?

Hmmn, I cannot seem to get this to work for some reason:

FOR /F "tokens=*" %%A IN ('DIR /B /S /A:HSA %1 ^| FIND "thumbs.db" ^| FIND "desktop.ini"') DO ECHO.%%A

I was expecting it to list all the tumbs.db and desktop.ini files, but it only seems to work without the FIND parts:

FOR /F "tokens=*" %%A IN ('DIR /B /S /A:HSA %1') DO ECHO.%%A

EDIT:  Okay, I have just realised my stupid mistake, by looking a bit closer.  The second FIND was filtering further and not additive.

Last edited by Jason404 (24 Dec 2010 21:26)

Offline

#6 24 Dec 2010 20:08

sarbjitsinghgill
Member
From: TORONTO CANADA
Registered: 09 Dec 2007
Posts: 112
Website

Re: How do I execute a command on all files except certain files?

Jason404 wrote:

Just a little question:  when you typed

ECHO.%%A

, what is the dot for?  Does it do anything?

One of many of special characters can be used here to tell comand interpreter that this ECHO command has second syntax (Print).

 + = . : ; / \ ( [ ] 

This characer is consumed and rest of the line is printed without a spcae at the beginning.

If we want to print a line (blank) without such a special char command interpretter will use first syntax and tell us state of ECHO.

 D:\>echo
ECHO is on.

Sometimes it is needed to print a line that has only a variable, that may be blank at some times.

D:\>set "any_var= "

D:\>echo %any_var%
ECHO is on.

D:\>echo. %any_var%

D:\>

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

Offline

#7 24 Dec 2010 20:16

Jason404
Member
From: London, UK
Registered: 23 Apr 2009
Posts: 32

Re: How do I execute a command on all files except certain files?

Okay, thanks Sarbit.  Got it.

I think I will make a new post(s) for my further questions.

Last edited by Jason404 (24 Dec 2010 21:28)

Offline

Board footer

Powered by