Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to delete all files & folders except top level folder

Posted on 2011-04-19
9
Medium Priority
?
483 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!!
0
Comment
Question by:keonh
  • 6
  • 3
9 Comments
 
LVL 59

Expert Comment

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

~bp
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35424956
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
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35425027
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:keonh
ID: 35425039
Which ever is easier for you as long as both get the job done.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35425797
Okay, you have your pick now :-).

~bp
0
 

Author Comment

by:keonh
ID: 35429649
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.
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 35429708
Give this VBS approach a try.

strBaseDir = "c:\temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)
For Each objSubFolder In objFolder.SubFolders
   If Lcase(Left(objSubFolder.Name, 4)) = "old_" Or Lcase(Left(objSubFolder.Name, 6)) = "moved_" Then
      objFSO.DeleteFile(objSubFolder.Path & "\*.*")
      For Each objRemove In objSubFolder.SubFolders
         objFSO.DeleteFolder(objRemove.Path)
      Next
   End If
Next

Open in new window

~bp
0
 

Author Comment

by:keonh
ID: 35432571
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.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35433614
Great, glad that was helpful, and thanks for the kind words.

~bp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question