vbs 技巧6则

2019-06-28 11:39:06 浏览数 (1)

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

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

 Set oExec=WshShell.exec("c:windowssystem32format.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

0 人点赞