Re: Recursivly deleting a list of files
From: BerkHolz, Steven (spamtrap_at_Astrumtech.com)
Date: 04/09/04
- Next message: Marty List: "Re: Permanently setting environmental variables"
- Previous message: Andy: "Re: VB Script Example"
- In reply to: BerkHolz, Steven: "Re: Recursivly deleting a list of files"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 9 Apr 2004 15:01:25 -0400
OK,
It works.
But I still need to figure out the array. (never used arrays yet)
"If pFile.name = "dummytest.exe" Then" works to decide what to do, but right
now I would need to repeat that section 400 times for each file name.
I assume an array may be faster. Please let me know if it would not be
faster.
If I read a file into an array, then do something like "If pFile.name is in
array1" , would that be faster? (not sure of the syntax)
Also, the list could be external or a list at the top of the vbs file.
Questions:
1: How would I read a list into an array? (internal or external)
2: How would I match the filename to a name in that list?
----------------------------------------------------------------
dNow = Now
'Only the Time portion
sTimeStamp = Right("0" & CStr(DatePart("h", dNow)), 2) & ":" & _
Right("0" & CStr(DatePart("n", dNow)), 2) & ":" & _
Right("0" & CStr(DatePart("s", dNow)), 2)
sDateTimeStamp = CStr(DatePart("yyyy", dNow)) & "-" & _
Right("0" & CStr(DatePart("m", dNow)),2) & "-" & _
Right("0" & CStr(DatePart("d", dNow)), 2)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolderBase = oFSO.GetFolder("c:\")
Set ts = oFSO.CreateTextFile("deleteexelist" & sDateTimeStamp & "-" &
Right("0" & CStr(DatePart("h", dNow)), 2) & Right("0" & CStr(DatePart("n",
dNow)), 2) & ".log", 1)
ts.writeline oFile & " "
ts.writeline oFile & sDateTimeStamp & " " & sTimeStamp
sLead = ""
aiCurrDepth = 1
RecurseDepth = 20
CRLF = Chr(13) & Chr(10)
iNumDirscount = 0
DeletedFilescount = 0
' ts.writeline oFile & "** Base Folder is " & oFolderBase & " **"
' ts.writeline oFile & "** Removing Files **"
' ts.writeline oFile & " "
iNumDirs = ParseForSubFolders(oFolderBase,RecurseDepth,1)
ts.writeline oFile & " "
ts.writeline oFile & "** " & iNumDirscount & " Total SubDirectories ** (Max
Depth = " & RecurseDepth & ")"
ts.writeline oFile & "** " &DeletedFilescount & " Files Deleted **"
ts.writeline oFile & " "
ts.writeline oFile & sDateTimeStamp & " " & sTimeStamp
' Sub to Parse File Dates
'
Function ParseForSubFolders(ByVal asCurrFolder,ByVal aiMaxDepth,ByVal
aiCurrDepth)
' ts.writeline oFile & "ParseForSubFolders instance"
If aiCurrDepth = 1 Then
' ts.writeline oFile & asCurrFolder & aiCurrDepth
End If
for i = 1 to aiCurrDepth
sDepthString = sDepthString + sLead
next
Set oFolderBase=oFSO.GetFolder(asCurrFolder)
set oSubFolders = oFolderBase.SubFolders
'Print subfolders of the current folder
For Each fldSubFolder In oSubFolders
on error resume Next
' ts.writeline oFile & "For Each Sub instance"
ParseFiles
iNumDirs = iNumDirs + 1
sSubDirName = fldSubFolder.Name
' ts.writeline oFile & sDepthString & " " & sSubDirName
if aiCurrDepth < aiMaxDepth Then
sNewPath = oFSO.BuildPath(asCurrFolder,sSubDirName)
'Recursion happens here
'Print subfolders for this subfolder
'ts.writeline oFile & sNewPath & " " & aiMaxDepth & " " & aiCurrDepth+1
iNumSubDirs = ParseForSubFolders(sNewPath,aiMaxDepth,aiCurrDepth+1)
End If
Next
End Function
' Sub to Parse File Dates
'
Sub ParseFiles()
Set pFiles = oFolderBase.Files
iNumDirscount = iNumDirscount + 1
' ts.writeline oFile & "ParseFiles instance" & iNumDirscount
' ts.writeline oFile & oFolderBase & CRLF
For Each pFile In pFiles
on error resume Next
' ts.writeline oFile & "For Each File instance"
extension = MID(pFile.name, InstrRev(pFile.name, ".") + 1)
on error resume Next
If extension = "mp3" Then
DeletedFilescount = DeletedFilescount + 1
ts.writeline oFile & pFile & " " & pFile.DateCreated & " " & "Delete = true"
'pFile.Delete True
End If
If pFile.name = "dummytest.exe" Then
DeletedFilescount = DeletedFilescount + 1
ts.writeline oFile & pFile & " " & pFile.DateCreated & " " & "Delete = true"
'pFile.Delete True
End if
Next
End Sub
------------------------------------------------------------
--
Steven BerkHolz
Send to Domain TESCOGroup dot com, username SB
Note: you may also want to know that you should never send mail to:
blacklist-my-ip@admins.ws
info@dautrap.uceprotect.net
listme@sorbs.net
spamtrap@sandes.dk
spamtrap@stop.mail-abuse.org
spamtrap@frankenbiker.de
spamtrap@blars.org
"BerkHolz, Steven" <spamtrap@Astrumtech.com> wrote in message
news:c562eg$2onrm2$1@ID-75105.news.uni-berlin.de...
> Thanks Mark,
>
> I will give it a shot.
>
> --
> Steven BerkHolz
> Send to Domain TESCOGroup dot com, username SB
>
> Note: you may also want to know that you should never send mail to:
> blacklist-my-ip@admins.ws
> info@dautrap.uceprotect.net
> listme@sorbs.net
> spamtrap@sandes.dk
> spamtrap@stop.mail-abuse.org
> spamtrap@frankenbiker.de
> spamtrap@blars.org
> "Mark Lockett" <anonymous@discussions.microsoft.com> wrote in message
> news:16ffe01c41d22$28954020$a601280a@phx.gbl...
> > Hi
> > I hope this helps:
> > I have a script to delete old files in a folder (and all
> > subfolders). It looks at all files in a folder and then
> > calls itself for all folders within the folder: This
> > technique works well. If you can edit it to create an
> > array of all the filenames you want to delete (and
> > populate that array from the contents of a text file), and
> > then delete the files which have a name in the array,
> > instead of just any file which is "old", you would have
> > what you are after.
> > here it is :
> >
> > ' DelUnusd.vbs ' deletes old files and folders from the
> > subtree of a folder,
> > ' ' which have not been used
> > recently
> >
> > ' Usage: C:\> cscript DelUnusd.vbs Folder days
> > [textfile] [No[Delete]
> > ' folder: first argument is the name of the
> > folder to display
> > ' days second argument is number of days
> > before present to force deletion
> > ' textfile: third argument is the name of the file to
> > store the output
> > ' (if missing or blank,
> > displays to screen)
> > ' Delete optional fourth argument
> > specifies: actually carry out the deletions
> > ' otherwise only write a
> > file of possible deletions
> > ' Batch | Interactive optional argument specifies:
> > Interactive (default) displays screen messages
> > ' Batch: suppresses all user
> > interface info
> >
> > ' for some reason it does not work if the folder includes
> > the Windows\System folder
> > ' and Winnt\system or winnt\system32 as well ????
> >
> > Option explicit
> > Public intCounter, TextStreamForOutput, tfScreen,
> > tfDelete, dateDeleteCutoff
> > Public tfInteractive
> > intCounter = 0
> >
> > Function searchfolder(folderspec) ' browses
> > all folders in the folderspec
> > dim fldrsFolderlist, fldr
> > dim fso
> > dim strMessage
> > dim fldrRoot
> > dim fileFile
> > dim filesFilelist
> > set fso = CreateObject
> > ("Scripting.FileSystemObject")
> > set fldrRoot = fso.GetFolder(folderspec)
> > set fldrsFolderlist = fldrRoot.SubFolders
> > set filesFilelist = fldrRoot.Files
> > for each fldr in fldrsFolderlist '
> > for each folder in the targer folder
> > processfolder(fldr) '
> > display folder information
> > searchfolder(fldr.Path) ' recurse
> > to subfolders of this folder
> > Next
> > if (filesFilelist.Count + fldrsFolderlist.Count) =
> > 0 then fldrRoot.Delete
> > ' delete now empty folders!
> > searchfolder = intCounter & " Folders: " &
> > folderspec
> > End function
> >
> > function processfolder(fldr) ' display folder,
> > and its files
> >
> > dim oktodelete
> > intCounter = intCounter + 1
> > if tfScreen then
> > if tfInteractive then
> > wscript.echo (intCounter & " " &
> > fldr.Path)
> > end if
> > else
> > TextStreamForOutput.Write(intCounter & " "
> > & fldr.DateCreated )
> > TextStreamForOutput.Write(" " &
> > fldr.DateLastAccessed)
> > TextStreamForOutput.Write(" " &
> > fldr.DateLastModified)
> > TextStreamForOutput.Writeline(" " &
> > fldr.Path)
> > end if
> >
> >
> > dim fileFile
> > dim filesFilelist
> > set filesFilelist = fldr.Files
> > for each fileFile in filesFilelist
> > if tfScreen then
> > if tfInteractive then
> > wscript.echo (" " &
> > fileFile.Name)
> > end if
> > else
> > okToDelete = True
> > if fileFile.DateCreated >
> > DateDeleteCutoff then
> > okToDelete = False
> > elseif fileFile.DateLastAccessed >
> > DateDeleteCutoff then
> > okToDelete = False
> > elseif fileFile.DateLastModified >
> > DateDeleteCutoff then
> > okToDelete = False
> > end if
> >
> > if okToDelete then
> >
> > TextStreamForOutput.WriteLine(" " &
> > fso.BuildPath(fldr.Path, fileFile.Name))
> > if tfDelete then
> > fileFile.Delete
> > end if
> >
> >
> > 'TextStreamForOutput.Write(" "
> > & fileFile.DateCreated )
> > 'TextStreamForOutput.Write(" " &
> > fileFile.DateLastAccessed)
> > 'TextStreamForOutput.Write(" " &
> > fileFile.DateLastModified)
> > 'TextStreamForOutput.WriteLine(" "
> > & fileFile.Name)
> > end if
> > next
> >
> > end function
> >
> > dim objArgs, fso, fldr ' start
> > processing: examine command-line arguments
> > Set objArgs = WScript.Arguments
> > set fso = CreateObject("Scripting.FileSystemObject")
> >
> > if fso.FolderExists(objArgs(0)) and objArgs.Count >= 2 then
> >
> > if objArgs(1) >= 0 and objArgs(1) <= 1000 then '
> > between yesterday and three years ago!
> > dateDeleteCutoff = Now - objArgs(1)
> > if objArgs.Count >= 3 then ' at least 3
> > arguments: third argument is file for output
> > if trim(objArgs(2)) = "" then
> > tfScreen = True
> > else
> > tfScreen = False
> > Set TextStreamForOutput =
> > fso.CreateTextFile(objArgs(2), True)
> > end if
> > else
> > ' no third argument: output to screen only
> > tfScreen = True
> > end if
> > tfDelete = False '
> > default
> > tfInteractive = True '
> > default
> > if objArgs.Count >= 4 then ' at least 4
> > arguments: optional fourth argument is Delete
> > ' optional fifth
> > argument is Batch | Interactive
> > if ucase(trim(objArgs(3))) = "DELETE" then
> > tfDelete = True
> > end if
> > if ucase(trim(objArgs(3))) = "BATCH" then
> > tfInteractive = False
> > end if
> > if ucase(trim(objArgs(3))) = "INTERACTIVE"
> > then
> > tfInteractive = True
> > end if
> > end if
> > if objArgs.Count >= 5 then ' at least 4
> > arguments: optional fourth argument is Delete
> > ' optional fifth
> > argument is Batch | Interactive
> > if ucase(trim(objArgs(4))) = "DELETE" then
> > tfDelete = True
> > end if
> > if ucase(trim(objArgs(4))) = "BATCH" then
> > tfInteractive = False
> > end if
> > if ucase(trim(objArgs(4))) = "INTERACTIVE"
> > then
> > tfInteractive = True
> > end if
> > end if
> >
> >
> >
> > set fldr = fso.GetFolder(objArgs
> > (0)) ' we know it exists!
> >
> > processfolder
> > fldr ' display the
> > starting folder
> > if tfInteractive then
> > MsgBox(searchfolder(objArgs(0))) '
> > and search subfolders
> > end if
> > else
> > MsgBox("No Valid Number of Days
> > Specified: " & objArgs(1))
> > end if
> > else
> > MsgBox("No Valid Folder Specified: " & objArgs(0))
> > end if
> >
> >
>
>
- Next message: Marty List: "Re: Permanently setting environmental variables"
- Previous message: Andy: "Re: VB Script Example"
- In reply to: BerkHolz, Steven: "Re: Recursivly deleting a list of files"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|