Skipping folder if size is greater then specified amount

I would like to incorporate the ability to skip a folder that is greater then some size in this home directory script. I found the following function that might help but i'm not sure how to incorporate it into the main script.

'# Folder Size
                function FolderSize(filespec)
                Set fso = CreateObject("Scripting.FileSystemObject")
                      Dim fso, f, s
                      if fso.FolderExists(filespec) Then
                            Set f = fso.GetFolder(filespec)
                            s = f.size
                            if s = "" Then s = "0"
                      Else
                            s = "0"
                       End if
                      FolderSize = CLNG(s)
                Set fso=nothing
                End function

Here's the script:


on error resume next

Const MY_COMPUTER = &H11&
Const SYSTEM32 = &H25&
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const FILES_YES = &H4000&

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
strComputer = "."

'INFORM THE USER THEY NEED XCACLS.VBS AND GIVE THEM THE OPTION TO DOWNLOAD IT

xcacQuest = "This script requires xcacls.vbs from Microsoft.  If you need to download it click yes.  If you don't, click no.  And if you feel safer getting the file from Microsoft yourself click cancel and rerun this script after you have."
xcacTitle = "Home Groan - Got xcacls?"
HomeGTitle = "Home Groan"
HomeGDomTitle = "Home Groan - Downlevel Domain"

xcacChoice = msgbox(xcacQuest, vbQuestion + vbYesNoCancel + vbDefaultButton6, xcacTitle)

xcacLoc = "http://download.microsoft.com/download/f/7/8/f786aaf3-a37b-45ab-b0a2-8c8c18bbf483/XCacls_Installer.exe"

'IF THEY WANT US TO DOWNLOAD IT FOR THEM WE WILL PAUSE THE SCRIPT
'WE PAUSE BY CREATING A LOOP THAT WAITS FOR THE XCACLS INSTALER PROCESS TO TERMINATE

IF xcacChoice = 6 then

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancedeletionevent " _
            & "within 1 where TargetInstance isa 'Win32_Process'")

xcacDLWait = msgbox ("When the download finishes run XCacls_Installer.exe which will extract xcacls.vbs to the directory of your choice.  This script should automatically continue when the xcacls_installer exits.", vbokonly, xcactitle)

'BELOW WE LAUNCH AN IE PROCESS TO BEGIN THE FILE DOWNLOAD

set ie = CreateObject("InternetExplorer.Application")
ie.visible = true
ie.navigate xcacLoc

i = 0

'THE NEXT LINE IS WHERE WE SPECIFY THE PROCESS TO MONITOR FOR TERMINATION

Do While i <> "XCacls_Installer.exe"
    Set objLatestProcess = colMonitoredProcesses.NextEvent
i = objLatestProcess.TargetInstance.Name
loop

ELSEIF xcacChoice = vbcancel then

xcacCancel = msgbox ("You can download xcacls.vbs by searching for it at http://www.microsoft.com/downloads", vbokonly, xcactitle)
wscript.quit()

END IF

'GET THE PATH OF THE SYSTEM32 FOLDER so we can launch cscript
'JUST IN CASE THE PATH SYSTEM VARIABLE IS HORRIBLY WRONG
'OR BECAUSE WE'RE OBSESIVE COMPULIVE AND CAN'T LEAVE IT ALONE

Set objSYSDR = objShell.Namespace(SYSTEM32)
Set objSYSDRItem = objsysDR.Self
Sys32 = objsysDRItem.Path

'GET THE PATH - ERR CLSID OF MY COMPUTER SO IT CAN BE USED AS THE ROOT
'WHEN WE BROWSE FOR FILES/FOLDERS

Set objMyComp = objShell.Namespace(MY_COMPUTER)
Set objMyCompItem = objMyComp.Self
BrowseRoot = objMyCompItem.Path

'OPEN BROWSE WINDOW SO USER CAN SPECIFY LOCATION OF XCACLS.VBS
'APPEND THE LOCATION WITH \XCACLS.VBS AND SET IT TO A VARIABLE

Do Until objFSO.FileExists(xcacls)

Set xcaclspath = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select the location of the xcacls.vbs file:", NO_OPTIONS, BrowseRoot)  

IF xcaclspath = Empty then
wscript.quit()

ELSEIF xcaclspath = "" then
wscript.quit()
END IF

Set xcaclspathItem = xcaclspath.Self
xcaclsPath = xcaclspathItem.Path

xcacls = xcaclspath & "\xcacls.vbs"

If not objFSO.FileExists(xcacls) then

oUserError = Msgbox ("xcacls.vbs does not exist in the folder you specified.  Please verify it's location and select the appropriate folder.", vbExclamation + vbokonly, HomeGTitle)
end if
loop

'OPEN BROWSE WINDOW SO USER CAN SPECIFY LOCATION OF HOME FOLDERS (PARENT DIRECTORY)

Set objpath = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select the drive or folder containing the Home Directories to secure:", NO_OPTIONS, BrowseRoot)      
Set objpathItem = objpath.Self
objPath = objpathItem.Path

'PROMPT USER FOR THE DOWNLEVEL DOMAIN NAME

do until thisdom <> empty
ThisDom = Inputbox("Type the downlevel name of the domain the accounts associated with these home folders are in (ie no .com)", HomeGDomTitle)

IF (ThisDom = empty) then

oUserError = Msgbox ("The domain name is required and can not be blank.  Script will now exit.", vbExclamation + vbokonly, HomeGTitle)
wscript.quit()
End IF
loop

'PROMPT USER FOR NUMBER OF CHARACTERS TO IGNORE FROM THE LEFT

iCHleft = Inputbox("In determining the account name, how many (if any) characters should be cropped from the left of the folder name?  For example if your naming convention is $$username then enter 2 here.  If the folder names begin with the account names enter 0.", HomeGTitle)

IF (iCHleft = empty) then
lUserError = Msgbox ("Script will now exit.  If you meant to specify not to skip any characters, run the script again and enter 0 next time", vbInformation + vbokonly, HomeGTitle)
wscript.quit()
END IF

'PROMPT USER FOR NUMBER OF CHARACTERS TO IGNORE FROM THE RIGHT

iCHright = Inputbox("In determining the account name, how many (if any) characters should be cropped from the right of the folder name?  For example if your naming convention is username$$ then enter 2 here.  If the folder names end with the account names enter 0.", HomeGTitle)

IF (iCHright = empty) then
rUserError = Msgbox ("Script will now exit.  If you meant to specify not to skip any characters, run the script again and enter 0 next time", vbInformation + vbokonly,  HomeGTitle)
wscript.quit()
END IF

'PROMPT USER FOR STRING TO MATCH

strFilter = Inputbox("If you want to filter by folder name enter the string to match below.  Only folders whose names begin with the string will be changed.  For example if you enter the letter ""A"" (do not use quotes) then the folder named asmith would be changed but not the folder named janedoe. Leave blank to change all.", HomeGTitle)

IF strFilter <> EMPTY then
      rFormatFilter = LCase(strFilter)
filterAlive = 1
END IF

'CREATE A COLLECTION OFF ALL SUB FOLDERS IN THE DIRECTORY SPECIFIED BY USER

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & objPath & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")


'SINCE WE USE APPACTIVATE TO TIGHTEN PERFORMANCE IT IS VERY IMPORTANT TO HAVE NO OTHER WINDOWS OPEN WHEN THIS RUNS
'THIS IS WHERE WE TELL THE USER THAT

xcacDLWait = msgbox ("Please make sure all other windows are closed - especially any folders you have open.  This is very important if you'd like this to work.  Please click ok when that's done and you're ready to continue.", vbInformation + vbokonly, xcactitle)

'HERE IS WHERE WE CHANGE FOLDER PERMISSIONS
'EVERYTHING BETWEEN THE FOR AND THE NEXT STATEMENT_
'WILL BE RUN ON EACH FOLDER IN THE COLLECTION ABOVE

For Each objFolder in colSubfolders

Set objFolders = objFSO.GetFolder(objFolder.name)

      FID = objFolders.name
      HomeFolder = objFolder.name

'WE ARE PARSING EACH FOLDER NAME TO FIND IT'S
'ASSOCIATED USER ACCOUNT.  WE MUST ALSO IGNORE
'THE NUMBER OF CHARACTERS SPECIFIED BY USER (IF ANY)

'*******************************************
'THANKS TO Kenneth Bryant (kbryant@checksinthemail.com) AND
'Richard Stephen Reese (rsreese@ifas.ufl.edu)
'FOR HELPING TO RESOLVE AND ERROR IN THIS NEXT SECTION
'*******************************************

If iCHright > 0 Then

      uidR = RTrim(Left(FID,Len(FID)-iCHright))
cRightnum = 1
END IF

IF cRightnum = 1 then

      UID = LTrim(Right(uidR,Len(uIDR)-iCHleft))

ELSE

      UID = LTrim(Right(FID,Len(FID)-iCHleft))

END IF

'WE COMPARE FOLDER NAMES TO OUR FILTER STRING (IF ANY)

noMatch = 0
IF filterAlive = 1 then
checkFilter = Left(uid,LEN(rFormatFilter))

IF lcase(checkFilter) <> rFormatFilter then
      noMatch = 1
END IF
END IF

'THIS IS THE COMMAND THAT RUNS THE XCACLS.VBS SCRIPT.
'THIS IS WHERE YOU CAN CHANGE WHAT PERMISSIONS ARE SET
'AND WHAT GROUPS THEY'RE SET FOR
'NOTE THAT OUTPUT LOGS TO c:\xcacls.txt
'750,000 FILES WILL GIVE YOU A 50 MEG LOG EASY

IF noMatch = 0 then

WSHShell.run ("""" & sys32 & "\cscript.exe"" """ & xcacls & """ """ & homefolder & """ /f /s /t /g administrators:F /g " & ThisDom & "\" & UID & ":M /I remove /l c:\xcacls.txt /Q")

      wscript.sleep 200
WshShell.AppActivate "C:\"

Do while WshShell.AppActivate("C:\") = TRUE
      wscript.sleep 200
loop
END IF

'THIS IS THE LAST LINE

Next
LVL 3
adamshieldsAsked:
Who is Participating?
 
JesterTooConnect With a Mentor Commented:
Here is a revamped version of your original script.  I've included two functions at the end of it... one to return the foldersize in bytes and the other to return the foldersize in "number of files it contains" in case this was what you really wanted (seems to make more sense for filecount rather than foldersize for what you're doing, I think).  Just choose which one you want to use at the indicated spot (or choose both of the with an OR condition).  You also need to edit your own "sizes" in those wame two statements... I just stuck some numbers in for testing.  Here is the revised script.  If I have misinterpreted what you need, then please clarify.


on error resume next
Const MY_COMPUTER = &H11&
Const SYSTEM32 = &H25&
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const FILES_YES = &H4000&

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objFSO   = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
strComputer  = "."

' INFORM THE USER THEY NEED XCACLS.VBS AND GIVE THEM THE OPTION TO DOWNLOAD IT
'
xcacQuest  = "This script requires xcacls.vbs from Microsoft.  If you need to download it click yes.  If you don't, click no.  And If you feel safer getting the file from Microsoft yourself click cancel and rerun this script after you have."
xcacTitle  = "Home Groan - Got xcacls?"
HomeGTitle = "Home Groan"
HomeGDomTitle = "Home Groan - Downlevel Domain"

xcacChoice = MsgBox(xcacQuest, vbQuestion + vbYesNoCancel + vbDefaultButton6, xcacTitle)

xcacLoc = "http://download.microsoft.com/download/f/7/8/f786aaf3-a37b-45ab-b0a2-8c8c18bbf483/XCacls_Installer.exe"

' IF THEY WANT US TO DOWNLOAD IT FOR THEM WE WILL PAUSE THE SCRIPT
' WE PAUSE BY CREATING A LOOP THAT WAITS FOR THE XCACLS INSTALLER PROCESS TO TERMINATE
'
If xcacChoice = 6 Then
   Set objWMIService = GetObject("winmgmts:" _
       & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colMonitoredProcesses = objWMIService. _
       ExecNotificationQuery("select * from __instancedeletionevent " _
               & "within 1 where TargetInstance isa 'Win32_Process'")

   xcacDLWait = MsgBox ("When the download finishes run XCacls_Installer.exe which will extract xcacls.vbs to the directory of your choice.  This script should automatically continue when the xcacls_installer exits.", vbokonly, xcactitle)

   ' BELOW WE LAUNCH AN IE PROCESS TO BEGIN THE FILE DOWNLOAD
   '
   Set ie = CreateObject("InternetExplorer.Application")
   ie.visible = True
   ie.navigate xcacLoc


   ' THE NEXT LINE IS WHERE WE SPECIFY THE PROCESS TO MONITOR FOR TERMINATION
   '
   i = ""
   Do While i <> "XCacls_Installer.exe"
      Set objLatestProcess = colMonitoredProcesses.NextEvent
      i = objLatestProcess.TargetInstance.Name
   Loop

ElseIf xcacChoice = vbcancel Then

   xcacCancel = MsgBox ("You can download xcacls.vbs by searching for it at http://www.microsoft.com/downloads", vbokonly, xcactitle)
   Wscript.Quit()

End If

' GET THE PATH OF THE SYSTEM32 FOLDER SO WE CAN LAUNCH CSCRIPT
' JUST IN CASE THE PATH SYSTEM VARIABLE IS HORRIBLY WRONG
' OR BECAUSE WE'RE OBSESSIVE COMPULSIVE AND CAN'T LEAVE IT ALONE
'
Set objSYSDR = objShell.Namespace(SYSTEM32)
Set objSYSDRItem = objsysDR.Self
Sys32 = objsysDRItem.Path

' GET THE PATH - ERR CLSID OF MY COMPUTER SO IT CAN BE USED AS THE ROOT
' WHEN WE BROWSE FOR FILES/FOLDERS
'
Set objMyComp = objShell.Namespace(MY_COMPUTER)
Set objMyCompItem = objMyComp.Self
BrowseRoot = objMyCompItem.Path

' OPEN BROWSE WINDOW SO USER CAN SPECIFY LOCATION OF XCACLS.VBS
' APPEND THE LOCATION WITH \XCACLS.VBS AND SET IT TO A VARIABLE
'
Do Until objFSO.FileExists(xcacls)

   Set xcaclspath = objShell.BrowseForFolder _
       (WINDOW_HANDLE, "Select the location of the xcacls.vbs file:", NO_OPTIONS, BrowseRoot)

   If xcaclspath = Empty Then
      Wscript.Quit()
   ElseIf xcaclspath = "" Then
      Wscript.Quit()
   End If

   Set xcaclspathItem = xcaclspath.Self
   xcaclsPath = xcaclspathItem.Path

   xcacls = xcaclspath & "\xcacls.vbs"

   If Not objFSO.FileExists(xcacls) Then
      oUserError = MsgBox ("xcacls.vbs does not exist in the folder you specified.  Please verify it's location and select the appropriate folder.", vbExclamation + vbokonly, HomeGTitle)
   End If
Loop

' OPEN BROWSE WINDOW SO USER CAN SPECIFY LOCATION OF HOME FOLDERS (PARENT DIRECTORY)
'
Set objpath = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select the drive or folder containing the Home Directories to secure:", NO_OPTIONS, BrowseRoot)
Set objpathItem = objpath.Self
objPath = objpathItem.Path

' PROMPT USER FOR THE DOWNLEVEL DOMAIN NAME
'
Do Until thisdom <> Empty
   ThisDom = InputBox("Type the downlevel name of the domain the accounts associated with these home folders are in (ie no .com)", HomeGDomTitle)

   If (ThisDom = Empty) Then
      oUserError = MsgBox ("The domain name is required and can not be blank.  Script will now exit.", vbExclamation + vbokonly, HomeGTitle)
      Wscript.Quit()
   End If
Loop

' PROMPT USER FOR NUMBER OF CHARACTERS TO IGNORE FROM THE LEFT
'
iCHleft = InputBox("In determining the account name, how many (if any) characters should be cropped from the left of the folder name?  For example If your naming convention is $$username Then enter 2 here.  If the folder names begin with the account names enter 0.", HomeGTitle)

If (iCHleft = Empty) Then
   lUserError = MsgBox ("Script will now exit.  If you meant to specify not to skip any characters, run the script again and enter 0 next time", vbInformation + vbokonly, HomeGTitle)
   Wscript.Quit()
END If

' PROMPT USER FOR NUMBER OF CHARACTERS TO IGNORE FROM THE RIGHT
'
iCHright = InputBox("In determining the account name, how many (if any) characters should be cropped from the right of the folder name?  For example If your naming convention is username$$ Then enter 2 here.  If the folder names end with the account names enter 0.", HomeGTitle)

If (iCHright = Empty) Then
   rUserError = Msgbox ("Script will now exit.  If you meant to specify not to skip any characters, run the script again and enter 0 next time", vbInformation + vbokonly,  HomeGTitle)
   Wscript.Quit()
End If

' PROMPT USER FOR STRING TO MATCH
'
strFilter = InputBox("If you want to filter by folder name enter the string to match below.  Only folders whose names begin with the string will be changed.  For example if you enter the letter ""A"" (do not use quotes) then the folder named asmith would be changed but not the folder named janedoe. Leave blank to change all.", HomeGTitle)

If strFilter <> Empty Then
   rFormatFilter = LCase(strFilter)
   filterAlive = 1
End If

' CREATE A COLLECTION OFF ALL SUB FOLDERS IN THE DIRECTORY SPECIFIED BY USER
'
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & objPath & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")


' SINCE WE USE APPACTIVATE TO TIGHTEN PERFORMANCE IT IS VERY IMPORTANT TO HAVE NO OTHER WINDOWS OPEN WHEN THIS RUNS
' THIS IS WHERE WE TELL THE USER THAT

xcacDLWait = MsgBox ("Please make sure all other windows are closed - especially any folders you have open.  This is very important If you'd like this to work.  Please click ok when that's done and you're ready to continue.", vbInformation + vbokonly, xcactitle)

' HERE IS WHERE WE CHANGE FOLDER PERMISSIONS
' EVERYTHING BETWEEN THE FOR AND THE NEXT STATEMENT
' WILL BE RUN ON EACH FOLDER IN THE COLLECTION ABOVE
'
For Each objFolder In colSubfolders

   Set objFolders = objFSO.GetFolder(objFolder.name)

   FID = objFolders.name
   HomeFolder = objFolder.name

'  If FileCount(objFolder.name) < 1000 Then           '  <== choose between this one
   If FolderSize(objFolder.name) < 100000 Then        '  <== and this one

      ' WE ARE PARSING EACH FOLDER NAME TO FIND IT'S
      ' ASSOCIATED USER ACCOUNT.  WE MUST ALSO IGNORE
      ' THE NUMBER OF CHARACTERS SPECIFIED BY USER (IF ANY)

      '*******************************************
      'THANKS TO Kenneth Bryant (kbryant@checksinthemail.com) AND
      'Richard Stephen Reese (rsreese@ifas.ufl.edu)
      'FOR HELPING TO RESOLVE AND ERROR IN THIS NEXT SECTION
      '*******************************************

      If iCHright > 0 Then
         uidR = RTrim(Left(FID,Len(FID)-iCHright))
         cRightnum = 1
      End If

      If cRightnum = 1 Then
         UID = LTrim(Right(uidR,Len(uIDR)-iCHleft))
      Else
         UID = LTrim(Right(FID,Len(FID)-iCHleft))
      End If

      ' WE COMPARE FOLDER NAMES TO OUR FILTER STRING (IF ANY)

      noMatch = 0
      If filterAlive = 1 Then
         checkFilter = Left(uid,LEN(rFormatFilter))
         If lcase(checkFilter) <> rFormatFilter Then
            noMatch = 1
        End If
      End If

      ' THIS IS THE COMMAND THAT RUNS THE XCACLS.VBS SCRIPT.
      ' THIS IS WHERE YOU CAN CHANGE WHAT PERMISSIONS ARE SET
      ' AND WHAT GROUPS THEY'RE SET FOR
      ' NOTE THAT OUTPUT LOGS TO C:\xcacls.txt
      ' 750,000 FILES WILL GIVE YOU A 50 MEG LOG EASY
      '
      If noMatch = 0 Then
         WSHShell.Run ("""" & sys32 & "\cscript.exe"" """ & xcacls & """ """ & homefolder & """ /f /s /t /g administrators:F /g " & ThisDom & "\" & UID & ":M /I remove /l c:\xcacls.txt /Q")
         Wscript.Sleep 200
         WshShell.AppActivate "C:\"
         Do While WshShell.AppActivate("C:\") = True
            Wscript.Sleep 200
         Loop
      End If
   End If
Next

' Cleanup...
'
Set objFSO = Nothing
Set objWMIService = Nothing
Set colMonitoredProcesses = Nothing
Set ie = Nothing
Set objLatestProcess = Nothing
Set xcaclspath = Nothing
Set xcaclspathItem = Nothing
Set objFolders = objFSO.GetFolder(objFolder.name)


' Returns the size of the folder in "bytes"
'
Function FolderSize(filespec)
   FolderSize = objFSO.GetFolder(filespec).Size
End function

' Returns the size of the folder in "number of files it contains"
'
Function FileCount(filespec)
   FileCount = objFSO.GetFolder(filespec).Files.Count
End function

' END-OF-PROGRAM  END-OF-PROGRAM  END-OF-PROGRAM


HTH,
Lynn
0
 
edwardiiiCommented:
Hi, adamshields.

In the foldersize function, can't you alter the If statement so it does nothing if the folder size is greater than a value?  I'm guessing filespec is the name of the folder you're examining; you can put this function anywhere in your Userform and in your folder-processing routine have "Call FolderSize(filespec)".


       function FolderSize(filespec)
              Set fso = CreateObject("Scripting.FileSystemObject")
                   Dim fso, f, s
                   if fso.FolderExists(filespec) Then
                        Set f = fso.GetFolder(filespec)
                        s = f.size
                        if s > = "" Then 'exit the function and do nothing with that folder
                   Else
                        'do whatever processing you need on the folder here
                   End if
                   FolderSize = CLNG(s)
              Set fso=nothing
       End function
0
 
adamshieldsAuthor Commented:
I found the function FolderSize else where on the internet and i'm trying to figure out how it will work with the script that I have posted.

What is 'Userform'? and which folder routine do you mean?

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
iHadiCommented:
This is the modified version of you script ready to go (as I think)

On Error Resume Next
Const MY_COMPUTER = &H11&
Const SYSTEM32 = &H25&
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const FILES_YES = &H4000&

Dim fso, f, s
Dim tmpNum As Long

Set WshShell = wscript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
strComputer = "."

'INFORM THE USER THEY NEED XCACLS.VBS AND GIVE THEM THE OPTION TO DOWNLOAD IT

xcacQuest = "This script requires xcacls.vbs from Microsoft.  If you need to download it click yes.  If you don't, click no.  And if you feel safer getting the file from Microsoft yourself click cancel and rerun this script after you have."
xcactitle = "Home Groan - Got xcacls?"
HomeGTitle = "Home Groan"
HomeGDomTitle = "Home Groan - Downlevel Domain"

xcacChoice = MsgBox(xcacQuest, vbQuestion + vbYesNoCancel + vbDefaultButton6, xcactitle)

xcacLoc = "http://download.microsoft.com/download/f/7/8/f786aaf3-a37b-45ab-b0a2-8c8c18bbf483/XCacls_Installer.exe"

'IF THEY WANT US TO DOWNLOAD IT FOR THEM WE WILL PAUSE THE SCRIPT
'WE PAUSE BY CREATING A LOOP THAT WAITS FOR THE XCACLS INSTALER PROCESS TO TERMINATE

If xcacChoice = 6 Then

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancedeletionevent " _
            & "within 1 where TargetInstance isa 'Win32_Process'")

xcacDLWait = MsgBox("When the download finishes run XCacls_Installer.exe which will extract xcacls.vbs to the directory of your choice.  This script should automatically continue when the xcacls_installer exits.", vbOKOnly, xcactitle)

'BELOW WE LAUNCH AN IE PROCESS TO BEGIN THE FILE DOWNLOAD

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate xcacLoc

i = 0

'THE NEXT LINE IS WHERE WE SPECIFY THE PROCESS TO MONITOR FOR TERMINATION

Do While i <> "XCacls_Installer.exe"
    Set objLatestProcess = colMonitoredProcesses.NextEvent
i = objLatestProcess.TargetInstance.Name
Loop

ElseIf xcacChoice = vbCancel Then

xcacCancel = MsgBox("You can download xcacls.vbs by searching for it at http://www.microsoft.com/downloads", vbOKOnly, xcactitle)
wscript.quit()

End If

'GET THE PATH OF THE SYSTEM32 FOLDER so we can launch cscript
'JUST IN CASE THE PATH SYSTEM VARIABLE IS HORRIBLY WRONG
'OR BECAUSE WE'RE OBSESIVE COMPULIVE AND CAN'T LEAVE IT ALONE

Set objsysDR = objShell.Namespace(SYSTEM32)
Set objsysDRItem = objsysDR.Self
sys32 = objsysDRItem.Path

'GET THE PATH - ERR CLSID OF MY COMPUTER SO IT CAN BE USED AS THE ROOT
'WHEN WE BROWSE FOR FILES/FOLDERS

Set objMyComp = objShell.Namespace(MY_COMPUTER)
Set objMyCompItem = objMyComp.Self
BrowseRoot = objMyCompItem.Path

'OPEN BROWSE WINDOW SO USER CAN SPECIFY LOCATION OF XCACLS.VBS
'APPEND THE LOCATION WITH \XCACLS.VBS AND SET IT TO A VARIABLE

Do Until objFSO.FileExists(xcacls)

Set xcaclspath = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select the location of the xcacls.vbs file:", NO_OPTIONS, BrowseRoot)

If xcaclspath = Empty Then
wscript.quit()

ElseIf xcaclspath = "" Then
wscript.quit()
End If

Set xcaclspathItem = xcaclspath.Self
xcaclspath = xcaclspathItem.Path

xcacls = xcaclspath & "\xcacls.vbs"

If Not objFSO.FileExists(xcacls) Then

oUserError = MsgBox("xcacls.vbs does not exist in the folder you specified.  Please verify it's location and select the appropriate folder.", vbExclamation + vbOKOnly, HomeGTitle)
End If
Loop

'OPEN BROWSE WINDOW SO USER CAN SPECIFY LOCATION OF HOME FOLDERS (PARENT DIRECTORY)

Set objPath = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select the drive or folder containing the Home Directories to secure:", NO_OPTIONS, BrowseRoot)
Set objpathItem = objPath.Self
objPath = objpathItem.Path

'PROMPT USER FOR THE DOWNLEVEL DOMAIN NAME

Do Until ThisDom <> Empty
ThisDom = InputBox("Type the downlevel name of the domain the accounts associated with these home folders are in (ie no .com)", HomeGDomTitle)

If (ThisDom = Empty) Then

oUserError = MsgBox("The domain name is required and can not be blank.  Script will now exit.", vbExclamation + vbOKOnly, HomeGTitle)
wscript.quit()
End If
Loop

'PROMPT USER FOR NUMBER OF CHARACTERS TO IGNORE FROM THE LEFT

iCHleft = InputBox("In determining the account name, how many (if any) characters should be cropped from the left of the folder name?  For example if your naming convention is $$username then enter 2 here.  If the folder names begin with the account names enter 0.", HomeGTitle)

If (iCHleft = Empty) Then
lUserError = MsgBox("Script will now exit.  If you meant to specify not to skip any characters, run the script again and enter 0 next time", vbInformation + vbOKOnly, HomeGTitle)
wscript.quit()
End If

'PROMPT USER FOR NUMBER OF CHARACTERS TO IGNORE FROM THE RIGHT

iCHright = InputBox("In determining the account name, how many (if any) characters should be cropped from the right of the folder name?  For example if your naming convention is username$$ then enter 2 here.  If the folder names end with the account names enter 0.", HomeGTitle)

If (iCHright = Empty) Then
rUserError = MsgBox("Script will now exit.  If you meant to specify not to skip any characters, run the script again and enter 0 next time", vbInformation + vbOKOnly, HomeGTitle)
wscript.quit()
End If

'PROMPT USER FOR STRING TO MATCH

strFilter = InputBox("If you want to filter by folder name enter the string to match below.  Only folders whose names begin with the string will be changed.  For example if you enter the letter ""A"" (do not use quotes) then the folder named asmith would be changed but not the folder named janedoe. Leave blank to change all.", HomeGTitle)

If strFilter <> Empty Then
     rFormatFilter = LCase(strFilter)
filterAlive = 1
End If

'CREATE A COLLECTION OFF ALL SUB FOLDERS IN THE DIRECTORY SPECIFIED BY USER

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & objPath & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")


'SINCE WE USE APPACTIVATE TO TIGHTEN PERFORMANCE IT IS VERY IMPORTANT TO HAVE NO OTHER WINDOWS OPEN WHEN THIS RUNS
'THIS IS WHERE WE TELL THE USER THAT

xcacDLWait = MsgBox("Please make sure all other windows are closed - especially any folders you have open.  This is very important if you'd like this to work.  Please click ok when that's done and you're ready to continue.", vbInformation + vbOKOnly, xcactitle)

'HERE IS WHERE WE CHANGE FOLDER PERMISSIONS
'EVERYTHING BETWEEN THE FOR AND THE NEXT STATEMENT_
'WILL BE RUN ON EACH FOLDER IN THE COLLECTION ABOVE

For Each objFolder In colSubfolders

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(filespec) Then
    Set f = fso.GetFolder(filespec)
    s = f.Size
    If s = "" Then s = "0"
Else
    s = "0"
End If

tmpNum = Val(s)
If tmpNum > 0 Then
Set objFolders = objFSO.GetFolder(objFolder.Name)

     FID = objFolders.Name
     homefolder = objFolder.Name

'WE ARE PARSING EACH FOLDER NAME TO FIND IT'S
'ASSOCIATED USER ACCOUNT.  WE MUST ALSO IGNORE
'THE NUMBER OF CHARACTERS SPECIFIED BY USER (IF ANY)

'*******************************************
'THANKS TO Kenneth Bryant (kbryant@checksinthemail.com) AND
'Richard Stephen Reese (rsreese@ifas.ufl.edu)
'FOR HELPING TO RESOLVE AND ERROR IN THIS NEXT SECTION
'*******************************************

If iCHright > 0 Then

     uIDR = RTrim(Left(FID, Len(FID) - iCHright))
cRightnum = 1
End If

If cRightnum = 1 Then

     UID = LTrim(Right(uIDR, Len(uIDR) - iCHleft))

Else

     UID = LTrim(Right(FID, Len(FID) - iCHleft))

End If

'WE COMPARE FOLDER NAMES TO OUR FILTER STRING (IF ANY)

noMatch = 0
If filterAlive = 1 Then
checkFilter = Left(UID, Len(rFormatFilter))

If LCase(checkFilter) <> rFormatFilter Then
     noMatch = 1
End If
End If

'THIS IS THE COMMAND THAT RUNS THE XCACLS.VBS SCRIPT.
'THIS IS WHERE YOU CAN CHANGE WHAT PERMISSIONS ARE SET
'AND WHAT GROUPS THEY'RE SET FOR
'NOTE THAT OUTPUT LOGS TO c:\xcacls.txt
'750,000 FILES WILL GIVE YOU A 50 MEG LOG EASY

If noMatch = 0 Then

WshShell.run ("""" & sys32 & "\cscript.exe"" """ & xcacls & """ """ & homefolder & """ /f /s /t /g administrators:F /g " & ThisDom & "\" & UID & ":M /I remove /l c:\xcacls.txt /Q")

     wscript.sleep 200
WshShell.AppActivate "C:\"

Do While WshShell.AppActivate("C:\") = True
     wscript.sleep 200
Loop
End If

'THIS IS THE LAST LINE
End If
Next

0
 
iHadiCommented:
Just Change th filespec in the above code to your folder path somthing equevelant to objFolder.path
0
 
adamshieldsAuthor Commented:
I get an error 'Expected end of statement'

Line 9, Char 12
0
 
JesterTooCommented:
Remove the "As Long" from that Dim statement... VBScript is an "untyped" language (everything is a variant datatype).  Thus, you cannot dimension a variable to be a specific datatype.

HTH,
Lynn
0
 
adamshieldsAuthor Commented:
How do I specify the size of the folders. Meaning if a folder size is too great where do I specify the size and is it in bytes, kb, etc...?
0
 
adamshieldsAuthor Commented:
I'm not sure the function added will work but I believe this one will:

Option Explicit
 
  'Edit your folder path here.  The
  'script will enumerate all sub directories
  'under this folder.
  '-----------------------------------
  Dim fPath: fPath="D:\homes\"  
  '-----------------------------------
 
  'This is the path where the CSV file will be created.
  'Change this to suit your needs.
  '---------------------------------------------------
  dim fileOut: fileOut="D:\homes\Folders.csv"
  '---------------------------------------------------
 
 
  function getFolderMetrics()
   
    Dim fso
    Set fso=CreateObject("scripting.filesystemobject")
 
    Dim fldrObj, oFolders, f
   
    Set fldrObj = fso.GetFolder(fPath)
    Set oFolders=fldrObj.SubFolders
 
    dim fStream
    set fStream=fso.createTextFile(fileOut, true)
   
      For Each f In oFolders      
        fStream.writeline f.name & "," & f.size            
      Next
     
        fStream.close
   
    set fso=nothing
  end function
 
  getFolderMetrics()

How can this be incorporated?
0
 
edwardiiiCommented:
Hi, adamshields.

Sorry for not replying sooner--please ignore my reference to a Userform; I didn't realize you were running this in Web page.  Regarding my comment about a folder-processing routine, I presumed you knew where that code was, and could just have the loop I referenced do nothing when processing folders over a certain size.
0
All Courses

From novice to tech pro — start learning today.