vbs 技巧6则

1 vbs 判断shell 程序运行结束的代码

 

比如要通过vbs 调用format格式化d: 盘,可以使用如下代码

 

 Set oExec=WshShell.exec("c:\windows\system32\format.com d: /fs:ntfs /q /y")


Do While oExec.Status = 0
     WScript.Sleep 100
Loop
 
2 vbs得到程序运行路径
 
Set   WshShell=WScript.CreateObject( "WScript.Shell")    '得到程序所在的当前路径


Path_init_file=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))& "nicinfo.ini"   '设置配置文件为当前路径下的nicinfo.ini
 
3 判断文件是否存在
 
Set objFSO = Createobject("Scripting.FileSystemObject")
If  objFSO.Fileexists(path_init_file) Then 
'msgbox("fil now")
else
'msgbox("fil now ---")
    exit sub
end if
 
4 读取文件
 
Set fso=CreateObject("Scripting.FileSystemObject")
Set file=fso.OpenTextFile(Path_init_file,ForReading)

While (Not file.AtEndOfLine)
  
  msg=file.ReadLine
  msgbox(msg)
Wend
file.Close
Set file=Nothing
Set fso=Nothing
 
5 判断有没有d:盘,如果有,调用第一条,格式化d:
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objDrive in colDrives
    if (objDrive.DriveLetter="D") then 
    'msgbox("have d exit")
    '有d盘直接退出,什么也不做
    exit sub
    else
    diskdnotexist="1"
    end if
Next
 
6 写文件,先判断文件是否存在,不存在,创建然后写。
 
Set   WshShell=WScript.CreateObject( "WScript.Shell")    '得到程序所在的当前路径
Path_log_file=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))& "nicinfo.log"    '设置日子文件为当前路径下的nicinfo.log

'判断日志文件是否存在,如果不存在,创建
Set logfso=CreateObject("Scripting.FileSystemObject")
If  logfso.Fileexists(Path_log_file) Then 
'msgbox("file exist")
else
  set ts=logfso.CreateTextFile(Path_log_file, True)
  ts.close
end if

'写日志

Set logfile=logfso.OpenTextFile(Path_log_file,8)
rtime=cstr(now())
logfile.write(logmsg+rtime + vbCrLf)
logfile.write("写一行" + vbCrLf)
logfile.Close
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值