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

Workaround for using wildcard "*" with FileExists method

4,792 views
Skip to first unread message

Highlander

unread,
Aug 15, 2008, 1:51:14 PM8/15/08
to
Hello all.

I want to delete all files in a folder that have a .txt extension.

This does NOT work:

Set objFSO = CreateObject("Scripting.FileSystemObject")
IF objFSO.FileExists("D:\Test\*.txt") Then
objFSO.DeleteFile("D:\Test\*.txt")
End IF
Set objFSO = nothing

This workaround does work:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
sCMD = "%COMSPEC% /c DIR /B D:\Test\*.txt"
Set objWshScriptExec = objShell.Exec(sCMD)
Set objStdOut = objWshScriptExec.StdOut
Do Until objStdOut.AtEndOfStream
sLine = objStdOut.ReadLine
IF Instr(sLine,".txt") Then
sFilepath = "D:\Test\" & sLine
objFSO.DeleteFile(sFilepath)
End IF
Loop
Set objFSO = nothing
Set objShell = nothing
Set objWshScriptExec = nothing
Set objStdOut = nothing

My question is if there's any way to streamline and shorten my
workaround script.

Any help would be greatly appreciated. Thanks!

- Dave

Tom Lavedas

unread,
Aug 15, 2008, 2:20:17 PM8/15/08
to

How about this ...

Set objShell = CreateObject("WScript.Shell")

sCMD = "%COMSPEC% /c del D:\Test\*.txt"
objShell.Run sCMD, 0, True
Set objShell = nothing

or even ...

sCMD = "%COMSPEC% /c del D:\Test\*.txt"
CreateObject("WScript.Shell").Run sCMD, 0, True

Tom Lavedas
===========

mayayana

unread,
Aug 15, 2008, 2:25:08 PM8/15/08
to
Dim FSO, oFol, oFils, oFil
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFol = FSO.GetFolder("D:\Test")
Set oFils = oFol.Files
For Each oFil in oFils
If UCase(right(oFil.Name, 4)) = ".TXT" Then oFil.Delete
Next
Set oFils = Nothing
Set oFol = Nothing
Set FSO = Nothing
0 new messages