Author Topic: VBScript to delete folders and files  (Read 5018 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
VBScript to delete folders and files
« on: December 02, 2008, 12:14:53 PM »
All,

I found this code on the net to delete files and folders and modified it a bit to suit my needs. My question is, how can I turn it into a function that I can feed it a path and tell it how deep to delete the folders?

When I try to feed fldname a path i get an error "Expected literal constant"

Code: [Select]
Function deletefilesandfolders (foldername, deep)
const fldname = foldername
....
If UBound(Split (folder.path, "\")) > deep Then
....
End Function

deletefilesandfolders ("C:\Documents and Settings\%username%\Desktop\testdel2", 5)

Code: [Select]
const fldname = "D:\Garbage"
set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder(fldname)
recurse fldr 
sub recurse(byref fldr)
dim subfolders,files,folder,file
set subfolders = fldr.subfolders
set files = fldr.files
for each file in files
on error resume next
file.Delete
next 
for each folder in subfolders
recurse folder
If UBound(Split (folder.path, "\")) > 2 Then
    folder.Delete
End If
next   
set subfolders = nothing
set files = nothing
end Sub

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: VBScript to delete folders and files
« Reply #1 on: December 02, 2008, 01:28:38 PM »
was is fldname a constant?

Try making it a variable:
...
dim fldname as string
fldname = foldername
...

ronjonp

  • Needs a day job
  • Posts: 7529
Re: VBScript to delete folders and files
« Reply #2 on: December 02, 2008, 02:11:43 PM »
That gives me a syntax error...I think the recursiveness is what is getting broken... :?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: VBScript to delete folders and files
« Reply #3 on: December 02, 2008, 02:36:21 PM »
are trying to delete the folder given and all subfolders under that?  could it be your feeding it 5 and there are 6 deep, or is it 0 based and you said 5 when you should have said 4
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ronjonp

  • Needs a day job
  • Posts: 7529
Re: VBScript to delete folders and files
« Reply #4 on: December 02, 2008, 03:11:56 PM »
I'm trying to delete all subfolders except the subs directly under the path specified. It is zero based.

The routine works fine when run standalone...it's when I try to turn it into a function that can accept the path and nested number as a variable where it bonks out?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: VBScript to delete folders and files
« Reply #5 on: December 02, 2008, 04:15:48 PM »
I got it working  :-)

Code: [Select]
Function deletefilesandfolders (foldername, deep)
dim fso,fldr
set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder(foldername)
deep = UBound(Split (foldername, "\")) + deep
recurse fldr, deep
Set fso = Nothing
Set fldr = Nothing
End Function

Sub recurse(byref fldr, deep)
dim subfolders,files,folder,file
set subfolders = fldr.subfolders
set files = fldr.files
for each file in files
on error resume next
file.Delete
next 
for each folder in subfolders
recurse folder, deep
If UBound(Split (folder.path, "\")) > deep Then
    folder.Delete
End If
next   
set subfolders = nothing
set files = nothing
End Sub

deletefilesandfolders "d:\garbage", 0 '0==delete all subfolders . 1==leave subfolders 1 deep . 2==leave subfolders 2 deep...and so on...

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC