【问题标题】:Read value one by one by using VBScript使用VBScript一一读取值
【发布时间】:2018-10-25 20:26:39
【问题描述】:

我有一个属性文件如下:

FileToRead=project.xml,CheckFile.ini RunTimeFile=MyRuntime.xml

我想通过VBScript的For循环,一一读取参数FileToRead的逗号分隔值。

我已经尝试了一些这样的示例进行测试的脚本:

Set fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1
Const ForWriting = 2

listFile = fso.OpenTextFile("MyFile.properties").ReadLine
listcheck = Split(listFile, vbCrLf)

For Each MyValue In listcheck
    segments = Split(line, ",")
    WScript.Echo "SimpleCheck" 
Next

有没有可能在 VBS 的 If 块内使用 Or 条件?

【问题讨论】:

  • 你的脚本只读取文件的第一行,这是故意的吗?
  • 我想从属性文件中读取两个参数,而不仅仅是第一行。如果我们指定 Key 并读取 Value 就可以了。
  • 你需要做的比你的代码现在做的要多得多。一方面,如果您之后要在换行符处拆分文件的内容,您应该使用ReadAll 而不是ReadLine。然后你需要在= 处分割每一行,这样你就可以将值与键分开。然后拆分, 处的值并使用第二个For Each 循环处理它们。
  • 是否可以使用属性名称 "FileToRead" 检索属性 "FileToRead" 的值? (就像我们在 ANT 中做 ${FileToRead} 一样)使用拆分,我可以分离属性名称和值,但如果能够使用属性名称获取值,那就太好了。

标签: vbscript properties


【解决方案1】:

你可以使用类似的东西

Set fso = CreateObject("Scripting.FileSystemObject")

'Dictionary to store all the lines in form of key value pair
Set dict = CreateObject("Scripting.Dictionary")

Set file = fso.OpenTextFile ("<InputFilePath>", 1)

'Reading the file and storing properties in the dictionary
Do Until file.AtEndOfStream
  line = file.Readline
  splittedLine = Split(line,"=")
  if UBound(splittedLine)=1 then
    dict.Add splittedLine(0),splittedLine(1)
  end if
Loop
file.Close

'Now get the value from dictionary using key name FileToRead
valFileToRead = dict.item("FileToRead")
arrFiles=Split(valFileToRead,",")
for i=0 to UBound(arrFiles)
  'Do anything with the values
  msgBox(arrFiles(i))
next

是的,你可以在 if 中使用 OR 关键字

if Condition1 OR Condition 2 then
   'Code
end if

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多