Vbs高手请进…(求一段很简单的vbs程序)

2024-12-17 07:21:18
推荐回答(2个)
回答1:

' test.VBS

set fs = CreateObject("Scripting.FileSystemObject")

filePath = "C:\ABC.txt"

if fs.FileExists(filePath) then
MsgBox "file existed"
WScript.Quit
end if

' 3
CreateFolders(filePath)
set ts = fs.CreateTextFile(filePath, true)

' 4
firstName = ""
lastName = ""
lastNameFlag = false

for x=0 to WScript.Arguments.Length - 1
if WScript.Arguments(x) = "/FirstName" then
if( x+1 firstName = WScript.Arguments(x+1)
end if
elseif WScript.Arguments(x) = "/LastName" then
lastNameFlag = true
elseif lastNameFlag then
lastName = WScript.Arguments(x)
end if
Next

do while firstName = ""
firstName = InputBox("Please Enter first name")
loop
do while lastName = ""
lastName = InputBox("Please Enter last name")
loop

ts.WriteLine("First Name:" & firstName)
ts.WriteLine("Last Name:" & lastName)

' 5
Set objProc = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'")

ts.Write "CPU Type:" & objProc.Name & " "

select case objProc.Architecture
case 0: ts.WriteLine("x86")
case 6: ts.WriteLine("IPF")
case 9: ts.WriteLine("x64")
case else: ts.WriteLine("Unknown")
end select

' 6
set wshnet = CreateObject("WScript.Network")
ts.WriteLine("UserDomain:" & wshnet.UserDomain)
ts.WriteLine("ComputerName:" & wshnet.ComputerName)
ts.WriteLine("UserName:" & wshnet.UserName)

ts.Close

sub CreateFolders(path)
start = 1 ' scan path beginning at pos 1

' search for "\"
pos = Instr(start, path, "\")
' loop until no more "\"
do until pos=0
' extract subpath to current "\"
folderpath = left(path, pos-1)

' does this folder already exist?
if not fs.FolderExists(folderpath) then
' create folder:
fs.CreateFolder folderpath
end if

' move to next "\" and scan for more:
start = pos+1
pos = Instr(start, path, "\")
loop
end sub

根据你列出的要求解释一下:
1、2点就不用讲了吧,代码比较简单,之后的每点我在代码中都有标出号码
3、先调用一个sub——CreateFolders创建文件夹,该sub的定义在代码最后,其创建的过程就是根据“\”分隔,从上至下一个一个创建文件夹。
然后在创建文件
4、我对4的理解就是/FirstName后的第一个名字就是first name,/LastName后最后一个名字就是last name,例如下面的命令行(假设该vbs存放路径是C:\test.vbs):
C:\test.vbs /FirstName abc ABC /LastName def DEF
结果abc就应该作为first name,大写的DEF就应该作为last name
如果命令行中没找到这两个name,就通过输入框输入,若输入为空则继续输入:
do while firstName = ""
firstName = InputBox("Please Enter first name")
loop
5、可以参考http://msdn.microsoft.com/en-us/library/aa394373.aspx上面的解释及例子
6、6是我从书上看到的

大概就这样,有些东西我也没搞清楚。

回答2:

没分