Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


need help to exclude a folder

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> need help to exclude a folder
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 need help to exclude a folder - 4/19/2006 7:19:29 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
i have a script here that will delete any extension through many sub directories and will exclude certain files.

how do i make it to exclude 1 or more directories?

i tried to add a oFolder in the DIM but got losted from there.


Thanks!


Option Explicit
Dim fso, sExtToDelete
Dim nCount
sExtToDelete = array(".png", ".dbk")
nCount = 0
Set fso = CreateObject("Scripting.FileSystemObject")
'DirWalk("\\Prod2\I_Drive\")     
Wscript.Echo nCount & " files deleted."
Sub DirWalk(parmPath)
Dim oSubDir, oSubFolder, oFile, n
Dim bDeleted
On Error Resume Next        
Set oSubFolder = fso.getfolder(parmPath)
For Each oFile In oSubFolder.Files   
If Err.Number = 0 Then
bDeleted = False
For n = 0 To UBound(sExtToDelete)
If LCase(Right(oFile.Name,Len(sExtToDelete(n)))) = sExtToDelete(n) Then
'Wscript.Echo "about to delete " & oFile.Path
fso.DeleteFile oFile.Path, True
nCount = nCount + 1
bDeleted = True
Exit For
End If
Next
If Not bDeleted Then
If LCase(oFile.Name) = "acxcad.eer" _
Or LCase(oFile.Name) = "123.XLS.ink" _
Or LCase(oFile.Name) = "456.XLS" _
Or LCase(oFile.Name) = "789" _ 
Or LCase(oFile.Name) = "abc.dgg" Then
Wscript.Echo "about to delete " & oFile.Path
fso.DeleteFile oFile.Path, True
nCount = nCount + 1
End If
Else   
Err.Clear
End If
End If
Next
For Each oSubDir In oSubFolder.Subfolders
DirWalk oSubDir.Path      
Next
On Error Goto 0             
End Sub
 
 
Post #: 1
 
 RE: need help to exclude a folder - 4/19/2006 9:14:24 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi kracksmith,

It's not easy to follow the flow of unformatted code - so I may be wrong. But I
think the lines

   For Each oSubDir In oSubFolder.Subfolders
         DirWalk oSubDir.Path      
   Next

are responsible for descending recurively down into the the subfolders of the
current directory. If you don't want to traverse certain folders and all their
children you should not call "DirWalk oSubDir.Path" for these folders - something like

   For Each oSubDir In oSubFolder.Subfolders
          If oSubDir.Path <> "c:\the\path\to\avoid" Then
              DirWalk oSubDir.Path      
          End If
   Next

Of course, there a better ways to specify the condition - you could use a dictionary
to store the pathes to exclude or come up with a suitable RegExp.

If you don't want to exclude whole branches, but only single specific directories,
you'll have to check if the folder you are just about to delete the files of belongs
to the set of directories to exclude -

  Set oSubFolder = fso.getfolder(parmPath)
  If oSubFolder .Path <> "c:\the\path\to\avoid" Then
     For Each oFile In oSubFolder.Files   
            ...
     Next
   End If

Good luck.

(in reply to kracksmith)
 
 
Post #: 2
 
 RE: need help to exclude a folder - 4/21/2006 4:07:25 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
thanks

i want to exclude a certain folder but not stop the script when it finds it.


I did a search and this is what i came up with:

skip = false
For Each xFolder in xFolderArray
If strSourcefolderpath & "\" & xFolder =
oFolderSource.Path Then
skip = true
End If
Next

If skip = false Then
...proceed with code


how can i incorporate this in my script?

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: need help to exclude a folder - 4/21/2006 6:12:40 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi kracksmith,

see/test if this helps:


      

Good luck!

(in reply to kracksmith)
 
 
Post #: 4
 
 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

(in reply to ehvbs)
 
 
Post #: 5
 
 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. 
 

(in reply to kracksmith)
 
 
Post #: 6
 
 RE: need help to exclude a folder - 4/25/2006 8:33:26 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Just realized at least one blunder (and have to hurry because
ebgreen seems to be looking!):

I used LCase( path|name ) in my comparison code to avoid problems. Then

+ dicPRF.Add "c:\temp\delsel\dir1\dir11\VenusVonMilo.png", 0
+ dicPRF.Add "c:\temp\delsel\dir1\dir11\LaraCroft.png", 0

is definitely BAD.

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: need help to exclude a folder - 4/25/2006 11:30:34 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
quote:

(and have to hurry because
ebgreen seems to be looking!)[/code]

Huh?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 8
 
 RE: need help to exclude a folder - 4/25/2006 4:22:19 PM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
[Hi ebgreen,

proof reading my posting, I saw you looking at this topic. I was afraid, you'd spot
my error before I did. My apologies, if my lame joke irritated you.

ehvbs]

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: need help to exclude a folder - 4/25/2006 5:07:09 PM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
No irritation at all...just confusion...as usual. 

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 10
 
 RE: need help to exclude a folder - 4/26/2006 4:32:28 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
sorry about the confusion. You are right, my codes says to delete additional specific files instead of excluding it.
 
When I mean by excluding I mean to exclude certain precious .png files within a certain directory. There can be more than 1 directory.
 
Yes I want to exclude the particular directory plus all its subfolders down. There can be more than 10 subdirectories.
 
Once the script find these particular directories to exclude I like it to continue doing it’s job until it finish.
 
So by excluding a full path is ok right? Or easy to implement right?
 
 
Ok, I’m not much of a programmer but willing to learn the best I can.
 
I’m reading you’re nicely researched out responds and we can drop the exclusion by name for now. It seems too much for me to get it going. I will just stick to the exclusion by path for now.
 
So by using path I can specify by folder or file, right?
 
(dicXFP.Add “c:\temp\delsel\dir11”,0)    ß by directory
Or
(dicPRF.Add “c:\temp\delsel\dir1\dir11\VenusVonMilo.png”, 0)  ß by file
 
 
So if I want to adapt your script to my needs, I would type:
 
If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ))) Then
            If  “dicXFP.Add “c:\temp\delsel\dir11”,0 Then
                        If bVerbose Then WScript.Echo “Keeping prec” + oFile.Path + “”
            Else
                        WScript.Echo “* Deleting “” + oFile.Path + “”
            End If
            Else
 
And if I wanted to do the same but with a file then all I have to do is add:
dicPRF.Add “c:\temp\delsel\dir1\dir11\VenusVonMilo.png”, 0
 
Also I would need to add the Variables?
is this the correct syntax?

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: need help to exclude a folder - 4/26/2006 5:56:45 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
quote:



ORIGINAL: kracksmith
EDITED: ehvbs

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 I want to exclude the particular directory plus all its subfolders down. There can be more than 10 subdirectories.
Ok

Once the script find these particular directories to exclude I like it to continue doing it's job until it finish.

Ok

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
 
I will just stick to the exclusion by path for now.
Ok

So by using path I can specify by folder or file, right?
Folders and Files will be identified by full path.

(dicXFP.Add "c:\temp\delsel\dir11”,0)    ß by directory
Ok  - directory identified by path

(dicPRF.Add "c:\temp\delsel\dir1\dir11\VenusVonMilo.png”, 0)  ß by file
?? - file identified by path (and please use lowercase)
 
So if I want to adapt your script to my needs, I would type:
 
If dicEXT.Exists( LCase( oFS.GetExtensionName( oFile.Name ))) Then
Ok - we look in dicEXT if there is a key equal to the (lowercase) extension of the current file

            If  "dicXFP.Add "c:\temp\delsel\dir11”,0 Then
Very bad: adding should be done at the start of the script
Very bad: to look for precious file you'll have to check the dictionary you stored the files in
Very bad: you should checking the full path of the current file

                        If bVerbose Then WScript.Echo "Keeping prec” + oFile.Path + "”
            Else
                        WScript.Echo "* Deleting "” + oFile.Path + "”
            End If
            Else
 
And if I wanted to do the same but with a file then all I have to do is add:
dicPRF.Add "c:\temp\delsel\dir1\dir11\VenusVonMilo.png”, 0
Ok

Also I would need to add the Variables?
is this the correct syntax?


I rewrote the script according to your specs


      

Good luck.

(in reply to kracksmith)
 
 
Post #: 12
 
 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


(in reply to ehvbs)
 
 
Post #: 13
 
 RE: need help to exclude a folder - 4/26/2006 9:03:33 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
Just ran it again, no error

There is a msg box that says it's going to delete it but it doesn't delete.



< Message edited by kracksmith -- 4/26/2006 9:51:35 AM >

(in reply to kracksmith)
 
 
Post #: 14
 
 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.

(in reply to kracksmith)
 
 
Post #: 15
 
 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

(in reply to ehvbs)