Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I'm very new to scripting. I was wondering if someone could help me with a script to delete all subfolders and files within a specified folder. I have a script below which is made up of two scripts that I took from a couple of people. It works, but I have lines in there that are not necessary because I do not need to save anything from the folder. The first script erases files from the specified folder, but does not erase the subfolders or files within those subfolders. The second part erases the subfolders and files within those, but not any files that are within the specified folder.
Set fso=CreateObject("Scripting.FileSystemObject")
CleanPath="C:\Test\"For Each file In fso.GetFolder(CleanPath).Files
file.attributes = file.attributes And Not 1
file.delete
Next---------------End first Part---------------
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder("C:\Test\")arrFolders = Array()
For Each oFolder In oFolder.SubFolders
' Note : Only use *lowercase* letters in the folder names below:
If Not LCase(oFolder.Name) = "foldera" _
And Not LCase(oFolder.Name) = "folderb" _
And Not LCase(oFolder.Name) = "folderc" Then
intCount = UBound(arrFolders) + 1
ReDim Preserve arrFolders(intCount)
arrFolders(intCount) = oFolder.Path
End If
NextFor n = 0 To UBound(arrFolders)
fso.DeleteFolder arrFolders(n), True
Next---------------
Can someone help refine this script so that it is neater. The person the script was originally created for wanted to keep certain subfolders. Since I don't, I don't need those extra lines of code.
Thanks
Spike

You cannot delete all "contents" of a folder in one easy step - at least not directly. The easiest way to accomplish what you are asking is to delete the target folder, thereby deleting all of it's contents, and then recreate the folder.
========BEGIN CODE==========
target = "C:\Test"Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(target) Then
fso.DeleteFolder target
fso.CreateFolder target
End If
========BEGIN CODE==========Michael J

I use w98 deltree.exe in w2k.
deltree /y somedir
[That's not what you asked, but MUCH simpler.]
If at first you don't succeed, you're about average.M2

I use rmdir /s
@echo off
rmdir /s /q somedir
mkdir somedirThat removes all subfolders, files, and the directory itself.
I don't see the point in writing lines and lines of code in VBSript when you can accomplish the same with 2 line of Batch scripting.
There's no place like 127.0.0.1
Visit my site Back Slash

Hi Tony,
I'm with you on 'simpler is better.
I wrote this, but was hampered by them old DOS habits. LOL
::== d2.bat
for /f %%D in ('dir/s/b/ad ^| sort /r') do (
del %%D /q
rd %%D
)
:: DONE
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |