Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to delet unwanted files and folders

3 views
Skip to first unread message

w123

unread,
Jul 11, 2005, 4:45:34 PM7/11/05
to
Hi Group,

I'm new to VBS. I'd appreciate if you give me some pointers to achieve
the following:

Search a folder recursively to delete all the files and folders NOT
named *.adb.

Thanks,
Kerr

Ray Costanzo [MVP]

unread,
Jul 11, 2005, 9:18:29 PM7/11/05
to
Which part are you stuck on? How do delete a file? How to delete a folder?
How to tell if a file name or a folder name contains certain text? How to
recursively traverse the file system?

Ray at home

"w123" <kerr...@abbott.com> wrote in message
news:1121114734.5...@f14g2000cwb.googlegroups.com...

w123

unread,
Jul 12, 2005, 10:19:01 AM7/12/05
to
I need to do for the following:

1. search a folder and recursively find all the files and folders that
is NOT *.abs
2. delete all those files and folders

The end result will be cleaned up folder with only the *.abs files
left.

I need help with 1.

Thanks,
w123

Michael Harris (MVP)

unread,
Jul 12, 2005, 8:40:22 PM7/12/05
to

2. Michael Harris \(MVP\) Mar 6 2004, 4:08 pm show options

Newsgroups: microsoft.public.scripting.wsh
From: "Michael Harris \(MVP\)" <mikhar at mvps dot org> - Find messages by
this author
Date: Sat, 6 Mar 2004 13:06:46 -0800
Local: Sat,Mar 6 2004 4:06 pm
Subject: Re: Renaming Files
Reply to Author | Forward | Print | Individual Message | Show original |
Report Abuse

> I have hundreds of files that have an underscore in the file name. I
> need to rename the files removing the underscore and replacing it
> with a space. Like this "file_file_file.txt" to "file file file.txt"
> Can I do this with a batch fileor windows script? If not can someone
> suggest a way this can be done.


'===========================================================
' DemoRecursiveFileSearch.vbs
'
' Demonstrates recursive file system search. This
' example searches the folder tree defined by the
' RootPath variable.
'
' In this example, RootPath = "C:\Program Files"
'
' SearchFolders takes a folder object argument and
' walks the Files collection of the folder, passing
' each file object to a SelectFile routine and then
' walks the SubFolders collection of the folder,
' calling itself recursively for each subfolder.
'
' The SelectFile routine applies whatever selection
' criteria is appropriate and adds the file object
' to a global SelectedFiles collection (dictionary).
'
' In this example, files with an extension other than "abs"
' are selected.
'
' On return to the mainline script, the SelectedFiles
' collection is walked and each selected file is
' passed to the ProcessFile routine.
'
' In this example, the full path of each file is
' simply echoed to the console window.
'
'===========================================================
' To adapt this example...
'
' * change the RootPath variable value
' * change the SelectFile selection logic
' * change the ProcessFile processing logic
'
'===========================================================
'
'================================== begin mainline
'
Option Explicit

Dim RootPath
RootPath = "C:\Your Path\To Search"

Dim SelectedFiles
Set SelectedFiles = CreateObject("Scripting.Dictionary")

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim RootFolder
Set RootFolder = fso.GetFolder(RootPath)

Call SearchFolders(RootFolder)

WScript.Echo SelectedFiles.Count & " files selected..."

Dim SelectedFile
For Each SelectedFile In SelectedFiles.Items
Call ProcessFile(SelectedFile)
Next

WScript.Echo "Done..."
WScript.Quit
'
'================================== End mainline

'================================== Begin procedures
'
'================================== ProcessFile
'
Sub ProcessFile(argFile)
' echos the file path...
'
' add logic to apply whatever process needed.
'
' e.g., argFile.Delete
'
WScript.Echo argFile.Path

End Sub


'================================== SelectFile
'
Sub SelectFile(argFile)

' echos the file path...
'
' add file to SelectedFiles based on your criteria,
' in this case any file without an "abs" extension...

If LCase(fso.GetExtensionName(argFile.Name, <> "abs") Then
Set SelectedFiles(argFile.Path) = argFile
End If

End Sub

'================================== SearchFolders
'
Sub SearchFolders(argFolder)
Dim file, subfolder

For Each file In argFolder.Files
Call SelectFile(file)
Next

For Each subfolder In argFolder.SubFolders
Call SearchFolders(subfolder)
Next

End Sub
'
'================================== End procedures


--
Michael Harris
Microsoft MVP Scripting


Andreas Kaestner

unread,
Jul 14, 2005, 9:45:46 PM7/14/05
to
Michael Harris (MVP) (mikhar at mvps dot org) schrieb/wrote:

> '================================== SelectFile
> '
> Sub SelectFile(argFile)
>
> ' echos the file path...
> '
> ' add file to SelectedFiles based on your criteria,
> ' in this case any file without an "abs" extension...
>
> If LCase(fso.GetExtensionName(argFile.Name, <> "abs") Then
> Set SelectedFiles(argFile.Path) = argFile
> End If
>
> End Sub

I guess

If LCase(fso.GetExtensionName(argFile.Name)) <> "abs" Then

will work better. :)

--
Gruß, | Bitte in der NG antworten | Win98-Tipps:
Regards, Andreas | Please reply to the NG | http://a-kaestner.de
==================*===========================*=======================72
OE-QuoteFix 1.19.2: http://home.in.tum.de/~jain/software/oe-quotefix/

Michael Harris (MVP)

unread,
Jul 16, 2005, 3:48:32 PM7/16/05
to
>
> I guess
>
> If LCase(fso.GetExtensionName(argFile.Name)) <> "abs" Then
>
> will work better. :)


;-)...

Real code always works better than on the fly air code...

0 new messages