'JCSetter.vbs(Java CLASSPATH Setter)<br />'t0nsha<liaodunxia#gmail.com><br />'v1.0@2010/3/20 第一版<br />'v1.1@2010/4/4對未設定CLASSPATH環境變數的情況進行了修正.</p><p>Option Explicit<br />Public Const g_program = "JCSetter"<br />Public Const g_delimiter = ";"<br />Public Const g_classpath = "%CLASSPATH%"</p><p>'得到副檔名<br />Function GetFileExtensions(fileName)<br />GetFileExtensions = Mid(fileName, InStrRev(fileName, ".") + 1)<br />End Function</p><p>'遍曆指定檔案夾下的所有指定檔案<br />Function GetFilenames(currentFolderFullPath)<br />Dim fso, currentFolder, currentFileSet, currentFile, s<br />Set fso = CreateObject("Scripting.FileSystemObject")<br />Set currentFolder = fso.GetFolder(currentFolderFullPath)<br />Set currentFileSet = currentFolder.Files<br />For Each currentFile In currentFileSet<br />If LCase(GetFileExtensions(currentFile.Name)) = "jar" Then<br />s = s & currentFile.Path & g_delimiter<br />End If<br />Next<br />GetFilenames = s<br />End Function</p><p>'得到指令檔所在的目前的目錄<br />Function GetCurrentFolderFullPath<br />Dim fso<br />Set fso = CreateObject("Scripting.FileSystemObject")<br />GetCurrentFolderFullPath = fso.GetParentFolderName(WScript.ScriptFullName)<br />End Function</p><p>'遞迴遍曆目前的目錄下的所有檔案夾和子檔案夾<br />Function GetSubFolders(currentFolderFullPath)<br />Dim fso, currentFolder, subFolderSet, subFolder<br />Set fso = CreateObject("Scripting.FileSystemObject")<br />Set currentFolder = fso.GetFolder(currentFolderFullPath)<br />Set subFolderSet = currentFolder.SubFolders<br />For Each subFolder in subFolderSet<br />'MsgBox "subFolder.Path=" & subFolder.Path<br />GetSubFolders = subFolder.Path & g_delimiter & GetSubFolders(subFolder.Path) & GetSubFolders<br />Next<br />End Function</p><p>Dim arr, s, i, WshShell, cp<br />arr = Split(GetSubFolders(GetCurrentFolderFullPath), g_delimiter)<br />'arr最後一個元素為空白<br />For i = LBound(arr) To UBound(arr) - 1<br />s = s & GetFilenames(arr(i))<br />Next<br />'將搜尋到的jar包添加到CLASSPATH<br />Set WshShell = WScript.CreateObject("WScript.Shell")<br />'s = WshShell.ExpandEnvironmentStrings("%CLASSPATH%") & g_delimiter & s<br />cp = WshShell.ExpandEnvironmentStrings(g_classpath)<br />If cp = g_classpath Then<br />cp = "." & g_delimiter & s<br />Else<br />cp = "." & g_delimiter & s & g_delimiter & cp<br />End If<br />WshShell.Environment("User").Item("CLASSPATH") = cp<br />'MsgBox "[" & cp & "] has been added to CLASSPATH!"<br />MsgBox "The environment variable [CLASSPATH] includes:" & vbCrLf & vbCrLf & cp, vbOKOnly, g_program