Posted by member 5575 on 2004-08-22 01:48:05
' sortDocs.vbs
' - A script to organize module documentation (files and folders) alphabetically
' so that the documentation menu doesn't grow outrageously large. Point this
' script at your module docs folder by:
' 1) Dragging the folder onto the script in the file manager
' 2) Input the docs root folder through a dialog box
' 3) Command line, e.g., wscript sortDocs.vbs "C:\Litestep\modules\docs"
'===============================================================================
Dim FSO, ScriptDir, RootDir, OutputFile
Dim LastSlash, FolderName, contents
Set FSO = CreateObject("Scripting.FileSystemObject")
ScriptDir = FSO.GetFile(Wscript.ScriptFullName).ParentFolder
'-- RootDir - top of docs file hierarchy. Initialized only here - input below.
RootDir = ""
'--- Get folder dropped on script or use InputBox:
If WScript.Arguments.count > 0 Then
RootDir = WScript.Arguments.item(0)
Else
Do while Len(RootDir) = 0
RootDir = InputBox("Enter path to docs folder", "Select docs folder")
Loop
End If
If (not FSO.FolderExists(RootDir)) Then
msgbox "Unable to locate input folder:"&_
vbcrlf&vbcrlf&RootDir&_
vbcrlf&vbcrlf&"Exiting."
Set FSO = Nothing
WScript.Quit
End If
'--- Strip trailing slash, and check to make sure the input is a folder and not
'--- a drive root, e.g., "M:\". The folder recursion doesn't tolerate that. =(
If (Right(RootDir,1) = "\") Then
If (Len(RootDir) = 3) Then
msgbox "Unable to recurse drive root - sorry!"&_
vbcrlf&vbcrlf&"Exiting."
Set FSO = Nothing
WScript.Quit
End If
RootDir = Left(RootDir,Len(RootDir)-1)
End If
'--- Truncate path to last folder name.
LastSlash = InStrRev(RootDir,"\",-1,1)
FolderName = Right(RootDir,Len(RootDir)-LastSlash)
Set OutputFile = FSO.CreateTextFile(ScriptDir&"\"&FolderName&".txt")
'--- Recursively scan the folder and organize it
sortFolder RootDir
'--- That's it.
Set OutputFile = Nothing
Set FSO = Nothing
Wscript.Quit
'===============================================================================
'===============================================================================
sub sortFolder(InputFolder)
Dim ThisFolder, TheseSubs
'----- Open the parent folder
Set ThisFolder = FSO.GetFolder(InputFolder)
Set TheseSubs = ThisFolder.SubFolders
'--- Process files in this folder
If ThisFolder.Files.Count 0 Then
For each File in ThisFolder.Files
If (isDocs(File.Name)) Then
firstLetter = Lcase(Left(File.Name,1))
destFolder = InputFolder&"\"&firstLetter&"\"
If (not FSO.FolderExists(destFolder)) Then
OutputFile.WriteLine("Creating folder: "&destFolder)
set dest = FSO.CreateFolder(destFolder)
End If
destPath = destFolder&File.Name
If (not FSO.FileExists(destPath)) Then
OutputFile.WriteLine("Moving file: "&File.Path&" -> "&destFolder)
FSO.MoveFile File.Path, destFolder
Else
msgbox "File already exists:"&vbcrlf&vbcrlf&destPath&vbcrlf&vbcrlf&_
"Deleting duplicate:"&vbcrlf&vbcrlf&File.Path
FSO.DeleteFile File.Path
End If
End If
Next
End If
'--- Process subfolders
If TheseSubs.Count 0 Then
For each Folder in TheseSubs
If (isDocs(Folder.Name)) Then
firstLetter = Lcase(Left(Folder.Name,1))
destFolder = InputFolder&"\"&firstLetter&"\"
If (not FSO.FolderExists(destFolder)) Then
OutputFile.WriteLine("Creating folder: "&destFolder)
set dest = FSO.CreateFolder(destFolder)
End If
OutputFile.WriteLine("Moving folder: "&Folder.Path&" -> "&destFolder)
FSO.MoveFolder Folder.Path, destFolder
End If
Next
End If
Set ThisFolder = Nothing
Set TheseSubs = Nothing
end sub 'sortFolder
'===============================================================================
' Function to determine whether a given file is 'docs'. Edit the file name
' matching rules below as you see fit. Currently we assume everything in that
' folder is docs if its name is more than one character long; single characters
' (a-z) are used for the names of the folders into which the docs are sorted.
'===============================================================================
function isDocs(Name)
isDocs = false
If ( Len(Name) > 1 ) Then
isDocs = true
End If
end function 'isDocs