RE: Batch File/Script to Recursively Delete Files and Folders in Subdi
- From: "bruce cartland" <bruce.cartland.REMOVE@xxxxxxxxxxx>
- Date: Thu, 23 Jun 2005 21:49:07 -0700
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...
>
.
- References:
- Prev by Date: Accessing WScript.Arguments.Named duplicate arguments
- Next by Date: Hwo can I get the job id from within a script?
- Previous by thread: Batch File/Script to Recursively Delete Files and Folders in Subdirectories
- Next by thread: Accessing WScript.Arguments.Named duplicate arguments
- Index(es):
Relevant Pages
|