RE: Batch File/Script to Recursively Delete Files and Folders in Subdi

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Bound to be something at
http://www.microsoft.com/technet/scriptcenter/default.mspx
or try adapting this (you could replace DeleteFiles with fso.DeleteFolder
(removes all content) for the directories below the subdirectories you want
to keep.)

=========================================================================
' RecursiveDeleteFiles.vbs
' 27 Jan 2004 Bruce Cartland
'
' See Usage for arguments or run without any arguments.
'
' From the given directory: recurse, find all supplied Extensions of the form
' *.ext and delete the files. Doesn't affect the directory tree.
'
' This script was designed to be called from the make file as part of a Clean.

const strComputer = "."
Recurse = true
FolderCount = 0
FolderCountOnMod = 5
FileExtns = ""
arrFileExtns = split(FileExtns,";")

set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set shell = WScript.CreateObject("WScript.Shell")

StartFolder = "."

ParseArgs

if StartFolder = "." then StartFolder = shell.CurrentDirectory
if StartFolder = "-" then
StartFolder = shell.CurrentDirectory
Recurse = false
end if

WScript.StdOut.WriteLine "Deleting " & FileExtns
if Recurse then
WScript.StdOut.WriteLine "(recursively from " & StartFolder & ")"
else
WScript.StdOut.WriteLine "(from " & StartFolder & ")"
end if
RecursivelyDeleteFiles fso.GetFolder(StartFolder)
WScript.StdOut.WriteLine

'************************************** subs and functions
****************************************************
Sub QuitMsg(msg)
if Len(msg) > 1 then
WScript.StdOut.WriteLine ""
WScript.StdErr.WriteLine msg
end if
WScript.Quit(1)
End Sub

Sub Usage(msg)
WScript.StdOut.WriteLine "usage: " & WScript.ScriptName & "
<StartFolder> | . <FileExt1;FileExt2;..>"
WScript.StdOut.WriteLine ""
WScript.StdOut.WriteLine "Starting at path <StartFolder>, find all files
matching FileExt1;FileExt2;.."
WScript.StdOut.WriteLine "and delete. Recusively scan subdirectories."
WScript.StdOut.WriteLine "<StartFolder> = '.' or '-' means current
directory. '-' suppresses recursion."
WScript.StdOut.WriteLine "Matching is based on *.FileExt1, ..."
WScript.StdOut.WriteLine ""
WScript.StdOut.WriteLine "eg. " & WScript.ScriptName & "
c:\work\source\project ocx;dcu;dll;exe;~pas"
QuitMsg(msg)
End Sub

Sub ParseArgs()
set oArgs = WScript.Arguments
' hidden arg to stop "casual" use.
if oArgs.Count >= 1 and oArgs(0) <> "MAKE" then Usage("ERROR: For safety
this is meant to be called from a makefile. Go away-use del")
if oArgs.Count < 3 then Usage ""
StartFolder = oArgs(1)
FileExtns = oArgs(2)
arrFileExtns = split(FileExtns,";")
End Sub

Sub DeleteFiles(Folder)
AtLeastOne = false
For Each spec in arrFileExtns
For Each f in Folder.Files
extpos = InStrRev(f.Name, ".", -1, 1)
if extpos > 0 then
if InStr(extpos, f.Name, "." & spec, 1) then
s = Folder.Path & "\" & f.Name
if fso.FileExists(s) then
AtLeastOne = true
WScript.StdOut.WriteLine
WScript.StdOut.Write s
fso.DeleteFile s, true
end if
end if
end if
Next
Next
if AtLeastOne then WScript.StdOut.WriteLine
End Sub

Sub RecursivelyDeleteFiles(Folder)
FolderCount = FolderCount + 1
if FolderCount mod FolderCountOnMod = 0 then WScript.StdOut.Write "."

DeleteFiles(Folder)
if Recurse then
For Each sf in Folder.SubFolders
RecursivelyDeleteFiles sf
Next
end if
End Sub
=========================================================================

"Collin" wrote:

> Hi! I am looking for some way to delete files and folders within the subdirectories of a parent. The only trouble is I don't want to remove the subdirectories themselves--just the contents. Is there some way to do this via a batch file or something else? Any help would be much appreciated. Thanks!
>
> **********************************************************************
> Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
> Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
>
.



Relevant Pages

  • Re: Looping on "if" statement?
    ... We upgrade our Linux Redhat OS from 3 to 5.5 and ... you shouldn't be one to use terms like recurse! ... recursing is a sub concept and has nothing to do with if's. ... that is tail recursion and is much simpler with a plain loop. ...
    (comp.lang.perl.misc)
  • Recurse into an HTML tree?
    ... I'm trying to figure out how to recursively scan an HTML tree. ... to recurse into the next level. ... Can anyone provide any clues to what code I can sub for "CAN ... DEEPER" in the ifstatement? ...
    (perl.beginners)
  • how to find the total number of files in a directory
    ... How can i find the total number of files in a directory (including sub ... but i'd have to recurse through ... Use of any com or api wouldn't matter. ...
    (microsoft.public.fox.helpwanted)
  • Re: browse each site in ie favorites list
    ... Recurse the favorites-namespace tree, checking items to be ... links or a folders ... Sub BrowseURLS ...
    (microsoft.public.scripting.vbscript)
  • Re: Searching a filename from multiple workbooks or drives
    ... you need to search the directory tree, including any subdirectories under the ... Sub ListAllFilesInDirectoryStructure() ... Sub ListFilesInDirectory(Directory As String) ... Dim aDirsAs String, iDir As Integer, stFile As String ...
    (microsoft.public.excel.programming)