Referencing all subfolders in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Conk1
Posts: 12
Joined: 04 Dec 2009 09:43

Referencing all subfolders in a folder

#1 Post by Conk1 » 19 Jul 2010 09:27

Hi I'm trying to make a batch file easier as I do a similar task to this quite a few times:

del /f /s /q "C:\ProgramData\Kaspersky Lab\AVP6\Data\Updater\Temporary Files\*.*"
del /f /s /q "C:\ProgramData\Kaspersky Lab\AVP7\Data\Updater\Temporary Files\*.*"
del /f /s /q "C:\ProgramData\Kaspersky Lab\AVP8\Data\Updater\Temporary Files\*.*"
del /f /s /q "C:\ProgramData\Kaspersky Lab\AVP9\Data\Updater\Temporary Files\*.*"
del /f /s /q "C:\ProgramData\Kaspersky Lab\AVP10\Data\Updater\Temporary Files\*.*"
del /f /s /q "C:\ProgramData\Kaspersky Lab\AVP11\Data\Updater\Temporary Files\*.*"

Ideally I'd like to wildcard it like:
del /f /s /q "C:\ProgramData\Kaspersky Lab\*\Data\Updater\Temporary Files\*.*"
So that it would delete all the folders mentioned above.

Anyone know how I can do this?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Referencing all subfolders in a folder

#2 Post by aGerman » 19 Jul 2010 13:34

FOR /D will help you.

Code: Select all

@echo off &setlocal
pushd "C:\ProgramData\Kaspersky Lab" ||goto :eof
for /d %%a in (AVP*) do (
  del /f /s /q "%%a\Data\Updater\Temporary Files\*.*"
)
popd



Regards
aGerman

Conk1
Posts: 12
Joined: 04 Dec 2009 09:43

Re: Referencing all subfolders in a folder

#3 Post by Conk1 » 20 Jul 2010 03:06

Thanks!

One more question though.

Say the folders weren't all called AVP6, AVP7, AVP8 etc and were just random names. Could I change your code to:
for /d %%a in (*)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Referencing all subfolders in a folder

#4 Post by aGerman » 20 Jul 2010 05:42

Yes, of course. The asterisk is for each literal expression, so your FOR loop will find each subfolder in the current directory.
You could also write something like that
for /d %%a in (?????) do ...
to find all subfolder names with 5 or less single characters.
For experiments it's harmless to echo the found names, e.g.

Code: Select all

for /d %%a in (?????) do echo %%a
pause


Regards
aGerman

Conk1
Posts: 12
Joined: 04 Dec 2009 09:43

Re: Referencing all subfolders in a folder

#5 Post by Conk1 » 20 Jul 2010 06:32

Thanks once again :)

Post Reply