Re: Recursivly deleting a list of files

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: BerkHolz, Steven (spamtrap_at_Astrumtech.com)
Date: 04/08/04


Date: Thu, 8 Apr 2004 17:25:55 -0400

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
>
>


Relevant Pages

  • Re: Execute macro for all documents in the folder
    ... It is simple enough to batch process a single folder. ... Dim strFileName As String ... It seems as though the below macro from an earlier posting was put ...
    (microsoft.public.word.docmanagement)
  • Re: Execute macro for all documents in the folder
    ... It is simple enough to batch process a single folder. ... Dim strFileName As String ... It seems as though the below macro from an earlier posting was put ...
    (microsoft.public.word.docmanagement)
  • Re: HTML Item properties vs. Regular item properties
    ... I don't use the MSN provider, but AFAIK the date in the default store's is ... OutlookSpy - Outlook, CDO ... as well as the Sent Items folder on the MSN server. ... Dim app As Outlook.Application ...
    (microsoft.public.outlook.program_vba)
  • RE: If Statement help
    ... Dim newbk As Variant ... Right now the macro takes all the files in the current folder, ... 'This is very useful in quickly archiving and storing daily batch files ... Dim objFile As File, strSourceFolder As String, strDestFolder As String ...
    (microsoft.public.excel.programming)
  • RE: If Statement help
    ... Dim newbk As Variant ... Right now the macro takes all the files in the current folder, ... 'This is very useful in quickly archiving and storing daily batch files ... Dim objFile As File, strSourceFolder As String, strDestFolder As String ...
    (microsoft.public.excel.programming)