Re: Dateien und Folder löschen

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



Hi there,

Karl Hass wrote:
> Hallo,
> ich suche ein Programm oder Script was in einem Folder alle Daten und
> Unterfolder löscht.
> Der root folder soll aber nicht gelöscht werden.
>

anbei ein Codebsp. von Michael Harris.
[code]
set fso = createobject("scripting.filesystemobject")

'init an empty array (ubound will be -1)...
'
'we use an array and store the file objects.
'this avoids any problems with altering the
'contents of the Files collections while they
'are being iterated.
'
arFiles = array()
count = -1

'get the path to the temp folder
'
Dir = fso.GetFolder("C:\Test")


'load the (global scope) arFiles
'SelectFiles calls itself recursively
'for SubFolders
'
SelectFiles Dir


'now do the actual deletes. the error trap
'is in case any are in-use...
'
dcount = 0
for each file in arFiles
s = s & file.name & vbcrlf
on error resume next
file.delete true
if err.number = 0 then dcount = dcount + 1
err.clear
on error goto 0
next


'now go back and delete empty folders
'below the temp folder


DeleteEmptyFolders Dir, False

'comment out if wou want "silent" operation,
'or add support for a "/s" command-line switch.
'
msgbox count+1 & " files found, " & dcount & " deleted."

sub SelectFiles(sPath)


'select files to delete and add to array...
'
set folder = fso.getfolder(sPath)
set files = folder.files


for each file in files
count = count + 1
redim preserve arFiles(count)
set arFiles(count) = file
next


for each fldr in folder.subfolders
SelectFiles fldr.path
next


end sub


sub DeleteEmptyFolders(sPath,bDeleteThisFolder)


set folder = fso.getfolder(sPath)


'recurse first...
'
for each fldr in folder.subfolders
DeleteEmptyFolders fldr.path,true
next


'if no files or folders then delete...
'
'bDeleteThisFolder is false for
'the root of the subtree, and true for
'sub-folders (unless you want to delete
'the entire subtree if it is empty).
'
if (folder.files.count = 0) and _
(folder.subfolders.count) = 0 and _
bDeleteThisFolder then
folder.delete
exit sub
end if


end sub
[/code]

Gruß
Christoph


.



Relevant Pages

  • Classes and Arrays
    ... have created a class called "record" to store information about a folder and ... The Perms property is a dynamic array as per ... Each element in the Perms array should be another class called PermRec. ... Private Sub Class_Initialize ...
    (microsoft.public.scripting.vbscript)
  • Re: Excel temporary files
    ... but temp files get left. ... 'we use an array and store the file objects as this avoids any problems ... 'now go back and delete empty folders below the temp folder ... Sub SelectFiles ...
    (microsoft.public.excel)
  • RE: Cycle through all folders under Mailbox
    ... The macro seems to crash on the first folder it goes to, ... Public Sub ProcessAllFolders() ... Dim fld As MAPIFolder ... Dim subfld As MAPIFolder ...
    (microsoft.public.outlook.program_vba)
  • *** TOUGH ONE *** Posting Form Results - using multiple selection boxes (ASP)
    ... I was changing the wrong files in the wrong folder. ... directory and the "_fpclass" folder that should be visible ... in the current web - folder view of FrontPage 2003. ... Sub FP_SetLocaleForPage ...
    (microsoft.public.frontpage.programming)
  • RE: Collect Info from Wkbks in a Folder with Criteria to 1 sheet.
    ... MsgBox ("Cannot open folder - Exiting Macro") ... Exit Sub ... Can you set a filter mechanism so that if ANY files in TEST FOLDER is ...
    (microsoft.public.excel.programming)