Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete * Files if they Exist 1

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
US
I acquired code from this site to delete a file:

Code:
Set fso = Wscript.CreateObject("Scripting.FilesystemObject")
fso.DeleteFile "C:\Program Files\HEAT\HFW1*",True

Working with an application called HEAT, that can run automated programs. One of these programs produces a temp file each time it is run. I'd like to clean out the temp files for users by adding a script to the beginning of the program so that all temp files will be deleted prior to the program running.

The temp file names are "HFW9999" where 9999 can be any numbers. The files always start with HFW and they have no extension. So in my example above, this script will delete any file named HFW1...

How can I get it to include all files with any number after the HFW? To add a twist, it can only be numbers, not letters, as there are actually valuable .DLLs that also begin with "HFW" followed by letters.

Also, this script bombs out if the file does not exist...is there a way I can add a "if exists" kind thing so that it will delete the file(s) if they are there, but will not error out if they're not?

Thanks...hope this is not too confusing...
stinsman
 
Browse the Files collection and use the Mid and IsNumeric functions.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
What Files collection? I'm not a programmer, I'm just winging it...
 
You could also try searching this forum. A search of the terms 'Delete Files Folder' produced 55 threads, several of which deal with this topic just from looking at the title.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Code:
Path1 = "C:\Program Files\HEAT"



Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

  Set fso = createobject("Scripting.FileSystemObject")
  
   Set oFolder = fso.GetFolder(Path1)
  
  For Each oFile In oFolder.files
   	If Left(oFile.Name,3)= "HFW" And Right(lcase(oFile.Name,3)) = "tmp" Then
    	oFile.Delete True
    End If
  Next

I hope you find this post helpful.

Regards,

Mark
 
Mark, that's just what I need...had to tweak it a bit because the files do not have a "tmp" extension:

Code:
Path1 = "C:\Stins\DELETE\DEVTEST"

Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

  Set fso = createobject("Scripting.FileSystemObject")
  
   Set oFolder = fso.GetFolder(Path1)
  
  For Each oFile In oFolder.files
       If Left(oFile.Name,3)= "HFW" And not(Right(oFile.Name,3)) = "dll" Then
        oFile.Delete True
    End If
  Next

Thanks a lot!

stinsman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top