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!!
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!!
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"
@echo off
set BaseDir=c:\temp
pushd "%BaseDir%"
del * /F /Q
rd . /S /Q 2>NUL
popd
~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
~bp
ASKER
Which ever is easier for you as long as both get the job done.
Okay, you have your pick now :-).
~bp
~bp
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
~bp
~bp