VB中准确的获得命令行参数列表的方法, 支持引号括起来的带空格的参数 Private Sub Form_Load() 'GUI下测试 'cmd = "-a " & Chr(34) & "c:/a 1.txt" & Chr(34) & " " & Chr(34) & "d:/b c.txt" & Chr(34) 'cmd = "-r " & Chr(34) & "c:/123/a.txt" & Chr(34) & " " & "c:/123/b.txt" cmd = Trim(Command) If cmd = "" Then '语法不正确 - 参数为空 '参数为空的具体处理。。。。。。 Unload Me ' 不加载窗口,即不显示 Exit Sub End If 'Args 为得到的参数数组 Args = GetCommandLine(cmd) ' 调试 MsgBox ("Debug-Command:" & cmd) MsgBox ("Debug-ArgCount:" & UBound(Args) + 1) For I = 0 To UBound(Args) MsgBox (I & ":" & Args(I)) Next I '对参数数组Args中的参数根据需要进行校验 '...... Unload Me ' 不加载窗口,即不显示 End Sub ' 获得命令行参数(最多3个参数),并将命令行参数以 Variant 类型之数组返回。 Function GetCommandLine(ByVal CmdLine As String) '声明变量。 Dim CmdLnLen, FirstFile, SecondFile, StartPos, StrLeft '取得命令行参数。 CmdLine = Trim(CmdLine) CmdLnLen = Len(CmdLine) '取得引号中的字符串,最多两对引号 FirstArr = GetStrBetweenQuotes(CmdLine) If FirstArr(0) = "RetCode0" Or FirstArr(0) = "RetCode1" Then GetCommandLine = StrSplit(CmdLine, " ") Exit Function Else FirstFile = FirstArr(0) '取第二对引号中的字符串 StartPos = FirstArr(1) StrLeft = Mid(CmdLine, StartPos + 1) SecondArr = GetStrBetweenQuotes(StrLeft) If SecondArr(0) = "RetCode1" Then GetCommandLine = StrSplit(CmdLine, " ") Exit Function ElseIf SecondArr(0) = "RetCode0" Then '没有第二对引号,SecondFile就是第二次处理的字符串 SecondFile = Trim(StrLeft) Else SecondFile = SecondArr(0) End If End If '将引号中的字符串替换掉 temp = Replace(CmdLine, FirstFile, "*1") 'MsgBox ("Debug-temp:" & temp) temp = Replace(temp, SecondFile, "*2") 'MsgBox ("Debug-temp:" & temp) '到此,已经不存在引号中含有空格的情况,但仍有可能有引号 temp = Replace(CmdLine, Chr(34), "") '去掉引号 ArgArray = StrSplit(temp, " ") For I = 0 To UBound(ArgArray) ArgArray(I) = Replace(ArgArray(I), "*1", FirstFile) '还原 ArgArray(I) = Replace(ArgArray(I), "*2", SecondFile) '还原 Next I GetCommandLine = ArgArray End Function '取得一对引号中的字符串,返回一个长度为2数组, '数组中第一个元素是引号中的字符串,第二个元素是第二个引号的位置 Private Function GetStrBetweenQuotes(ByVal src As String) As Variant Dim ResultArray(1) quote = Chr(34) FirstQuotePos = InStr(1, src, quote) 'MsgBox ("Debug-src:" & src & ",FirstQuotePos:" & FirstQuotePos) If FirstQuotePos = 0 Then '没有引号,退出 ResultArray(0) = "RetCode0" GetStrBetweenQuotes = ResultArray() Exit Function Else '有引号,说明文件路径中有空格 SecondQuotePos = InStr(FirstQuotePos + 1, src, quote) 'MsgBox ("Debug-src:" & src & ",SecondQuotePos:" & SecondQuotePos) If SecondQuotePos = 0 Then '没有第二个引号,语法不正确,引号必须成对出现 ResultArray(0) = "RetCode1" GetStrBetweenQuotes = ResultArray() Exit Function Else '有第二个引号,取出第一对引号中的数据 FirstFile = Mid(src, FirstQuotePos, SecondQuotePos - FirstQuotePos + 1) End If End If ResultArray(0) = FirstFile ResultArray(1) = SecondQuotePos 'MsgBox ("Debug-ResultArray(0)(FirstFile):" & ResultArray(0) & ",ResultArray(1):" & ResultArray(1)) GetStrBetweenQuotes = ResultArray() End Function '分割参数字符串为参数数组 Private Function StrSplit(ByVal src As String, ByVal delimiter As String) As Variant src = Trim(src) Arr = Split(src, delimiter) Dim result As String For I = 0 To UBound(Arr) j = Arr(I) If j <> "" Or j <> " " Then If result <> "" Then result = result & "," & j Else result = j End If End If Next I StrSplit = Split(result, ",") End Function