Last active
September 29, 2017 22:09
-
-
Save simply-coded/4e92794829127670c9ea11cb21862a00 to your computer and use it in GitHub Desktop.
Loops through all folders and subfolders under a root directory and runs them through a routine of your choice.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function EachFolder(strRoot, includeRoot, funcRef, funcArgs, oFSO) | |
' @author: Jeremy England ( SimplyCoded ) | |
' @description: Loops through all folders and subfolders under a root directory. | |
Dim oFolder, changeProtection | |
If oFSO.FolderExists(strRoot) Then | |
For Each oFolder In oFSO.GetFolder(strRoot).SubFolders | |
changeProtection = oFolder.Path | |
EachFolder = funcRef(oFolder, funcArgs) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
If UCase(EachFolder) = "SKIP_PARENT" Then Exit For | |
If UCase(EachFolder) = "SKIP_FOLDER" Then changeProtection = False | |
If oFSO.FolderExists(changeProtection) Then | |
EachFolder = EachFolder(changeProtection, False, funcRef, funcArgs, oFSO) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
End If | |
Next | |
If includeRoot Then | |
funcRef oFSO.GetFolder(strRoot), funcArgs | |
End If | |
End If | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
' Visually see how EachFolder recurses through your directory. | |
' | |
Option Explicit | |
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") | |
Dim startFolder, again | |
'Where to start the recursion from | |
startFolder = "c:\location\of\start\folder" | |
'Start the recursion | |
EachFolder startFolder, False, GetRef("HowItRecurses"), Null, fso | |
again = MsgBox("Go again but also include the start folder?", vbYesNo + vbQuestion + vbDefaultButton2) | |
If again = vbYes Then | |
EachFolder startFolder, True, GetRef("HowItRecurses"), Null, fso | |
End If | |
'Recursion complete | |
MsgBox "Complete!", vbInformation | |
'-------------------------------------------------------------------------------- | |
Function HowItRecurses(oFolder, args) | |
Dim continue | |
continue = MsgBox(oFolder.Path, vbOKCancel, oFolder.Name) | |
If continue = vbCancel Then | |
HowItRecurses = "SKIP_ALL" | |
End If | |
End Function | |
'================================================================================ | |
Function EachFolder(strRoot, includeRoot, funcRef, funcArgs, oFSO) | |
' @author: Jeremy England ( SimplyCoded ) | |
' @description: Loops through all folders and subfolders under a root directory. | |
Dim oFolder, changeProtection | |
If oFSO.FolderExists(strRoot) Then | |
For Each oFolder In oFSO.GetFolder(strRoot).SubFolders | |
changeProtection = oFolder.Path | |
EachFolder = funcRef(oFolder, funcArgs) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
If UCase(EachFolder) = "SKIP_PARENT" Then Exit For | |
If UCase(EachFolder) = "SKIP_FOLDER" Then changeProtection = False | |
If oFSO.FolderExists(changeProtection) Then | |
EachFolder = EachFolder(changeProtection, False, funcRef, funcArgs, oFSO) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
End If | |
Next | |
If includeRoot Then | |
funcRef oFSO.GetFolder(strRoot), funcArgs | |
End If | |
End If | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
' Interactively see how regular and special return values work. | |
' | |
Option Explicit | |
Dim startFolder, includeStartFolder, inputArgs, returnedValue | |
'Where to start the recursion from | |
startFolder = "c:\location\of\start\folder" | |
'Also pass startFolder into HowItReturns function? | |
includeStartFolder = True | |
'Arguments for the HowItReturns function | |
inputArgs = Array(startFolder, "") | |
'Start the recursion | |
EachFolder _ | |
startFolder, _ | |
includeStartFolder, _ | |
GetRef("HowItReturns"), _ | |
inputArgs, _ | |
CreateObject("Scripting.FileSystemObject") | |
'Recursion complete | |
MsgBox "Complete!", vbInformation | |
'Retrieve data stored in the inputArgs array by HowItReturns | |
Dim returned | |
returned = inputArgs(1) | |
'Display the data | |
MsgBox returned, 0, "Heres what HowItReturns() saved" | |
'-------------------------------------------------------------------------------- | |
Function HowItReturns(oFolder, args) | |
Dim returnValue, shortPath | |
'Shorten the path to start always at the { startFolder } | |
shortPath = Mid(oFolder.Path, Len(args(0)) + 1) | |
'Display an inputbox that the user can return messages from each folder with | |
returnValue = InputBox(shortPath & vbLf & vbLf & "Type to return something from this folder." _ | |
& vbLf & vblf & "Special return strings:" & vbLf & "[SKIP_ALL, SKIP_PARENT, SKIP_FOLDER]", "PARENT: " _ | |
& oFolder.ParentFolder.Name & " | FOLDER: " & oFolder.Name, oFolder.Name) | |
'If cancel button clicked | |
If IsEmpty(returnValue) Then | |
WScript.Quit | |
'If special return values used then return them to EachFolder for processing | |
ElseIf UCase(returnValue) = "SKIP_ALL" Or UCase(returnValue) = "SKIP_PARENT" Or UCase(returnValue) = "SKIP_FOLDER" Then | |
HowItReturns = returnValue | |
'Save the folder names from the inputbox or whatever the user inputed | |
Else | |
'Appends info to inputArgs(1). | |
'You can change how and what it stores to whatever you want. | |
args(1) = args(1) & returnValue & ", " | |
End If | |
End Function | |
'================================================================================ | |
Function EachFolder(strRoot, includeRoot, funcRef, funcArgs, oFSO) | |
' @author: Jeremy England ( SimplyCoded ) | |
' @description: Loops through all folders and subfolders under a root directory. | |
Dim oFolder, changeProtection | |
If oFSO.FolderExists(strRoot) Then | |
For Each oFolder In oFSO.GetFolder(strRoot).SubFolders | |
changeProtection = oFolder.Path | |
EachFolder = funcRef(oFolder, funcArgs) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
If UCase(EachFolder) = "SKIP_PARENT" Then Exit For | |
If UCase(EachFolder) = "SKIP_FOLDER" Then changeProtection = False | |
If oFSO.FolderExists(changeProtection) Then | |
EachFolder = EachFolder(changeProtection, False, funcRef, funcArgs, oFSO) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
End If | |
Next | |
If includeRoot Then | |
funcRef oFSO.GetFolder(strRoot), funcArgs | |
End If | |
End If | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
' Collects all file paths in an entire directory. | |
' | |
Option Explicit | |
Dim startDir, andStart, paths, pathFilter | |
'Where to start the search from | |
startDir = "c:\location\of\start\folder" | |
'Also look for files in startDir? | |
andStart = True | |
'All files will be saved in this dictionary | |
Set paths = CreateObject("Scripting.Dictionary") | |
'Only get files with these extensions. Use "." for all files | |
Set pathFilter = New RegExp | |
pathFilter.Pattern = "(\.png|\.jpg|\.bmp|\.jpeg)$" | |
'Start recursion | |
EachFolder startDir, andStart, GetRef("CollectFiles"), _ | |
Array(paths, pathFilter), CreateObject("Scripting.FileSystemObject") | |
MsgBox paths.Count & " files found." | |
'-------------------------------------------------------------------------------- | |
Function CollectFiles(oFolder, args) | |
Dim oFile | |
For Each oFile in oFolder.Files | |
'If path matches the regular expression | |
If args(1).Test(oFile.Path) Then | |
'Add them to paths dictionary | |
args(0).Add args(0).Count, oFile.Path | |
End If | |
Next | |
End Function | |
'================================================================================ | |
Function EachFolder(strRoot, includeRoot, funcRef, funcArgs, oFSO) | |
' @author: Jeremy England ( SimplyCoded ) | |
' @description: Loops through all folders and subfolders under a root directory. | |
Dim oFolder, changeProtection | |
If oFSO.FolderExists(strRoot) Then | |
For Each oFolder In oFSO.GetFolder(strRoot).SubFolders | |
changeProtection = oFolder.Path | |
EachFolder = funcRef(oFolder, funcArgs) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
If UCase(EachFolder) = "SKIP_PARENT" Then Exit For | |
If UCase(EachFolder) = "SKIP_FOLDER" Then changeProtection = False | |
If oFSO.FolderExists(changeProtection) Then | |
EachFolder = EachFolder(changeProtection, False, funcRef, funcArgs, oFSO) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
End If | |
Next | |
If includeRoot Then | |
funcRef oFSO.GetFolder(strRoot), funcArgs | |
End If | |
End If | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
' Deletes all empty folders and subfolders in a directory. | |
' | |
Option Explicit | |
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") | |
Dim rootDir, andRoot, deleteArgs, limitDeletions | |
rootDir = "c:\location\of\start\folder" | |
'Delete root folder if it is empty? | |
andRoot = False | |
limitDeletions = 20 | |
'Set things up | |
deleteArgs = Array(fso, False, 0, limitDeletions) | |
'Start recursion | |
Do | |
deleteArgs(1) = False | |
EachFolder rootDir, andRoot, GetRef("DeleteIfEmpty"), deleteArgs, fso | |
'Continue until no more deletions are made | |
Loop While deleteArgs(1) | |
MsgBox deleteArgs(2) & " empty folders deleted.", vbInformation, "Success!" | |
'-------------------------------------------------------------------------------- | |
Function DeleteIfEmpty(oFolder, args) | |
'Delete if empty or only file is "desktop.ini" | |
If (oFolder.Size = 0) Or (oFolder.Files.Count = 1 _ | |
And args(0).FileExists(oFolder.Path & "\desktop.ini")) Then | |
'Limit deletions check | |
If args(2) >= args(3) Then DeleteIfEmpty = "SKIP_ALL" : Exit Function | |
'Delete folder | |
oFolder.Delete True | |
'Deletion counter | |
args(2) = args(2) + 1 | |
'Deletion occurred | |
args(1) = True | |
End If | |
End Function | |
'================================================================================ | |
Function EachFolder(strRoot, includeRoot, funcRef, funcArgs, oFSO) | |
' @author: Jeremy England (SimplyCoded). | |
' @description: Loops through all folders and subfolders under a root directory. | |
Dim oFolder, changeProtection | |
If oFSO.FolderExists(strRoot) Then | |
For Each oFolder In oFSO.GetFolder(strRoot).SubFolders | |
changeProtection = oFolder.Path | |
EachFolder = funcRef(oFolder, funcArgs) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
If UCase(EachFolder) = "SKIP_PARENT" Then Exit For | |
If UCase(EachFolder) = "SKIP_FOLDER" Then changeProtection = False | |
If oFSO.FolderExists(changeProtection) Then | |
EachFolder = EachFolder(changeProtection, False, funcRef, funcArgs, oFSO) | |
If UCase(EachFolder) = "SKIP_ALL" Then Exit Function | |
End If | |
Next | |
If includeRoot Then | |
funcRef oFSO.GetFolder(strRoot), funcArgs | |
End If | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment