Link to home
Start Free TrialLog in
Avatar of keonh
keonh

asked on

Script to delete all files & folders except top level folder

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!!
Avatar of Bill Prew
Bill Prew

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

~bp
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
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
Avatar of keonh

ASKER

Which ever is easier for you as long as both get the job done.
Okay, you have your pick now :-).

~bp
Avatar of keonh

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keonh

ASKER

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.
Great, glad that was helpful, and thanks for the kind words.

~bp