vbs操作txt文本文件常用方法(函数)

------------------------------------

'creat by 席飞剑(小席老师)

'操作文本文件,操作fso对象(文件对象操作)

------------------------------------

创建文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) '第二个参数表示目标文件存在时是否覆盖
f.Write("写入内容")
f.WriteLine("写入内容并换行")
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
f.Close()
set f = nothing
set fso = nothing

-----------------------------------------------------------

打开并读文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建
f.Skip(3) '将当前位置向后移三个字符
f.SkipLine() '将当前位置移动到下一行的第一个字符,注意:无参数
response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符
response.Write f.ReadLine() '从当前位置向后读取直到遇到换行符(不读取换行符),并将当前位置移动到下一行的第一个字符,注意:无参数
response.Write f.ReadAll() '从当前位置向后读取,直到文件结束,并将当前位置移动到文件的最后
if f.atEndOfLine then
response.Write("一行的结尾!")
end if
if f.atEndOfStream then
response.Write("文件的结尾!")
end if
f.Close()
set f = nothing
set fso = nothing

-----------------------------------------------------------

打开并写文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二个参数 2 表示重写,如果是 8 表示追加
f.Write("写入内容")
f.WriteLine("写入内容并换行")
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
f.Close()
set f = nothing
set fso = nothing

-----------------------------------------------------------

判断文件是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
response.Write("目标文件存在")
else
response.Write("目标文件不存在")
end if
set fso = nothing

-----------------------------------------------------------

移动文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
set fso = nothing

-----------------------------------------------------------

复制文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
set fso = nothing

-----------------------------------------------------------

删除文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing

-----------------------------------------------------------

创建文件夹
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder("C:\test") '目标文件夹的父文件夹必须存在
set fso = nothing

-----------------------------------------------------------

判断文件夹是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
response.Write("目标文件夹存在")
else
response.Write("目标文件夹不存在")
end if
set fso = nothing

-----------------------------------------------------------

删除文件夹
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") '文件夹不必为空
set fso = nothing


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果) 使用vbs 脚本对工作目录下的字符串进行替换并统计被修改的文件数主调的批处理方法将捕捉该vbs脚本运行后的结果并打印在cmd窗口。 示例代码(t.vbs)如下: Set fso=Wscript.CreateObject("Scripting.FileSystemObject") flrName="D:\Workspace\src\" 'dir that you need deal with count=0 'get the count of modified files function Traversal(dir) set flr=fso.getfolder(dir) set fs=flr.files findstr1="[assembly: SecurityTransparent]" 'find string that need to be replaced findstr2=replace(findstr1," ","") 'becasuse there isspace char in findstr,so add this findstr2 replaceStr="[assembly: AllowPartiallyTrustedCallers()]" 'destination stringin replace for each f in fs if lcase(f.name)="assemblyinfo.cs" then set findf=fso.opentextfile(f) do while findf.atendofstream=false d=f.datelastmodified 'get modified datetime of current file alltext=findf.readall if(InStr(alltext,findstr1)> 0 ) then s=replace(alltext,findstr1, replaceStr) count=count+1 findf.close() 'If no findstr2, delete the following elseif block elseif(InStr(alltext,findstr2) > 0) then s=replace(alltext,findstr2, replaceStr) count=count+1 findf.close() else findf.close() exit do end if set r=fso.opentextfile(f, 2, true) r.write s d2=f.datelastmodified if d2>=d then exit do loop end if next set fs=flr.subfolders for each f in fs Traversal(f.path) next end function Traversal(flrName) 'msgbox ("Done! "& count &" files were modified successfully.") wscript.echo "Done! "& count & " files were modified successfully." 'value popup by"wscript.echo" can be received by .bat easily Wscript.quit count 'this variable "count" in order to get a return value by%ErrorLevel% 上面的操作只执行了处理文件的步骤 如果不需要提取运行结果,在 批处理 或主调vbs脚本里直接 运行该vbs文件就行了。 如果在bat 里调用该vbs脚本,并希望打印出提示信息 可以用下面的两种方法来实现: 1,在bat中加入如下代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值