VBScript:数组排序 Sort Array 【1】

例如,需要获取Windows环境变量PATH,分隔之后排序输出

'filename: for-each-loop.vbs
'Get
Set wshShell = CreateObject( "WScript.Shell" )
paths_str = wshShell.ExpandEnvironmentStrings("%PATH%")
paths = Split(paths_str,";")

'Sort
set scal =CreateObject("System.Collections.ArrayList")
For Each path In  paths
  scal.Add(path)  
Next
scal.Sort()
paths_arr = scal.toArray

'Display
For Each path In paths_arr
  WScript.Echo path
Next

outputs

Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.


%SystemRoot%
%SystemRoot%\system32
%SystemRoot%\System32\Wbem
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
%USERPROFILE%\.cargo\bin
%USERPROFILE%\AppData\Local\atom\bin
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
%USERPROFILE%\AppData\Local\Programs\Fiddler
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Program Files (x86)\Pandoc\
C:\Program Files (x86)\Python36-32\
C:\Program Files (x86)\Python36-32\Scripts\
C:\Program Files\Microsoft VS Code\bin
C:\Program Files\Sublime Text 3
C:\Program Files\Sublime Text 3
C:\ProgramData\chocolatey\bin
C:\rakudo\bin
C:\rakudo\share\perl6\site\bin
C:\Windows
C:\Windows\system32
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
D:\PHP-7.1.7

如果提示

for-each-loop.vbs(6, 1) Microsoft VBScript runtime error: 
ActiveX component can't create object: 'System.Collectinos.ArrayList'

需要安装.Net Framework 3.5

VBScript:数组排序 Sort Array 【1】_第1张图片
安装完成.Net Framework 3.5

要trigger安装提示的话,新建一个vbs文件,里面只写一句话

set scal =CreateObject("System.Collections.ArrayList")

点击运行,就可以了

你可能感兴趣的:(VBScript:数组排序 Sort Array 【1】)