【问题标题】:pass arraylist from VBscript to BAT file将数组列表从 VBscript 传递到 BAT 文件
【发布时间】:2015-03-18 18:05:40
【问题描述】:

我有一个包含一些名称的 txt 文件。我需要这些名字 由 VBscript 读取,将它们存储在数组列表中并将其传递给 BAT 文件。 如何将数组列表传递给 BAT 文件?

这是vbscript:

Dim objFile, strLine(), WshShell, intsize
intSize = 0 
Redim Preserve strLine(intsize)
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.OpenTextFile("read.txt", 1)
Do While Not objFile.AtEndOfStream
  strLine(intsize) = objFile.readline
  ReDim Preserve strLine(intSize + 1)
Loop
set WshShell=Wscript.Createobject("Wscript.shell")
    Wshshell.run "test.bat " & strLine(intSize)
objFile.Close

bat 文件

@echo off
echo %1

【问题讨论】:

  • 为什么一定要使用vbscript?您可以直接从文件批量创建数组。

标签: windows batch-file vbscript


【解决方案1】:

Join 函数返回一个字符串,该字符串通过连接数组中包含的多个子字符串创建。

语法Join( list[, delimiter])

参数

  • list:必填。包含要连接的子字符串的一维数组。
  • delimiter:可选。用于分隔返回字符串中的子字符串的字符串字符。如果省略,则使用空格字符 (" ")。如果 delimiter 是一个长度为零的字符串,则列表中的所有项目都连接起来,没有分隔符。

" 括号括住列表会导致它在调用的批处理中显示为唯一的参数(%1)。您的run 方法行可能如下:

Wshshell.run "test.bat """ & Join( strLine, ";") & """"

附带说明:循环中缺少数组索引递增;这是一个几乎防弹的代码sn-p:

intSize = -1
ReDim strLine( 0)
strLine( 0) = ""
Do While Not objFile.AtEndOfStream
  intSize = intSize + 1
  ReDim Preserve strLine( intSize)
  strLine( intsize) = objFile.readline
Loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2021-05-08
    • 2012-05-19
    • 1970-01-01
    相关资源
    最近更新 更多