更改脚本以将结果写入文件

时间:2015-01-12 13:04:59

标签: vbscript

我有以下脚本收集了我需要的所有信息,但我想知道如何更改它以将窗口中显示的所有结果打印到某种列表中?我想在excel中操纵这些数据。

Dim arrHeaders(35)

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:directory")

For i = 0 to 34
  arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
Next

For Each strFileName in objFolder.Items
  For i = 0 to 34
    Wscript.Echo i & vbtab & arrHeaders(i) _
      & ": " & objFolder.GetDetailsOf(strFileName, i)
  Next
Next

2 个答案:

答案 0 :(得分:0)

收集另一个数组中的详细信息,并以逗号分隔的行回显每条记录:

Dim arrData(35)
...

WScript.Echo Join(arrHeaders, ",")

For Each strFileName in objFolder.Items
  For i = 0 to 34
    arrData(i) = objFolder.GetDetailsOf(strFileName, i)
  Next
  WScript.Echo Join(arrData, ",")
Next

通过这样运行带有cscript.exe的脚本,您可以将输出重定向到CSV文件:

cscript //NoLogo "C:\path\to\your.vbs" > "C:\path\to\your.csv"

然后可以使用Excel打开CSV文件。

另一种选择是使用FileSystemObject从脚本中编写输出文件:

Dim arrData(35)
...

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\path\to\your.csv", 2, True)

f.WriteLine Join(arrHeaders, ",")

For Each strFileName in objFolder.Items
  For i = 0 to 34
    arrData(i) = objFolder.GetDetailsOf(strFileName, i)
  Next
  f.WriteLine Join(arrData, ",")
Next

f.Close

答案 1 :(得分:-1)

您不需要修改脚本以将输出发送到文件。只需打开控制台,导航到脚本位置并运行脚本,将控制台的输出重定向到文件

myvbscript.vbs > myoutputfile.txt

请参阅:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true