Re: Dateien und Folder löschen
- From: "Christoph Fricke" <*NoSpam#christoph.fricke@xxxxxx>
- Date: Fri, 13 Jan 2006 14:02:54 +0100
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
.
- Follow-Ups:
- Re: Dateien und Folder löschen
- From: Karl Hass
- Re: Dateien und Folder löschen
- References:
- Dateien und Folder löschen
- From: Karl Hass
- Dateien und Folder löschen
- Prev by Date: Re: spielt keine Rolle [seriell oder parallel]
- Next by Date: Re: Dateien und Folder löschen
- Previous by thread: Dateien und Folder löschen
- Next by thread: Re: Dateien und Folder löschen
- Index(es):
Relevant Pages
|