在 VBS 中使用命令行参数

Use command-line arguments in VBS

我正在使用以下 VBS 脚本从文件中删除行的前 n 行:

strInputFile = "*Filename.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
If Not objInputFile.AtEndOfStream Then
      objInputFile.SkipLine
Else
      WScript.Quit
End If
strContents = ""
While Not objInputFile.AtEndOfStream
      If strContents = "" Then
            strContents = objInputFile.ReadLine
      Else
            strContents = strContents & VbCrLf & objInputFile.ReadLine
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile(strInputFile, True)
objOutputFile.Write strContents
objOutputFile.Close
Set objOutputFile = Nothing
Set objFSO = Nothing

如何更改代码,以便在我通过 CMD 启动程序时将其作为参数而不是常量输入文件?

改变

strInputFile = "*filename.txt" 

strInputFile = WScript.Arguments(0)

第一个参数是文件名。

查看 wscript.arguments,它提供对用于启动脚本的命令行的访问。

if (wscript.arguments.count <> 1) then
    wscript.echo "Usage: dl2unc <drive-letter-path>"
    wscript.quit 1
end if

s = wscript.arguments.Item(0)