Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Better way to delete wildcard files

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,50810
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Better way to delete wildcard files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Better way to delete wildcard files - 8/16/2007 5:58:42 AM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
I'm being lazy, but wonder if someone has a better idea. I need to clean up wilcard files (tmp*.cat adn sp2.cat) from a directory. This is what I have, but I don't like it.


  If objFSO.FolderExists("C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}")
      colFiles = objFSO.GetFolder("C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}").Files
      For each sFile in colFiles
          arrF = split(sFile, ".") 'get extension
          If lcase(arrFName(uBound(arrF))) = "cat" Then
              If Instr(sFile.Name, "tmp") > 1 AND Instr(sFile.Name, "tmp") < 4 Then
                  objFSO.DeleteFile(sFile,true)
              ElseIf Instr(sFile.Name, "sp2") > 1 AND Instr(sFile.Name, "sp2") < 4 Then
                  objFSO.DeleteFile("sp2.cat",true)
              End If
          End If
      Next
  End If



< Message edited by CondoPC -- 8/16/2007 8:23:27 AM >
 
 
Post #: 1
 
 RE: Better way to delete wildcard files - 8/16/2007 6:40:55 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
I'd use a regular expression to test for the various extensions.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to CondoPC)
 
 
Post #: 2
 
 RE: Better way to delete wildcard files - 8/16/2007 7:13:01 AM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
so the regular expressions would be?


Dim RegExExt : Set RegEx = New RegExp
' specify the extension you want to search for; seperate with a |

RegEx.Pattern =
"\.(cat)$"

RegEx.IgnoreCase =
True
 



Dim
RegExFile

' specify the extension you want to search for; seperate with a |

RegEx.Pattern =
"(^tmp|^sp2)" 'or would it be "^(tmp|sp2)"

RegEx.IgnoreCase =
True


< Message edited by CondoPC -- 8/16/2007 7:22:35 AM >

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Better way to delete wildcard files - 8/16/2007 7:27:22 AM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
I don't know if it's better, but here it is. I think it has more possibilities for expanded use.

   Dim RegExExt : Set RegEx = New RegExp
   'specify the extension you want to search for; seperate with a |
   RegEx.Pattern = "\.(cat)$"
   RegEx.IgnoreCase = True

   Dim RegExFile
   'specify the start of the filename you want to search for; seperate with a |
   RegEx.Pattern = "^(tmp|sp2)"
   RegEx.IgnoreCase = True

   'test to see if the file matches the extension defined w/ the RegExp
   If objFSO.FolderExists("C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}") Then
       colFiles = objFSO.GetFolder("C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}").Files
       For each sFile in colFiles
           If RegExExt.Test(sFile) Then
               If RegExFile.Test(sFile) Then
                   objFSO.DeleteFile sFile,true
               End If
           End If
       Next
   End If

(in reply to CondoPC)
 
 
Post #: 4
 
 RE: Better way to delete wildcard files - 8/16/2007 7:32:53 AM   
  ehvbs

 

Posts: 2199
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Hi CondoPC,

this may look like I'm harassing you, but that's not my intention. You posted
a lot of good code lately and I think it deserves careful study. Anyway, this time you
explicitely asked for it!

(1) Even if using copy & paste I wouldn't like to have something like

       "C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}"

     more than once in a script! Variables are cheap!

     Dim sFolder : sFolder =
"C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}"
     If objFSO.FolderExists( sFolder ) Then
         Set
colFiles = objFSO.GetFolder( sFolder ).Files

(2) I would be more careful concerning the data(sub)types and the hungarian prefix of
     the variable names: colFiles contains File objects, not string

     For Each oFile In colFiles

(3) Using sFile=oFile as parameter to the Split function will coerce the object to a string
     via the default property of the File object. This is a risky business. Do you know
     whether the Path or the Name is the default property? I don't!

(4) Spliting the Name/Path of a file on "." to get the extension is risky. What would happen
     for the file specification "c:\openoffice.org\anothersubdir\file with no extension"?. Let
     objFSO.GetExtensionName( oFile.Name ) do the dirty work.

(5) To test whether the file name starts with "tmp" I'd use

       If "tmp" = LCase( Left( oFile.Name, 3 ) ) Then

(6) To test
whether the file name exactly equal to with "sp2.cat" I'd use

        If "sp2.cat" = LCase( oFile.Name ) Then

     but never

       If
Instr(sFile.Name, "sp2") > 1 Then

(7) As we got the File object oFile, why not use it to zap itself:

       oFile.Delete True

Lots of nit picking, as usual from the pedantic paranoiac

ehvbs



  

(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: Better way to delete wildcard files - 8/16/2007 7:57:55 AM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
Thanks for the great feedback. I have no tutor and in posting this sample I figured I would be poking the bees nest. My replies:

(1) I know, but this was a quick hit script with a very specific task - I agree that using the variable is better, although cut and paste worked for me. Thanks for catching the missing "Set", I caught it in my debug later, as well as a missing "Then"

(2) I wasn't aware I would be getting back an object. I should review the specs on GetFolder a bit more.

(3) I'm not sure but do you mean this: arrF = split(sFile.Name, ".") 'get extension

(4) figured there was something like this method objFSO.GetExtensionName( oFile.Name ) but was lazy to find it. As for the extension, I was taking the last array value, and knew what files I would be encountering. Proper standard would have been what you stated though.

(5|6) Now your just making me feel stupid. (Not really, I am making me feel this way) I should have known that and was trying to figure out how to get left or right for another path string earlier in the script. Silly why I didn't think of using it here. That kind of solve the regular expressions issue too.

(7) DOH!! Now I feel like Homer.


As for the nitpicking... I have a story for that ... Pounding a nail: old shoe or glass bottle http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx


This is my cleaned up code:

'3. Delete any "tmp*.cat" and "sp2.cat" files in the "C:\Windows\System32\CatRoot\{f*}" folder
    Dim strCatFldr : strCatFldr = "C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}"
  If objFSO.FolderExists(strCatFldr) Then
      Set colFiles = objFSO.GetFolder(strCatFldr).Files
      For each oFile in colFiles
          dim strExt : strExt =
objFSO.GetExtensionName( oFile.Name )
            If lcase(strExt) = "cat" Then
              If left(lcase(oFile.Name), 3) = "tmp" OR lcase(oFile.Name) = "sp2.cat" Then
                  oFile.Delete true
              End If
          End If
      Next
  End If


< Message edited by CondoPC -- 8/16/2007 9:01:35 AM >

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: Better way to delete wildcard files - 8/16/2007 8:22:10 AM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
Also, thanks for the kudos, I don't take it lightly after just parusing the monthly challenge and getting "lost" in the subtypes for variants challenge.

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: Better way to delete wildcard files - 8/16/2007 9:18:48 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
I  haven't tested it fully, but this is what I had in mind.


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to CondoPC)
 
 
Post #: 8
 
 RE: Better way to delete wildcard files - 8/16/2007 12:33:12 PM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
I like it. And actually, after doing the research on reg exressions, I can understand your expression when reading it. I have already finshed and posted a script though. I will keep it in mind for future reference because it is much easier to use than a bunch of "if OR OR AND Then" or "If Then Else"  statements.

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: Better way to delete wildcard files - 8/17/2007 12:55:27 AM   
  ehvbs

 

Posts: 2199
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Hi CondoPC,

thanks for your response, your admirable attitude, and the link!

In general:

It's ok to code for the job at hand and do it quick and a bit dirty
to meet a deadline (No disagreement on (1) and (5)). It's better
than just ok, if you are aware of the shortcuts you take. That's why
I nit picked those points.

It's easy to bash a working script; coming up with something that works
is much harder - and choosing the 'best' method requires experience. Ex-
perience is just time and there is no merit in getting older. So don't feel
bad about learning that you could have done this sub task in another way.

As to the Split: To get the extension it works for 'nice' filenames (re-
gardless to whether oFile=sFile feeds the path or just the filename to
the function). If you use it to get other parts, i.e. the drive, you
may get surprised, depending on what oFile=sFile delivers via its
default property. So Split for file spec parsing is risky; furthermore
it hides the factual problem/task (file spec parsing) by treating it
as a case of string splitting: If you look at the code later, you'll
have to 'translate' "split string on ." to "get the extension".
Therefore I prefere GetExtensionFileName() - no doubt about what happens,
and if it fails on some funny file spec, you can blame Mr. Gates.

Your code:

      For each oFile in colFiles
         dim strExt : strExt = objFSO.GetExtensionName( oFile.Name )
           If lcase(strExt) = "cat" Then
             If left(lcase(oFile.Name), 3) = "tmp" OR lcase(oFile.Name) = "sp2.cat" Then
                 oFile.Delete true
             End If
         End If
      Next

The extension is used just once, so it doesn't deserve (even a cheap)
variable; the LCase( oFile.Name ) is used twice. So:

      For Each oFile In colFiles
          Dim sFiNa : sFiNa = LCase( oFile.Name )
          If "cat" = objFSO.GetExtensionName( sFiNa ) Then
             If "tmp" = Left( sFiNa, 3) OR "sp2.cat" = sFiNa Then
                 oFile.Delete True
             End If
          End If
      Next
     
I admit:

   (1) I didn't test it
  
   (2) I wouldn't have written this code from scratch; without your
       contribution, this - at least I hope - nice piece of code would not
       exist!
      
   (3) How about:   
      
             If "tmp" = Left( sFiNa, 3) Then
                 oFile.Delete True
             Else
                 If "sp2.cat" = sFiNa Then
                    oFile.Delete True
                 End If
             End If
      
       Is that optimization or obfuscation? You see: the song^H^H^H^H coding
       never ends. To hell with deadlines!
            
Thanks again!

ehvbs

(in reply to CondoPC)
 
 
Post #: 10
 
 RE: Better way to delete wildcard files - 8/17/2007 2:03:01 AM   
  CondoPC


Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
Thanks again. It amazes me that yet again I learn something. Putting the string in the evaluation first - never thought of that or would have considered it as something that worked. I always thought you had the compare a defined variable on the left of the equation. After considering though, I don't know why I thought that was a rule, maybe I picked it up as a readability standard.

As for getting rid of the double LCase, I did that in my finished code, just like you did. As for (3), I didn't like that because I was reusing the oFile.Delete. I would use it if I was going to do something different, but since both evals resulted in the same step next, I combined them. Now if I had more evals but same step, I would then consider using RegEx, based on dm_4ever posts.

Thanks again all for the input and I hope someone learns some from my "schooling"


J


PS. I don't know why, but the emoticons on the left didn't all animate, so when I added the one for the group (), I thought it was just putting heads together. Funny, after I relooked at my post I was SURPRISED to see them come together with a heart in the middle - come on, who would ever add that to a post? I have promptly removed it (other in this ps) since it really didn't fit the context. LOL.

< Message edited by CondoPC -- 8/17/2007 2:07:54 AM >

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: Better way to delete wildcard files - 8/20/2007 1:46:17 PM   
  ChadR

 

Posts: 24
Score: 0
Joined: 8/20/2007
Status: offline
If you use WMI you can use the Extension parameter to find and delete all files of a certain extension.  I find this method much simpler and very reliable.

The following example shows how to delete all files *.txt  It also contains a commented replacement objFile.Extension comparison for *.*


      

You can also use this method to delete files containing a certain string of text.  The example below would delete all files *readme*.txt


      

(in reply to CondoPC)
 
 
Post #: 12
 
 RE: Better way to delete wildcard files - 8/21/2007 1:56:53 AM   
  4scriptmoni


Posts: 208
Score: 0
Joined: 5/3/2007
Status: offline
I posted this script other day i did to clean up space on disk C:
I learned few tricks from your maybe i will update my.

http://www.visualbasicscript.com/m_50961/tm.htm

cheers,

_____________________________

Enterprise Microsoft Scripts
Exchange, Login/Logout Monitor,TS, Monitoring, Security, AD, etc...
http://www.felipeferreira.net

(in reply to ChadR)
 
 
Post #: 13
 
 RE: Better way to delete wildcard files - 8/23/2007 1:06:06 AM   
  4scriptmoni


Posts: 208
Score: 0
Joined: 5/3/2007
Status: offline
Interesting thread, I am changing my script and wanted to scan the entire C:\ for a certain extension.
But it is way slow! If I use Regx would it be faster, any sample?

, my example is here:

      

if anyone can give me a regx for deleting an extension? extension should be a variable

< Message edited by 4scriptmoni -- 8/23/2007 1:17:11 AM >


_____________________________

Enterprise Microsoft Scripts
Exchange, Login/Logout Monitor,TS, Monitoring, Security, AD, etc...
http://www.felipeferreira.net

(in reply to CondoPC)
 
 
Post #: 14
 
 RE: Better way to delete wildcard files - 8/23/2007 1:27:45 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You can see an example in the post I made above and in the Frequently Asked Stuff post also.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to 4scriptmoni)
 
 
Post #: 15
 
 RE: Better way to delete wildcard files - 8/23/2007 1:59:16 AM   
  4scriptmoni


Posts: 208
Score: 0
Joined: 5/3/2007
Status: offline
sorry i was being lazy...
i havent tested yet but i got something like
Dim strExt : strExt = "dmp"
Dim RegExExt : Set RegEx = New RegExp
  RegEx.Pattern = "\.(strExt)$"
  RegEx.IgnoreCase = True

  'test to see if the file matches the extension defined w/ the RegExp
  If objFSO.FolderExists(ofolder) Then
      colFiles = objFSO.GetFolder(ofolder).Files
      For each sFile in colFiles
          If RegExExt.Test((right(sFile.name,4)) Then
                wscript.echo "Found: "  & sFile.name
                'objFSO.DeleteFile sFile,true             
          End If
      Next
  End If

(in reply to dm_4ever)
 
 
Post #: 16
 
 RE: Better way to delete wildcard files - 8/23/2007 4:34:05 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
With a regular expression you don't need to do any Left, Right, Mid on the file name...just give it the file name and your pattern will do the work for you...this is the reason prefer it since you can add more extension to test for by simply seperating them with a | and FileSystemObject is several times faster than WMI...especially if you recurse through sub folders.

< Message edited by dm_4ever -- 8/23/2007 4:35:06 AM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to 4scriptmoni)
 
 
Post #: 17
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Better way to delete wildcard files Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts