'JCSetter.vbs(Java CLASSPATH Setter)
't0nsha<liaodunxia#gmail.com>
'v1.0@2010/3/20 第一版
'v1.1@2010/4/4 对未设置CLASSPATH环境变量的情况进行了修正.
Option Explicit
Public Const g_program = "JCSetter"
Public Const g_delimiter = ";"
Public Const g_classpath = "%CLASSPATH%"
'得到文件扩展名
Function GetFileExtensions(fileName)
GetFileExtensions = Mid(fileName, InStrRev(fileName, ".") + 1)
End Function
'遍历指定文件夹下的所有指定文件
Function GetFilenames(currentFolderFullPath)
Dim fso, currentFolder, currentFileSet, currentFile, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set currentFolder = fso.GetFolder(currentFolderFullPath)
Set currentFileSet = currentFolder.Files
For Each currentFile In currentFileSet
If LCase(GetFileExtensions(currentFile.Name)) = "jar" Then
s = s & currentFile.Path & g_delimiter
End If
Next
GetFilenames = s
End Function
'得到脚本文件所在的当前目录
Function GetCurrentFolderFullPath
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetCurrentFolderFullPath = fso.GetParentFolderName(WScript.ScriptFullName)
End Function
'递归遍历当前目录下的所有文件夹和子文件夹
Function GetSubFolders(currentFolderFullPath)
Dim fso, currentFolder, subFolderSet, subFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set currentFolder = fso.GetFolder(currentFolderFullPath)
Set subFolderSet = currentFolder.SubFolders
For Each subFolder in subFolderSet
'MsgBox "subFolder.Path=" & subFolder.Path
GetSubFolders = subFolder.Path & g_delimiter & GetSubFolders(subFolder.Path) & GetSubFolders
Next
End Function
Dim arr, s, i, WshShell, cp
arr = Split(GetSubFolders(GetCurrentFolderFullPath), g_delimiter)
'arr最后一个元素为空
For i = LBound(arr) To UBound(arr) - 1
s = s & GetFilenames(arr(i))
Next
'将搜索到的jar包添加到CLASSPATH
Set WshShell = WScript.CreateObject("WScript.Shell")
's = WshShell.ExpandEnvironmentStrings("%CLASSPATH%") & g_delimiter & s
cp = WshShell.ExpandEnvironmentStrings(g_classpath)
If cp = g_classpath Then
cp = "." & g_delimiter & s
Else
cp = "." & g_delimiter & s & g_delimiter & cp
End If
WshShell.Environment("User").Item("CLASSPATH") = cp
'MsgBox "[" & cp & "] has been added to CLASSPATH!"
MsgBox "The environment variable [CLASSPATH] includes:" & vbCrLf & vbCrLf & cp, vbOKOnly, g_program
原文链接: http://blog.youkuaiyun.com/t0nsha/article/details/5399782