Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Workaround for using wildcard "*" with FileExists method

Reply
 
Old 08-15-2008   #1 (permalink)
Highlander


 
 

Workaround for using wildcard "*" with FileExists method

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

My System SpecsSystem Spec
Old 08-15-2008   #2 (permalink)
Tom Lavedas


 
 

Re: Workaround for using wildcard "*" with FileExists method

On Aug 15, 1:51*pm, Highlander <tron9...@xxxxxx> wrote:
Quote:

> 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
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
===========
My System SpecsSystem Spec
Old 08-15-2008   #3 (permalink)
mayayana


 
 

Re: Workaround for using wildcard "*" with FileExists method

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
Quote:

>
> 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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Seeking a safe workaround for "An unknown program wants access to your computer" System Security
Windows Contacts "last name" bug, and a stange workaround Vista mail
Action Pack "upgrade" install workaround.... Vista General
Help wanted: Vistas "Windows Mail" and older SSL encryption method Vista mail
My downloaded Vista with OEM emulation workaround, passed WGA and is considered "legit" !! Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46