VBScript: If..Or..Then

Just been trying to do something that I thought would be really simple. Just been modifying a script which checks a specified network drive’s folders, to make sure that every folder has a corresponding user in Active Directory, and then, if it does set the owner and permissions on that folder accordingly. (We have changed from using Home Folders in AD to using Folder Redirection, which only works when the user is the owner of their folder)

So, during testing I noticed that two folders kept coming up that I did not want to modify the permissions on; System Volume Information and $RECYCLE.BIN. Instant thought: If folder is not called ‘System Volume Information’ Or ‘$RECYCLE.BIN’ Then Run Code.

Translated into VBScript it looked something like this:


For Each objSubfolder in colSubfolders
If objSubfolder.Name <> "System Volume Information" And objSubfolder.Name <> "$RECYCLE.BIN" Then
Code to check goes here
End If

Could not get this to work at all. Even Microsoft Technet says that it should. Tried converting to lowercase before comparing too. As a test I tried it with just a single condition, and both conditions individually work.

In the end I changed the code and used a Select Case. Not quite as short on the code, but the same end result and possibly more elegant.


For Each objSubfolder in colSubfolders
Select Case LCase(objSubfolder.Name)
Case "system volume information", "$recycle.bin"
Case Else
Code to check goes here
End Select

Just another really annoying 10 minutes trying to work out something that should just work!

12 people found this post useful.


6 thoughts on “VBScript: If..Or..Then

  1. The logic to exclude both those folders before any code is executed should be AND. In algorithm speak you want:


    if
    (current file is NOT "System Volume Information")
    AND
    (current file is NOT "$RECYCLE.BIN")
    {
    deal with the current file / folder
    }

    Use a spreadsheet with formulae matching your if statement to validate the logic. For example, column A has the sample folder names, column B the formula, which in Excel speak is =if(or(a1″System Volume Information”,a1″$RECYCLE.BIN”),True,False). Column C has the same formula, but uses AND instead of the OR. Compare the column values.

    The OR logic *WILL* result in *EVERY* folder resulting in a result of TRUE as either of those folders will always make the other test TRUE.

    Hope this helps,
    Keith

    1. Thanks Keith,

      Turnsd out my copy typing is not that good – in my original script that I used I had put the AND in the correct place. Thanks for noticing the correction on the site though. I have now updated the code.

      Cheers
      Craig.

  2. The ‘owner’ of the folder but stumped me for quite a while. I found the answer by looking through the Event Viewer on one of our clients.

    I will dig out the script and post the code. Not sure it is all quite as elegant as it could be, but it works.

  3. I really liked reading your post!. Quallity content. With such a valuable blog i believe you deserve to be ranking even higher in the search engines :). Check out the link in my name. That links to a tool that really helped me rank high in google. This way even more people can enjoy your posts and nothing beats a big audiance ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.