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