标题: [问题求助] [已解决]VBS脚本如何实现字符对齐输出? [打印本页]
作者: 秋风·飞扬 时间: 2012-6-7 15:48 标题: [已解决]VBS脚本如何实现字符对齐输出?
本帖最后由 秋风·飞扬 于 2012-6-8 12:10 编辑
- strComputer= "."
- Set system_process = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set process_get= system_process.instancesOf("win32_process")
-
- For Each process_infomation in process_get
-
- length process_infomation.name,22
- process_name_space=str_space
-
- length process_infomation.processid,10
- process_id_space=str_space
-
- i=i+1
-
- If i=1 Then
-
- str= str & process_infomation.csname & " " & process_infomation.name & process_name_space & process_infomation.processid & process_id_space
-
- Else
- str= str & process_infomation.csname & " " & process_infomation.name & process_name_space & process_infomation.processid & vbCr
- i=0
-
- End If
-
- process_name_space=""
- process_id_space=""
-
- Next
- Set system_process= Nothing
- Set system_process_infomation= Nothing
-
- WScript.Echo str
-
-
- Function length(a,b)
-
- process_space=""
- c = Len (a)
- process_space = b- c
- For j=0 To process_space
- str_space = str_space & " "
- Next
- End Function
复制代码
问题:只有一个进程不能对齐,就是PID为0 的system idle process这个进程,不明真相(在调试的时候看见函数里面正常计算,但是在跳出函数之后赋值的时候就不行了)。
作者: powerbat 时间: 2012-6-7 16:06
编程风格太难看了,没耐心看完。
作者: 秋风·飞扬 时间: 2012-6-7 20:36
回复 2# powerbat
这次呢
作者: powerbat 时间: 2012-6-7 21:09
也发一个- strComputer = "."
- Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colPS = wmi.InstancesOf("Win32_Process")
- For Each ps in colPS
- i = (i+1) mod 2
- If i Then
- str= str & FMT(ps.CSName, ps.Name, ps.ProcessId)
- Else
- str= str & " | " & FMT(ps.CSName, ps.Name, ps.ProcessId) & vbCrLf
- End If
- Next
- WScript.Echo str
-
- Function FMT(s1, s2, s3)
- CONST MAX_LEN = 38
- CONST PID_LEN = 5
- Dim s
- s = s1 & " " & s2
- If Len(s) > (MAX_LEN - PID_LEN - 1) then
- 'name与pid之间至少要一个空格,所以再减1
- s = Left(s, MAX_LEN - PID_LEN - 1)
- End If
- s = s & String(MAX_LEN - PID_LEN - Len(s), " ") & s3
- s = s & String(MAX_LEN - Len(s), " ") '后面填充空格
- FMT = s
- End Function
复制代码
作者: 秋风·飞扬 时间: 2012-6-8 11:58
我的第一个程序 第一个进程没有对齐,是不是piD是0 的缘故?
作者: 秋风·飞扬 时间: 2012-6-8 12:10
我自问自答吧:开始要对process_space初始化,process_str_space=""
谢那个大笑提供的算法
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |