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