Board logo

标题: 本人的VBS学习笔记(待续) [打印本页]

作者: batman    时间: 2009-4-3 18:07     标题: 本人的VBS学习笔记(待续)

这些都是本人这几天学习vbs以来写下的代码,也发出来大家一起讨论和学习了:
  1. '获取当前目录名
  2. msgbox(createobject("wscript.shell").currentdirectory)
复制代码
  1. '写入并读取excel表指定单元格的内容
  2. dim path
  3. set wshshell=createobject("wscript.shell")
  4. path=wshshell.currentdirectory
  5. set objexcel=createobject("excel.application")
  6. set objworks=objexcel.workbooks.open(path&"\test.xls")
  7. set objsheets=objworks.sheets(1)
  8. objsheets.range("d1").value="这是测试内容"
  9. msgbox objsheets.range("d1").value
  10. objexcel.quit
  11. set objsheets=nothing
  12. set objworks=nothing
  13. set objexcel=nothing
  14. set wshshell=nothing
复制代码
  1. '直接显示
  2. wscript.echo "我是一个兵"
复制代码
  1. '延时关闭窗口
  2. createobject("wscript.shell").popup "本窗口将在3秒后关闭",5,"系统消息"
复制代码
  1. '回车换行 vbcrlf
  2. wscript.echo "1"&vbcrlf&"2"
复制代码
  1. '调用cmd命令
  2. set wshshell=createobject("wscript.shell")
  3. wshshell.run "cmd /c dir /a E:\test&pause"
  4. wscript.sleep 3000
  5. set wshshell=nothing
复制代码
  1. '获取文件信息
  2. set objfso = createobject("scripting.filesystemobject")
  3. set wshshell=createobject("wscript.shell")
  4. path=wsh.shell.currentdirectory
  5. set objfile = objfso.Getfile(path&"\test.xls")
  6. wscript.echo "文件创建时间:"&objfile.datecreated
  7. wscript.echo "文件最后访问时间:"&objfile.dateLastaccessed
  8. wscript.echo "文件最后修改时间:"&objfile.dateLastmodified
  9. wscript.echo "所在盘符:"&objfile.drive
  10. wscript.echo "文件名:"&objfile.name
  11. wscript.echo "文件大小:"&objfile.parentfolder
  12. wscript.echo "文件路径:"&objfile.path
  13. wscript.echo "文件短路径 "&objfile.shortpath
  14. wscript.echo "文件类型 "&objfile.type
复制代码
  1. '读取当前目录下b.txt文件4-9行的内容
  2. dim a,str
  3. set objfso=createobject("scripting.filesystemobject")
  4. set wshshell=createobject("wscript.shell")
  5. set objtext=objfso.opentextfile (wshshell.currentdirectory&"\b.txt",1,true)
  6. for a=1 to 3
  7. objtext.skipline
  8. next
  9. for a=1 to 5
  10. str=str&vbcrlf&objtext.readline
  11. next
  12. wscript.echo str
  13. set objtext=nothing
  14. set objfso=nothing
  15. set wshshell=nothing
复制代码
  1. '向文本中写入内容
  2. dim str,path
  3. str="这是一个测试文本"
  4. set objfso=createobject("scripting.filesystemobject")
  5. set wshshell=createobject("wscript.shell")
  6. path=wshshell.currentdirectory
  7. if objfso.fileexists(path&"\2.txt") then
  8. set objtext=objfso.opentextfile(path&"\2.txt",8) '8表示打开文件并在文件末尾进行写操作
  9. objtext.write str
  10. objtext.writeline str
  11. else
  12. set objtext=objfso.opentextfile(path&"\2.txt",2, true)
  13. objtext.writeblanklines 2
  14. objtext.write str
  15. end if
  16. objtext.close
  17. set wshshell=nothing
  18. set objtext=nothing
  19. set objfso=nothing
复制代码

[ 本帖最后由 batman 于 2009-4-4 09:31 编辑 ]
作者: kingerpk    时间: 2009-5-7 15:26

很有用!!!!!!!!谢谢分享!




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2