We help IT Professionals succeed at work.

Script to delete all files & folders except top level folder

keonh
keonh asked
on
493 Views
Last Modified: 2012-05-11
Hello, Oh great scripting guru's!!!  I would like to have a script that will delete all files & folders including subfolders (may be several levels deep) within a specially named folder (moved_* or old_*) but don't delete the moved_ or old_ folder.  


Example:

Before Script:

Folder old_abc
            Files
            Folder
                  Subfolder
                        Files
            Files
            Folders

Folder moved_abc
            Files
            Folder
                  Subfolder
                        Files
            Files
            Folders

Folder old_123
Files
            Folder
                  Subfolder
                        Files
            Files
            Folders

Folder moved_123
            Files
            Folder
                  Subfolder
                        Files
            Files
            Folders


After Script:

Folder old_abc
Folder moved_abc
Folder old_123
Folder moved_123


I hope this makes sense.
Thanks in advance!! It's greatly appreciated!!
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Does this need to be VBS or would a BAT script work just as well for you (quite a bit easier).

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
If a BAT approach works, here are two examples that both would do the job.

@echo off
set BaseDir=c:\temp
del /F /Q "%BaseDir%\*.*"
for /D %%A in ("%BaseDir%\*") do rd /S /Q "%%A"

Open in new window


@echo off
set BaseDir=c:\temp
pushd "%BaseDir%"
del * /F /Q
rd . /S /Q 2>NUL
popd

Open in new window

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
If you need a VBS solution then I think this should do the job.

strBaseDir = "c:\temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(strBaseDir & "\*.*")
Set objFolder = objFSO.GetFolder(strBaseDir)
For Each objSubFolder In objFolder.SubFolders
   objFSO.DeleteFolder(objSubFolder.Path)
Next

Open in new window

~bp

Author

Commented:
Which ever is easier for you as long as both get the job done.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, you have your pick now :-).

~bp

Author

Commented:
Thanks bp!!  I've tested each.  Sorry to report...   They delete all the folders including old_somename & moved_somename.  I need to keep those folders and delete the contents.  

For my test, I created 3 folders name old_abc, moved_abc & old_123 in C:\temp.  Each of the folders contains files & folders that need to be deleted but I need to keep the empty parent folders, old_abc, moved_abc & old_123.  The folders all begin with either old_somename or moved_somename.  

Thanks again bp.  This may be somewhat complexed so either vbs or bat is fine, whichever is easier.
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
bp ... Excellent work!! It works just like I need it too.  Thanks!!  Your ranking of Genius is beneath you.  Thanks again!! This will definitely shave some time than manually doing this task.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Great, glad that was helpful, and thanks for the kind words.

~bp
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.