可以用作参数的文本长度是否有限制?如果是,限制是多少?

Is there a limit in the text length that you can use as a parameter? If yes whats the limit?

我有一个 VB脚本,它接受 5 个参数作为命令行参数。 5 个参数中的两个包含某些 .txt 文件的完整绝对路径,因此命令行参数长度可能会变得很长,在这种情况下我的自动化脚本可能会失败。

谁能告诉我我们是否对要在命令行中为 VBScript 传递的文本长度有任何限制? 其实我想知道,从VB脚本的角度来看是否有限制?

我运行脚本如下:

cscript.exe Sample.vbs "C:\Program Files\z.txt" param2 param3 D:\abcd.txt param5

我发现了这个:http://blogs.msdn.com/b/oldnewthing/archive/2003/12/10/56028.aspx

但最好的办法是亲自测试一下。尝试使用超长字符串调用它,然后在 vb 脚本中输出字符串,或输出字符串的长度。我认为您不会遇到文件路径长度方面的问题。

a.vbs

Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")

Dim arguments
For i = 1 To 6540
  arguments = arguments & LPad(i,4,"0") & ","
Next

objShell.Run "b.vbs " & arguments

' Using Set is mandatory
Set objShell = Nothing


Function LPad(s, l, c)
  Dim n : n = 0
  If l > Len(s) Then n = l - Len(s)
  LPad = String(n, c) & s
End Function

b.vbs

WriteString "C:\temp\vbscripttest\c.txt",WScript.Arguments.Item(0) 

Function WriteString( filename, contents )
    Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile( filename,2,true)
    objFileToWrite.WriteLine(contents)
    objFileToWrite.Close
    Set objFileToWrite = Nothing
End Function

它的最大值为 6540 * 5 个字符 = 32700。如果你愿意,你可以多玩玩它。如果我输入 6541,我得到:


Windows 脚本宿主

脚本:C:\temp\vbscripttest\a.vbs 线路:9 字符:1 错误:文件名或扩展名太长。 代码:800700CE 来源:(空)


好的