Page 1 of 1

Referencing all subfolders in a folder

Posted: 19 Jul 2010 09:27
by Conk1
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?

Re: Referencing all subfolders in a folder

Posted: 19 Jul 2010 13:34
by aGerman
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

Re: Referencing all subfolders in a folder

Posted: 20 Jul 2010 03:06
by Conk1
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 (*)

Re: Referencing all subfolders in a folder

Posted: 20 Jul 2010 05:42
by aGerman
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

Re: Referencing all subfolders in a folder

Posted: 20 Jul 2010 06:32
by Conk1
Thanks once again :)