Delete all files in several folders under a wildcard directory, Win7

I want to delete any files inside 3 folders (their names do not change).  However, these folders are inside a folder that has a variable name (created automatically).  In the scenario below, L12345 is the variable name, and will always start with L, but the numbers that follow will be randomized.

In this scenario, I want to delete all files inside dir1, dir2, and dir3 which are inside the variable-ly named folder L12345.

C:\Program Files\main\L12345\dir1\*.*
C:\Program Files\main\L12345\dir2\*.*
C:\Program Files\main\L12345\dir3\*.*

How do I accomplish this?  This will be run under Windows 7.
fl4ianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

B HCommented:
how about this:

note:  this won't actually delete anything until you remove the REM at the bottom

check results.txt for what would happen

run in the "main" folder as a .BAT file

echo starting > results.txt
for /f %%R in ('dir /b /ad') do call :1 %%R
echo done >> results.txt

:1
echo %1's files would have been deleted >> results.txt
cd %1
cd dir1
echo %1 \ dir1 \ *.* would be deleted >> results.txt
rem del /q *.*
cd ..
cd dir2
echo %1 \ dir2 \ *.* would be deleted >> results.txt
rem del /q *.*
cd ..
cd dir3
echo %1 \ dir3 \ *.* would be deleted >> results.txt
rem del /q *.*
cd ..
cd ..

Open in new window

0
B HCommented:
(line 6 should be deleted, but doesnt affect anything)
0
B HCommented:
this will actually do the deletes, so be careful and test first
@echo off
rem this should be ran in the "main" parent folder
rem it creates a new results.txt each time it runs. 
rem change the ">" to ">>" if you just want to append forever
rem "rem" out the whole line if you dont want it at all

echo starting at %date% %time% > results.txt
for /f %%R in ('dir /b /ad') do call :1 %%R
echo done at %date% %time% >> results.txt

:1
cd %1
cd dir1
del /q *.*
cd ..
cd dir2
del /q *.*
cd ..
cd dir3
del /q *.*
cd ..

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

fl4ianAuthor Commented:
I was editing the question, but it locked!

couple notes:
1) There may be more than one L***** directory.
2) It will need to be run directly from the user's desktop instead of inside the main directory (or instead of putting the .bat inside main, and placing a shortcut on the desktop).
0
B HCommented:
the line:
for /f %%R in ('dir /b /ad') do call :1 %%R

means, "do a directory listing, and for each directory listed go into it and do what's at the bottom"

at the bottom, it says:
"go into the first result, then into dir1 and delete *.*, then back up and go into dir2 and delete *.*, then back up and go into dir3 and delete *.*... then return to the script and continue on with the next directory listed"

are there other directories besides L##### ones?  if so, you might change the FOR line to be this:

for /f %%R in ('dir L*. /b /ad') do call :1 %%R
0
B HCommented:
host the script in the "main" folder, right-click copy it.. .then on the users desktop, right-click paste shortcut.

right-click the shortcut, properties, and in "start in" make it be the path to the "main" folder
0
Bill PrewCommented:
==> In this scenario, I want to delete all files inside dir1, dir2, and dir3 which are inside the
==> variable-ly named folder L12345.

==> C:\Program Files\main\L12345\dir1\*.*
==> C:\Program Files\main\L12345\dir2\*.*
==> C:\Program Files\main\L12345\dir3\*.*


So in this example, do you want to also remove the dir1, dir2 and dir3 folders, or just the files in them?

Also, are there any subfolders in dir1, dir2 and dir3?

~bp
0
Bill PrewCommented:
If you want to remove the entire subdirectory then this would do that.  Keep in mind though, that to delete files from c:\program files on Windows 7, the user running this script will need elevated access.

@echo off
set BaseDir=C:\Program Files\main
set DirList="dir1","dir2","dir3"
for /D %%A in ("%BaseDir%\L*") do (
  for %%B in (%DirList%) do (
    if exist "%%~A\%%~B" rd /S /Q "%%~A\%%~B"
  )
)

Open in new window

~bp
0
Bill PrewCommented:
If you want to test the above first, before letting it delete anything, change:

rd /S /Q "%%~A\%%~B"

to

ECHO rd /S /Q "%%~A\%%~B"

and it will just display to the screen the commands that would have been executed.  You'll want to do this from a command prompt, or add a PAUSE to the BAT script though, so that you can see the results (rather than launching from the desktop for testing, where the window will open, and then quickly close).

~bp
0
fl4ianAuthor Commented:
@bryon: it was making the log appear in several places, so I rem'd out lines 6 and 8 for now.  I would like the log to be appended, and list what was deleted, and the log to be located in the same directory as the batch file itself.  Is that possible?  If it's not possible to list what was deleted, can the rest be accomplished?  Lastly, if I wanted it to delete *.* in C:\temp what would I add?

@bill: no, only the contents, not the directories. I think bryon is close here, though.
0
fl4ianAuthor Commented:
@bryon: should have been 7 and 9 for the REMs.
0
Bill PrewCommented:
Okay, if just the files then this will work:

@echo off
set BaseDir=C:\Program Files\main
set DirList="dir1","dir2","dir3"
for /D %%A in ("%BaseDir%\L*") do (
  for %%B in (%DirList%) do (
    if exist "%%~A\%%~B" del /Q "%%~A\%%~B\*.*"
  )
)

Open in new window

~bp
0
B HCommented:
for mine, with your new changes, i'd do this:


@echo off
rem this should be ran in the "main" parent folder
rem it creates a new results.txt each time it runs. 
rem "rem" out the whole line if you dont want it at all

echo starting at %date% %time% >> c:\progra~1\main\results.txt
for /f %%R in ('dir L*. /b /ad') do call :1 %%R
c:
cd\temp
del /q *.*
echo done at %date% %time% >> c:\progra~1\main\results.txt

:1
cd %1
cd dir1
del /q *.*
cd ..
cd dir2
del /q *.*
cd ..
cd dir3
del /q *.*
cd ..
cd..

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
B HCommented:
(well line 3 no longer applies, it all appends)

not sure how to make it list what files as they're deleted though

maybe you could add a line after 14, 18, and 21 that does:
dir >> c:\progra~1\main\results.txt

but that could end up in a really big log file depending how many files you have
0
fl4ianAuthor Commented:
Thank you both for the effort.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.