vbscript >> Script to delete contents of Folder ( Files and Subfolders)

by Sko » Tue, 23 May 2006 07:02:02 GMT

Hello Guys,

I have Public PC's that need to have a folder cleared out after hours. I
would like to setup a script that deletes the contents ( Files + Subfolders)
of a folder. The folder has special rights assigned to it so i don't want to
script something to delete the folder then re-create it. I cam across a the
following script that works great for deleting the files, but it does not
delete the subfolders The info on the script says you can include the
subfolders by including the bIncludeSubFolders variable, but how do i do that?

Thank you !!!


-------------------- Script -----------------------------------



' folder to start search in...
path = "c:\PublicAccess"

' delete files older than 7 days...
killdate = date() - 1

arFiles = Array()
set fso = createobject("scripting.filesystemobject")

' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true

nDeleted = 0
for n = 0 to ubound(arFiles)
'=================================================
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
'=================================================
on error resume next 'in case of 'in use' files...
arFiles(n).delete true
if err.number <> 0 then
wscript.echo "Unable to delete: " & arFiles(n).path
else
nDeleted = nDeleted + 1
end if
on error goto 0
next

msgbox nDeleted & " of " & ubound(arFiles)+1 _
& " eligible files were deleted"


sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
on error resume next
'select files to delete and add to array...
'
set folder = fso.getfolder(sPath)
set files = folder.files

for each file in files
' uses error trapping around access to the
' Date property just to be safe
'
dtlastmodified = null
on error resume Next
dtlastmodified = file.datelastmodified
on error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
end if
end if
next

if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
next
end if
end sub




vbscript >> Script to delete contents of Folder ( Files and Subfolders)

by McKirahan » Tue, 23 May 2006 11:48:11 GMT


JJ" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
I
Subfolders)
to
the
that?

I found the script you are referring to:

"Looking for script to delete files older than X days"

<URL:
http://groups.google.com/group/microsoft.public.scripting.wsh/browse_frm/thr
ead/62bde02a8324d7bc/9eefe8b4808a3606?lnk=st&q=%22bIncludeSubFolders%22+grou
p%3Amicrosoft.public.*&rnum=3&hl=en#9eefe8b4808a3606>


However, you changed the value but not the comment:

' delete files older than 7 days...
killdate = date() - 1

Don't know if the version you had includes all the bug fixes
mentioned in the link above.


How about this? Like the original, it stores the qualifying
files and folders in an array for subsequent deletion.

Option Explicit
'*
'* Declare Constants
'*
Const cFOL = "C:\PublicAccess"
Const cDAZ = 1 '# days re DateLastModified
'*
'* Declare Variables
'*
Dim intDEL
intDEL = 0
Dim arrFIL()
Dim intFIL
intFIL = 0
Dim strFIL
Dim arrFOL()
Dim intFOL
intFOL = 0
Dim strFOL
Dim intNOT
intNOT = 0
Dim strNOT
strNOT = ""
Dim strNOW
strNOW = Now
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*
'* GetFilesFolders()
'*
Call GetFilesFolders(cFOL)
'*
'* DelFilesFolders()
'*
Call DelFilesFolders()
'*
'* Destroy Objects
'*
Set objFSO = Nothing
'*
'* Finish Message
'*
WScript.Echo intDEL & " files and folders deleted."
WScript.Echo intNOT & " files and folders could not be deleted:" &
strNOT

Sub GetFilesFolders(folder)
'*
'* Get Files
'*
For Each strFIL In objFSO.GetFolder(folder).Files
If DateDiff("d",strFIL.DateLastModified,strNOW) > cDAZ Then
intFIL = intFIL + 1
ReDim Preserve arrFIL(intFIL)
arrFIL(intFIL) = folder & "\" & strFIL.Name
End If
Next
'*
'* Get Subfolders
'*
For Each strFOL In objFSO.GetFolder(folder).SubFolders
If DateDiff("d",strFOL.DateLastModified,strNOW) > cDAZ Then
intFOL = intFOL + 1
ReDim Preserve arrFOL(intFOL)
arrFOL(intFOL) = strFOL.Path
End If
Call GetFilesFolders(strFOL.Path)
Next
End Sub

Sub DelFilesFolders()
'*
'* Delete Files
'*
If intFIL > 0 Then
For intFIL = 1 To UBound(arrFIL)
On Error Resume Next
objFSO.DeleteFile arrFIL(intFIL), True
If Err.Number <> 0 Then
intNOT = intNOT + 1
strNOT = strNOT & vbCrLf & " " & arrFIL(intFIL)
Else
intDEL = intDEL + 1
End If
On Error GoTo 0
Next
End If
'*
'* Delete Folders
'*
If intFOL > 0 Then
For intFOL = 1 To UBound(arrFOL)
On Error Resume Next
objFSO.DeleteFolder arrFOL(intFOL), True
If Err.Number <> 0 Then
intNOT = intNOT + 1
strNOT = strNOT & vbCrLf & " " & arrFOL(intFOL)
Else
intDEL = intDEL + 1
End If
On Error GoTo 0
Next
End If
End Sub





Similar Threads

1. Delete Content of many folders withour deleting folders

2. delete files older than x days from a folder and its subfolder

The script is GREAT!!  Just what I need.  Thanks!!

"Pegasus (MVP)" wrote:

> 
> "Chris" < XXXX@XXXXX.COM > wrote in message 
> news: XXXX@XXXXX.COM ...
> > looking for a script or something will let me schedule it.  Only plan to 
> > do
> > it once a year so might have lots of log files. Each log is less than 2 
> > MB.
> >
> > Thanks.
> 
> Here you go. Make sure to run the script with active=false until you're 
> satisfied that it deletes the right files!
> 
> Const Active  = False
> Const sSource = "d:\My Files"
> Const MaxAge  = 10 'days
> Const Recursive = True
> 
> Checked = 0
> Deleted = 0
> 
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> if active then verb = "Deleting """ Else verb = "Old file: """
> CheckFolder oFSO.GetFolder(sSource)
> 
> WScript.echo
> if Active then verb = " file(s) deleted" Else verb = " file(s) would be deleted"
> WScript.Echo Checked & " file(s) checked, " & Deleted & verb
> 
> Sub CheckFolder (oFldr)
>  For Each oFile In oFldr.Files
>   Checked = Checked + 1
>   If DateDiff("D", oFile.DateLastModified, Now()) > MaxAge Then
>    Deleted = Deleted + 1
>    WScript.Echo verb & oFile.Path & """"
>    If Active Then oFile.Delete
>   End If
>  Next
> 
>  if not Recursive then Exit Sub
>  For Each oSubfolder In oFldr.Subfolders
>   CheckFolder(oSubfolder)
>  Next
> End Sub
> 
> 
> 

3. Delete file in folder/subfolder?

4. How to delete subfolders and files in a folder

Hi,

I need your help.

There is a folder, it contains some subfolders and files.

I need to delete the files created 2days ago in this folder , I also need to 
delete the files in the subfolders, but I do not need to delete the subfolder.

I have looked for the scripts in internet for a long time, but no result was 
found.


5. delete files older than x days from a folder and its subfolders

6. WMI Script for changing attributes of folder, subfolders and files

Hi

I am looking for a way to set the attirbutes of a folder and all of
its subfolders (including all files) from any to Archive

help is greatly apreciated

Regards, Martin

7. Deleting All Files and Folders inside folder without deleting root folder

8. copy text files only from all subfolders within a folder to a another folder

Dear All, 

i'm new in vb scripting and a task was assigned to me so as to creat a vb script that performs the following:

if i have a folder whose path is: "D:\Test"
and it containes more than one sub folder as: "D:\Test\Sub1", "D:\Test\Sub2", etc..

the sub folders containes different types of files

what i want to do is to copy only all text files in all the sub folders under "D:\Test", to a new folder "D:\Text" without copying any of the sub folders.

could any one help?

thanks in advance