zoukankan      html  css  js  c++  java
  • vbs操作txt文本文件常用方法(函数)

    创建文件
    
    dim fso, f
    set fso = server.CreateObject("Scripting.FileSystemObject")
    set f = fso.CreateTextFile("C:	est.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:	est.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:	est.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:	est.txt") then
        response.Write("目标文件存在")
    else
        response.Write("目标文件不存在")
    end if
    set fso = nothing
    
    -----------------------------------------------------------
    
    移动文件
    
    dim fso
    set fso = server.CreateObject("Scripting.FileSystemObject")
    call fso.MoveFile("C:	est.txt", "D:	est111.txt") '两个参数的文件名部分可以不同
    set fso = nothing
    
    -----------------------------------------------------------
    
    复制文件
    
    dim fso
    set fso = server.CreateObject("Scripting.FileSystemObject")
    call fso.CopyFile("C:	est.txt", "D:	est111.txt") '两个参数的文件名部分可以不同
    set fso = nothing
    
    -----------------------------------------------------------
    
    删除文件
    
    dim fso
    set fso = server.CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile("C:	est.txt")
    set fso = nothing
    
    -----------------------------------------------------------
    
    创建文件夹
    
    dim fso
    set fso = server.CreateObject("Scripting.FileSystemObject")
    fso.CreateFolder("C:	est") '目标文件夹的父文件夹必须存在
    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:	est") '文件夹不必为空
    set fso = nothing

    个人微信订阅号:专注测试开发、自动化测试

  • 相关阅读:
    【小工具】根据定义的白名单字段进行Bean的拷贝
    【Java】Java8的Lambda入门记录
    【Java】浅谈Java IO
    【工具】我的Git学习日志
    【Java】浅谈HashMap
    【Java】Java Queue的简介
    【ZooKeeper】ZooKeeper入门流水记
    【MQTT】Mosquitto的安装与使用流水记
    【数据结构】简单的数据结构图解
    【Java多线程】JDK1.5并发包API杂谈
  • 原文地址:https://www.cnblogs.com/wakey/p/5798185.html
Copyright © 2011-2022 走看看