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