Login | |
|
 |
RE: need help to exclude a folder - 4/25/2006 5:28:12 AM
|
|
 |
|
| |
kracksmith
Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
|
Hey, thanks for every efforts in trying resolve my problem. I tried your script but it doesn't work out perfectly for me. Basically can you show me how to exclude certain folder wheather it's by path or by name, all i need to do is exclude maybe 1 or 2 folders. this current script does exclude files. can't i just rename the ofile to ofolder? here is my original formatted VBS Option Explicit Dim fso, sExtToDelete Dim nCount sExtToDelete = array(".gif", ".exe", ".nextext", ".etc") ' sample for deleting files with any of these extensions nCount = 0 Set fso = CreateObject("Scripting.FileSystemObject") 'DirWalk("C:\") ' repeat this subroutine call with a different path to process more paths 'DirWalk("F:\users\data\") ' like this. DirWalk("\\phobos\E$\ee\") ' and/or like this. Wscript.Echo nCount & " files deleted." Sub DirWalk(parmPath) Dim oSubDir, oSubFolder, oFile, n Dim bDeleted On Error Resume Next ' We'll handle any errors ourself, thank you very much Set oSubFolder = fso.getfolder(parmPath) ' NOTE: Cannot reference "oFile.xxx" once the file has been deleted! ' Therefore, bDeleted serves as a flag to prevent that error. ' Once a file has been deleted, the script will avoid referencing it. For Each oFile In oSubFolder.Files ' look in the current dir If Err.Number = 0 Then bDeleted = False ' delete files with specific extensions if found... For n = 0 To UBound(sExtToDelete) If LCase(Right(oFile.Name,Len(sExtToDelete(n)))) = sExtToDelete(n) Then Wscript.Echo "about to delete " & oFile.Path '''uncomment the next line when you are satisfied this script works properly ''' fso.DeleteFile oFile.Path, True nCount = nCount + 1 bDeleted = True Exit For End If Next ' delete specific files if found... If Not bDeleted Then If LCase(oFile.Name) = "acad.err" _ Or LCase(oFile.Name) = "something.else" _ Or LCase(oFile.Name) = "acadstic.dmp" Then Wscript.Echo "about to delete " & oFile.Path '''uncomment the next line when you are satisfied this script works properly ''' fso.DeleteFile oFile.Path, True nCount = nCount + 1 End If Else 'if we got an error, just skip this entry... the Volume Label appears 'as a folder in the root of each volume and is not accessible. Err.Clear End If End If Next For Each oSubDir In oSubFolder.Subfolders DirWalk oSubDir.Path ' recurse the DirWalk sub with the subdir paths Next On Error Goto 0 ' Resume letting system handle errors. End Sub
|
|
| |
|
|
|
 |
RE: need help to exclude a folder - 4/25/2006 8:19:34 AM
|
|
 |
|
| |
ehvbs
Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
|
Hi kracksmith, Thanks for taking the trouble to indent your code. It's much better to understand now. But why don't you use the code tags for long sequences of code? I didn't expect my code to work perfectly for you, because I was not (and am not) sure about what you really want: (a) You said, you want to "exclude certain files" but your code *deletes* some extra files delete specific files if found... If Not bDeleted Then If LCase(oFile.Name) = "acad.err" ... Then fso.DeleteFile oFile.Path, True If you specify ".png" as extension of files to delete - does "exclude" mean you don't want to delete some precious .png files (like "VenusVonMilo.png")? (b) I asked you, whether to "exclude 1 or more directories" should mean (1) exclude a folder and all its subfolders from being processed (like don't delete any files in the whole branch under "DocsAndSettings\boss") (2) don't delete files in a folder to exclude, but look in all its sub(sub*)directories You told me you didn't want to stop the script when reaching a folder to exclude. (c) specifying the set of folders to be excluded isn't easy either: Sometimes a list of full pathes is ok, but if you have a lot of users having "..\dirtypics" folders, keeping a long list of "DocsAndSettings\...\dirtypics" pathes is cumbersome or even impossible if you don't know all user names. That's why my script shows you how to use path or name to specify a folder shows you both ways of handling directories to be excluded ignores the problem of excluding files completely Now lets try to adapt my script to your needs - but before that, I want to say that from my experience programming problems can rarely be solved by "just rename the ofile to ofolder". Assuming you want to exlude some precious files from being killed. The script will only delete files with the extension given in dicEXT. The relevant code part is If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ) ) ) Then WScript.Echo "* Deleting '" + oFile.Path + "'" Else We need something like If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ) ) ) Then If <file is precious> Then If bVerbose Then WScript.Echo "Keeping prec" + oFile.Path + "'" Else WScript.Echo "* Deleting '" + oFile.Path + "'" End If Else How to implement <file is precious>? Use the path strategy exemplified by dicXFP. Dim dicXFP : Set dicXFP = CreateObject( "Scripting.Dictionary" ) + Dim dicPRF : Set dicPRF = CreateObject( "Scripting.Dictionary" ) ' Folder pathes to exclude (subdirs will still be processed) dicXFP.Add "c:\temp\delsel\dir1\dir11", 0 +'Precious files to keep identified by full filespec + dicPRF.Add "c:\temp\delsel\dir1\dir11\VenusVonMilo.png", 0 + dicPRF.Add "c:\temp\delsel\dir1\dir11\LaraCroft.png", 0 If dicXFP.Exists( LCase( sThisDir ) ) Then > If dicPRF.Exists( LCase( oFile.Path ) ) Then If bVerbose Then WScript.Echo "Keeping prec" + oFile.Path + "'" Or do you want to identify precious files just by their name - without regard to where they are found? Then use the name strategy exemplified by dicXFN. + dicPRF.Add "VenusVonMilo.png", 0 > If dicPRF.Exists( LCase( oFile.Name ) ) Then You don't need to exclude whole branches - just don't add any folders to the dictionary used by the code to inhibit the subdir recursion If dicXFN.Exists( LCase( oSubDir.Name ) ) Then If bVerbose Then WScript.Echo "Skipping '" + oSubDir.Path + "'" Else delSelFiles oFS, oSubDir.Path, dicXFN, dicXFP, dicEXT, bVerbose End If or delete the IF construct to force full traversal For Each oSubDir In oThisDir.SubFolders delSelFiles oFS, oSubDir.Path, dicXFN, dicXFP, dicEXT, bVerbose Next You want exculde some folder by path - just add them to dicXFP ' Folder pathes to exclude (subdirs will still be processed) dicXFP.Add "c:\temp\delsel\dir1\dir11", 0 + dicXFP.Add "c:\temp\dont\delete\my\files", 0 The same, but exclude by name - look at the name comparison scheme for dicXFN If dicXFN.Exists( LCase( oSubDir.Name ) ) Then and use it instead of - If dicXFP.Exists( LCase( sThisDir ) ) Then + If dicXFP.Exists( LCase( oSubDir.Name ) ) Then Of course you'll have to add names, not pathes to dicXFN (and then the variable name should be changed too). In short: Don't feel bad about my initial remarks (no offense meant, it is difficult to get your specs right and even more difficult to explain them to another person), use the code to experiment and post your findings here. Good luck.
|
|
| |
|
|
|
 |
RE: need help to exclude a folder - 4/26/2006 8:28:05 AM
|
|
 |
|
| |
kracksmith
Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
|
What you have is a killer script. Much shorter than my original. Probably more efficient too. When I mean by excluding I mean to exclude certain precious .png files within a certain directory. There can be more than 1 directory. so you would exlude VenusVonMilo.png in c:\temp\dir1, but still delete c:\temp\dir2\VenusVonMilo.png yes, exclude files by single path only. if i have many files i would exclude it using multiple paths. and if i want to exclude a folder it should exclude the folder and all subdirectories under it. So by excluding a full path is ok right? Or easy to implement right? Yes, but you have the full path for each file/folder you want to exclude yes perfect. I'm just doing a test run on my workstation before releasing it to the real servers. doesn't seem to be deleting anything. it does run but not deleting. i don't see any rem statement before the delete statement. and if there isn't any .dbk to be deleted as in my test run, should i need to worry about putting in a "on error resume next"? ' Path to start directory (adapt) Dim sSDir : sSDir = "C:\Documents and Settings\alex.BLA.000\Desktop\New Folder\" Dim dicXFP : Set dicXFP = CreateObject( "Scripting.Dictionary" ) Dim dicEXT : Set dicEXT = CreateObject( "Scripting.Dictionary" ) Dim dicPFP : Set dicPFP = CreateObject( "Scripting.Dictionary" ) ' Yes I want to exclude the particular directory plus all its subfolders down. ' There can be more than 1 directory. ' Folder pathes to exclude (subdirs won't be processed; inhibit recursion) ' identified by Path (LCase); replace/add all the folders you want to exclude dicXFP.Add sSDir + "C:\Documents and Settings\alex.BLA.000\Desktop\New Folder\Music", 0 dicXFP.Add sSDir + "other\dir\to\exclude", 0 ' Extensions of files to delete; replace/add all your extensions dicEXT.Add "wma", 0 dicEXT.Add "dbk", 0 ' I mean to exclude certain precious .png files within a certain directory ' Precious files to keep ' identified by Path (LCase); replace/add all your precious files dicPFP.Add sSDir + "dir1" + "C:\Documents and Settings\alex.BLA.000\Desktop\New Folder\The Ventures\01 Walk Don't Run.wma", 0 Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) delSelFiles3 oFS, sSDir, dicXFP, dicEXT, dicPFP, true Sub delSelFiles3( oFS, sThisDir, dicXFP, dicEXT, dicPFP, bVerbose ) Dim oThisDir : Set oThisDir = oFS.GetFolder( sThisDir ) If bVerbose Then WScript.Echo "Processing '" + sThisDir + "'" ' if we reach a directory it's files and subdirs should be processed Dim oFile For Each oFile In oThisDir.Files If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ) ) ) Then WScript.Echo " Found '" + oFile.Path + "'" If dicPFP.Exists( LCase( oFile.Path ) ) Then WScript.Echo "* Keeping '" + oFile.Path + "'" Else WScript.Echo "* Deleting '" + oFile.Path + "'" End If Else If bVerbose Then WScript.Echo " Keeping '" + oFile.Path + "'" End If Next Dim oSubDir For Each oSubDir In oThisDir.SubFolders If dicXFP.Exists( LCase( oSubDir.Path ) ) Then If bVerbose Then WScript.Echo " Skipping '" + oSubDir.Path + "'" Else delSelFiles3 oFS, oSubDir.Path, dicXFP, dicEXT, dicPFP, bVerbose End If Next End Sub
|
|
| |
|
|
|
 |
RE: need help to exclude a folder - 4/26/2006 5:00:22 PM
|
|
 |
|
| |
ehvbs
Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
|
quote:
ORIGINAL: kracksmith EDIT: ehvbs What you have is a killer script. Much shorter than my original. Probably more efficient too. Thanks When I mean by excluding I mean to exclude certain precious .png files within a certain directory. There can be more than 1 directory. so you would exlude VenusVonMilo.png in c:\temp\dir1, but still delete c:\temp\dir2\VenusVonMilo.png yes, exclude files by single path only. if i have many files i would exclude it using multiple paths. and if i want to exclude a folder it should exclude the folder and all subdirectories under it. So by excluding a full path is ok right? Or easy to implement right? Yes, but you have the full path for each file/folder you want to exclude yes perfect. Ok I'm just doing a test run on my workstation before releasing it to the real servers. Very good doesn't seem to be deleting anything. it does run but not deleting. i don't see any rem statement before the delete statement. The line oFS.DeleteFile( oFile.Path ) should be added after the script is tested rigorously. and if there isn't any .dbk to be deleted as in my test run, should i need to worry about putting in a "on error resume next"? Two answers: (1) No, don't worry - the script won't try to delete a file that wasn't found in the oThisDir.Files collection before (2) Yes - if you think its possible that someone else deletes the file after the script found it and before the script deletes it or that the file is locked/used or that there may be permission problems. Then do it this way On Error Resume Next oFS.DeleteFile( oFile.Path ) If 0 <> Err.Number Then If bVerbose Then WScript.Echo "can't delete " + oFile.Path End If On Error GoTo 0 ' Path to start directory (adapt) Dim sSDir : sSDir = "C:\Documents and Settings\alex.BLA.000\Desktop\New Folder\" Dim sSDir : sSDir = "c:\documents and settings\alex.bla.000\desktop\new folder\" ' lowercase!!! Dim dicXFP : Set dicXFP = CreateObject( "Scripting.Dictionary" ) Dim dicEXT : Set dicEXT = CreateObject( "Scripting.Dictionary" ) Dim dicPFP : Set dicPFP = CreateObject( "Scripting.Dictionary" ) ' Yes I want to exclude the particular directory plus all its subfolders down. ' There can be more than 1 directory. ' Folder pathes to exclude (subdirs won't be processed; inhibit recursion) ' identified by Path (LCase); replace/add all the folders you want to exclude dicXFP.Add sSDir + "C:\Documents and Settings\alex.BLA.000\Desktop\New Folder\Music", 0 All pathes have sSDir as prefix, so dicXFP.Add sSDir + "\music", 0 ' I mean to exclude certain precious .png files within a certain directory ' Precious files to keep ' identified by Path (LCase); replace/add all your precious files dicPFP.Add sSDir + "dir1" + "C:\Documents and Settings\alex.BLA.000\Desktop\New Folder\The Ventures\01 Walk Don't Run.wma", 0 Should read: dicPFP.Add sSDir + "\the ventures\01 walk don't run.wma", 0 dicPFP.Add sSDir + "\the ventures\precious2.wma", 0 dicPFP.Add sSDir + "\the ventures\precious1.dbk", 0 dicPFP.Add sSDir + "\otherdirbelow_new_folder\precious175.wma", 0 Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) delSelFiles3 oFS, sSDir, dicXFP, dicEXT, dicPFP, true Sub delSelFiles3( oFS, sThisDir, dicXFP, dicEXT, dicPFP, bVerbose ) Dim oThisDir : Set oThisDir = oFS.GetFolder( sThisDir ) If bVerbose Then WScript.Echo "Processing '" + sThisDir + "'" ' if we reach a directory it's files and subdirs should be processed Dim oFile For Each oFile In oThisDir.Files If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ) ) ) Then WScript.Echo " Found '" + oFile.Path + "'" If dicPFP.Exists( LCase( oFile.Path ) ) Then WScript.Echo "* Keeping '" + oFile.Path + "'" Else WScript.Echo "* Deleting '" + oFile.Path + "'" add delete code here End If Else If bVerbose Then WScript.Echo " Keeping '" + oFile.Path + "'" End If Next Dim oSubDir For Each oSubDir In oThisDir.SubFolders If dicXFP.Exists( LCase( oSubDir.Path ) ) Then If bVerbose Then WScript.Echo " Skipping '" + oSubDir.Path + "'" Else delSelFiles3 oFS, oSubDir.Path, dicXFP, dicEXT, dicPFP, bVerbose End If Next End Sub This LCase strategy I came up with is really nothing to be proud of! I thought specifying pathes and names in lower case and using LCase() in the comparisons - like If dicXFP.Exists( LCase( oSubDir.Path ) ) Then would avoid the problem of missing items because of case differences. But in real life you would copy the pathes from a directory listing - and changing everything to lower case by hand sucks. Any idea? You should run the script with cscript to avoid the MsgBoxes.
|
|
| |
|
|
|
 |
RE: need help to exclude a folder - 4/27/2006 6:08:06 AM
|
|
 |
|
| |
kracksmith
Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
|
3 questions and 1 comment 1. it's deleting all the .wma files. i have the "music" directory to exclude on the script. 2. It's also deleting my specific .wma file path 3. where do i insert the "on error resume next", i think in the beginning? 4. how do i make it to run in cscript? or is it a good idea just to rem out the "echo" statement? 5. i don't mind changing all path to lowercase, that is the least i need to worry about. thanks! ' Path to start directory (adapt) Dim sSDir : sSDir = "C:\documents and settings\alex.bla.000\desktop\new folder\" Dim dicXFP : Set dicXFP = CreateObject( "Scripting.Dictionary" ) Dim dicEXT : Set dicEXT = CreateObject( "Scripting.Dictionary" ) Dim dicPFP : Set dicPFP = CreateObject( "Scripting.Dictionary" ) ' Yes I want to exclude the particular directory plus all its subfolders down. ' There can be more than 1 directory. ' Folder pathes to exclude (subdirs won't be processed; inhibit recursion) ' identified by Path (LCase); replace/add all the folders you want to exclude dicXFP.Add sSDir + "C:\documents and settings\alex.bla.000\desktop\new folder\music", 0 'dicXFP.Add sSDir + "other\dir\to\exclude", 0 ' Extensions of files to delete; replace/add all your extensions dicEXT.Add "wma", 0 dicEXT.Add "dbk", 0 ' I mean to exclude certain precious .png files within a certain directory ' Precious files to keep ' identified by Path (LCase); replace/add all your precious files dicPFP.Add sSDir + "C:\documents and settings\alex.bla.000\desktop\new folder\the ventures\01 walk don't run.wma", 0 Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) delSelFiles3 oFS, sSDir, dicXFP, dicEXT, dicPFP, true Sub delSelFiles3( oFS, sThisDir, dicXFP, dicEXT, dicPFP, bVerbose ) Dim oThisDir : Set oThisDir = oFS.GetFolder( sThisDir ) If bVerbose Then WScript.Echo "Processing '" + sThisDir + "'" ' if we reach a directory it's files and subdirs should be processed Dim oFile For Each oFile In oThisDir.Files If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ) ) ) Then WScript.Echo " Found '" + oFile.Path + "'" If dicPFP.Exists( LCase( oFile.Path ) ) Then WScript.Echo "* Keeping '" + oFile.Path + "'" Else WScript.Echo "* Deleting '" + oFile.Path + "'" oFS.DeleteFile( oFile.Path) End If Else If bVerbose Then WScript.Echo " Keeping '" + oFile.Path + "'" End If Next Dim oSubDir For Each oSubDir In oThisDir.SubFolders If dicXFP.Exists( LCase( oSubDir.Path ) ) Then If bVerbose Then WScript.Echo " Skipping '" + oSubDir.Path + "'" Else delSelFiles3 oFS, oSubDir.Path, dicXFP, dicEXT, dicPFP, bVerbose End If Next End Sub
|
|
| |
| | |