zoukankan      html  css  js  c++  java
  • VBS基础篇

    VBS基础篇 - 对象(3) - FileSystemObject对象

     

      文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs中对桌面和文件系统进行访问的顶级对象是FileSystemObject(FSO),这个对象特别复杂,是vbs进行文件操作的核心。

           FSO包含的常见对象有:

                 

    对象/集合

    描述

    Drive

    包含储存设备的信息,包括硬盘、光驱、ram盘、网络驱动器

    Drives

    提供一个物理和逻辑驱动器的列表

    File

    检查和处理文件

    Files

    提供包含在文件夹内的所有文件的列表

    Folder

    检查和处理文件夹

    Folders

    提供在 Folder 内的所有文件夹的列表

    TextStream

    对象。用来读写文本文件。

           如何使用FSO

           要用FSO对象模型来编程,使用CreateObject方法来创建FileSystemObject对象,例如:  

    1
    2
    Dim fso
    Set fso = wscript.createobject("scripting.filesystemobject")

    在这个示例中,Scripting 是类型库的名字,而 FileSystemObject 则是想要创建的对象的名字。至此我们获取了fso对象,接下来就可以使用fso对象了。如果要释放的话也很简单,例如:

    1
    Set fso = nothing

           FileSystemObject对象总共一个属性即Drives

           描述:获得所有可用驱动器的集合。

           说明:无论是否本地磁盘、插入媒体、可移动媒体驱动器都显示在 Drives 集合中。

           具体示例代码如下所示:获取本计算机上所有的磁盘的盘符  

    1
    2
    3
    4
    5
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Drivers = fso.Drives
    For Each Driver in Drivers
        Msgbox Driver.DriveLetter '输出计算机上所有的磁盘盘符
    Next 

       方法:(仅常用的方法)

      CreateFile

           描述:创建一个空文件

           语法:object. CreateTextFile(strFile,blnOverWrite)

           参数:strFile为文件名称

                   blnOverWrite为Ture强制覆盖,为False不覆盖

           示例:创建文件C: est.txt

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用CreateTextFile创建文件,不覆盖存在的文件
    Fso.CreateTextFile "C: est.txt",False
    '覆盖存在的文件
    Fso.CreateTextFile "C: est.txt",True

      CreateFolder

           描述:创建一个空的文件夹

           语法:object. CreateFolder(strFolder)

           参数:strFolder为想要创建的文件夹名称

           示例:创建文件夹: c: est

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用MyFolder创建文件夹
    Fso.CreateFolder("c: est")

      DeleteFile

           描述:删除一个文件

           语法:object. DeleteFile (strFile,force)

           参数:strFile为想要删除的文件。组成部分中可用通配符。

                   force如果要删除只读文件,则该值为 True;否则为 False(默认)

           示例:删除文件: c: est.txt   

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用DeleteFile删除指定文件
    Fso.DeleteFile("c: est.txt")
    '强制删除只读的文件
    Fso.DeleteFile "c: est.txt",True

      DeleteFolder

           描述:删除一个文件夹

           语法:object. DeleteFolder(strFolder,force)

           参数:strFolder为想要删除的文件夹名称。组成部分中可用通配符。

                   force如果要删除只读文件夹,则该值为 True;否则为 False(默认)

           示例:删除文件夹: c: est

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用DeleteFile删除指定文件
    Fso.DeleteFolder("c: est")
    '强制删除只读的文件
    Fso.DeleteFolder "c: est",True

      FileExists

           描述:判断指定文件是否存在

           语法:object. FileExists (strFile)

           参数:strFile为指定的文件

           示例:检查文件: c: est.txt是否存在

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用FileExists判断指定文件是否存在
    MsgBox Fso.FileExists("c: est.txt")

      FolderExist

           描述:判断指定文件夹是否存在

           语法:object. FolderExists (strFolder)

           参数:strFolder为指定的文件夹

           示例:检查文件夹: c: est是否存在

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用FolderExists判断指定文件夹是否存在
    MsgBox Fso.FolderExists("c: est")

      CopyFile

           描述:将一个或多个文件从某位置复制到另一位置

           语法:object.CopyFile "source", "destination"[, overwrite]

           参数:source必选项。表示指定文件的路径。组成部分中可用通配符。

    destination必选项。表示目标位置路径

    overwrite可选项。Boolean 值表明是否覆盖现有文件。如果是 True,则覆盖文件;如果是 False,则不覆盖现有文件。默认值是 True

           示例: c: est.txt文件复制到D:下

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用CopyFile复制文件到另一个位置,False不覆盖已存在文件
    Fso.CopyFile "c: est.txt","D:",False
    'True覆盖已存在文件
    Fso.CopyFile "c: est.txt","D:",True

            示例: c:下所有的txt文件复制到D:下

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用*.txt,可以同时将多个文件复制到另一个位置,False不覆盖已存在文件
    Fso.CopyFile "c:*.txt","D:",False
    'True表示覆盖已存在文件
    Fso.CopyFile "c:*.txt","D:",True

      CopyFolder

           描述:将文件夹从某位置复制到另一位置

           语法:object. CopyFolder "source", "destination"[, overwrite]

           参数:source必选项。表示指定文件夹的路径。组成部分中可用通配符。

    destination必选项。表示目标位置的路径

    overwrite可选项。Boolean 值表明是否覆盖现有文件夹。如果是 True,则覆盖文件夹;如果是 False,则不覆盖现有文件夹。默认值是 True

           示例: c: est文件夹复制到D:下    

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用CopyFile复制文件到另一个位置,默认为True覆盖已存在文件
    Fso.CopyFolder "c: est","D:"
    'False不覆盖已存在文件
    Fso.CopyFolder "c: est","D:",False

      MoveFile

    描述:将一个或多个文件从某位置移动到另一位置

    语法:object.MoveFile source, destination

    参数:source必选项。要移动的文件的路径。组成部分中可用通配符。

    destination必选项。指定路径,表示要将文件移动到该目标位置。destination 参数不能包含通配符。

    示例: c: est文件夹移动到D:下

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用MoveFile移动文件到另一个位置
    Fso.MoveFile "c: est.txt","D:"

      MoveFolder

    描述:将一个或多个文件夹从某位置移动到另一位置

    语法:object.MoveFolder source, destination

    参数:source必选项。要移动的文件夹的路径。组成部分中可用通配符。

    destination必选项。指定路径,表示要将文件夹移动到该目标位置。

    示例:

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用MoveFolder移动文件夹到另一个位置
    Fso.MoveFolder "c: est","D:"

     

      GetExtensionName

    描述:获取文件后缀名

    语法:object.MoveFolder source, destination

    参数:source必选项。要移动的文件夹的路径。组成部分中可用通配符。

    destination必选项。指定路径,表示要将文件夹移动到该目标位置。

    示例:获取”c: est.txt”文件后缀名

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    Dim GetExtensionName
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用GetExtensionName获取文件后缀名
    GetExtensionName = fso.GetExtensionName("c: est.txt")
    MsgBox GetExtensionName '输出txt

     

      GetBaseName

    描述:获取文件当前所在文件夹

    语法:object.GetBaseName Path

    参数:Path必选项。文件路径名。

    示例:获取”c: est.txt”文件名称

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    Dim GetBaseName
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用GetBaseName获取文件的文件名称
    GetBaseName = Fso.GetBaseName("c: est est.txt")
    MsgBox GetBaseName '输出test

     

      GetParentFolderName

    描述:将一个或多个文件夹从某位置移动到另一位置

    语法:object.GetParentFolderName Path

    参数:Path必选项。文件路径名。

    示例:获取”c: est.txt”文件所在的文件夹

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    Dim GetParentFolderName
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用GetParentFolderName获取文件当前所在的文件夹
    GetParentFolderName = Fso.GetParentFolderName("c: est est.txt")
    MsgBox GetParentFolderName '输出c: est

     

    VBS基础篇 - 对象(3) - FileSystemObject对象

     

      文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs中对桌面和文件系统进行访问的顶级对象是FileSystemObject(FSO),这个对象特别复杂,是vbs进行文件操作的核心。

           FSO包含的常见对象有:

                 

    对象/集合

    描述

    Drive

    包含储存设备的信息,包括硬盘、光驱、ram盘、网络驱动器

    Drives

    提供一个物理和逻辑驱动器的列表

    File

    检查和处理文件

    Files

    提供包含在文件夹内的所有文件的列表

    Folder

    检查和处理文件夹

    Folders

    提供在 Folder 内的所有文件夹的列表

    TextStream

    对象。用来读写文本文件。

           如何使用FSO

           要用FSO对象模型来编程,使用CreateObject方法来创建FileSystemObject对象,例如:  

    1
    2
    Dim fso
    Set fso = wscript.createobject("scripting.filesystemobject")

    在这个示例中,Scripting 是类型库的名字,而 FileSystemObject 则是想要创建的对象的名字。至此我们获取了fso对象,接下来就可以使用fso对象了。如果要释放的话也很简单,例如:

    1
    Set fso = nothing

           FileSystemObject对象总共一个属性即Drives

           描述:获得所有可用驱动器的集合。

           说明:无论是否本地磁盘、插入媒体、可移动媒体驱动器都显示在 Drives 集合中。

           具体示例代码如下所示:获取本计算机上所有的磁盘的盘符  

    1
    2
    3
    4
    5
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Drivers = fso.Drives
    For Each Driver in Drivers
        Msgbox Driver.DriveLetter '输出计算机上所有的磁盘盘符
    Next 

       方法:(仅常用的方法)

      CreateFile

           描述:创建一个空文件

           语法:object. CreateTextFile(strFile,blnOverWrite)

           参数:strFile为文件名称

                   blnOverWrite为Ture强制覆盖,为False不覆盖

           示例:创建文件C: est.txt

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用CreateTextFile创建文件,不覆盖存在的文件
    Fso.CreateTextFile "C: est.txt",False
    '覆盖存在的文件
    Fso.CreateTextFile "C: est.txt",True

      CreateFolder

           描述:创建一个空的文件夹

           语法:object. CreateFolder(strFolder)

           参数:strFolder为想要创建的文件夹名称

           示例:创建文件夹: c: est

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用MyFolder创建文件夹
    Fso.CreateFolder("c: est")

      DeleteFile

           描述:删除一个文件

           语法:object. DeleteFile (strFile,force)

           参数:strFile为想要删除的文件。组成部分中可用通配符。

                   force如果要删除只读文件,则该值为 True;否则为 False(默认)

           示例:删除文件: c: est.txt   

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用DeleteFile删除指定文件
    Fso.DeleteFile("c: est.txt")
    '强制删除只读的文件
    Fso.DeleteFile "c: est.txt",True

      DeleteFolder

           描述:删除一个文件夹

           语法:object. DeleteFolder(strFolder,force)

           参数:strFolder为想要删除的文件夹名称。组成部分中可用通配符。

                   force如果要删除只读文件夹,则该值为 True;否则为 False(默认)

           示例:删除文件夹: c: est

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用DeleteFile删除指定文件
    Fso.DeleteFolder("c: est")
    '强制删除只读的文件
    Fso.DeleteFolder "c: est",True

      FileExists

           描述:判断指定文件是否存在

           语法:object. FileExists (strFile)

           参数:strFile为指定的文件

           示例:检查文件: c: est.txt是否存在

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用FileExists判断指定文件是否存在
    MsgBox Fso.FileExists("c: est.txt")

      FolderExist

           描述:判断指定文件夹是否存在

           语法:object. FolderExists (strFolder)

           参数:strFolder为指定的文件夹

           示例:检查文件夹: c: est是否存在

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用FolderExists判断指定文件夹是否存在
    MsgBox Fso.FolderExists("c: est")

      CopyFile

           描述:将一个或多个文件从某位置复制到另一位置

           语法:object.CopyFile "source", "destination"[, overwrite]

           参数:source必选项。表示指定文件的路径。组成部分中可用通配符。

    destination必选项。表示目标位置路径

    overwrite可选项。Boolean 值表明是否覆盖现有文件。如果是 True,则覆盖文件;如果是 False,则不覆盖现有文件。默认值是 True

           示例: c: est.txt文件复制到D:下

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用CopyFile复制文件到另一个位置,False不覆盖已存在文件
    Fso.CopyFile "c: est.txt","D:",False
    'True覆盖已存在文件
    Fso.CopyFile "c: est.txt","D:",True

            示例: c:下所有的txt文件复制到D:下

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用*.txt,可以同时将多个文件复制到另一个位置,False不覆盖已存在文件
    Fso.CopyFile "c:*.txt","D:",False
    'True表示覆盖已存在文件
    Fso.CopyFile "c:*.txt","D:",True

      CopyFolder

           描述:将文件夹从某位置复制到另一位置

           语法:object. CopyFolder "source", "destination"[, overwrite]

           参数:source必选项。表示指定文件夹的路径。组成部分中可用通配符。

    destination必选项。表示目标位置的路径

    overwrite可选项。Boolean 值表明是否覆盖现有文件夹。如果是 True,则覆盖文件夹;如果是 False,则不覆盖现有文件夹。默认值是 True

           示例: c: est文件夹复制到D:下    

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用CopyFile复制文件到另一个位置,默认为True覆盖已存在文件
    Fso.CopyFolder "c: est","D:"
    'False不覆盖已存在文件
    Fso.CopyFolder "c: est","D:",False

      MoveFile

    描述:将一个或多个文件从某位置移动到另一位置

    语法:object.MoveFile source, destination

    参数:source必选项。要移动的文件的路径。组成部分中可用通配符。

    destination必选项。指定路径,表示要将文件移动到该目标位置。destination 参数不能包含通配符。

    示例: c: est文件夹移动到D:下

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用MoveFile移动文件到另一个位置
    Fso.MoveFile "c: est.txt","D:"

      MoveFolder

    描述:将一个或多个文件夹从某位置移动到另一位置

    语法:object.MoveFolder source, destination

    参数:source必选项。要移动的文件夹的路径。组成部分中可用通配符。

    destination必选项。指定路径,表示要将文件夹移动到该目标位置。

    示例:

    1
    2
    3
    4
    5
    Dim Fso
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用MoveFolder移动文件夹到另一个位置
    Fso.MoveFolder "c: est","D:"

     

      GetExtensionName

    描述:获取文件后缀名

    语法:object.MoveFolder source, destination

    参数:source必选项。要移动的文件夹的路径。组成部分中可用通配符。

    destination必选项。指定路径,表示要将文件夹移动到该目标位置。

    示例:获取”c: est.txt”文件后缀名

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    Dim GetExtensionName
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用GetExtensionName获取文件后缀名
    GetExtensionName = fso.GetExtensionName("c: est.txt")
    MsgBox GetExtensionName '输出txt

     

      GetBaseName

    描述:获取文件当前所在文件夹

    语法:object.GetBaseName Path

    参数:Path必选项。文件路径名。

    示例:获取”c: est.txt”文件名称

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    Dim GetBaseName
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用GetBaseName获取文件的文件名称
    GetBaseName = Fso.GetBaseName("c: est est.txt")
    MsgBox GetBaseName '输出test

     

      GetParentFolderName

    描述:将一个或多个文件夹从某位置移动到另一位置

    语法:object.GetParentFolderName Path

    参数:Path必选项。文件路径名。

    示例:获取”c: est.txt”文件所在的文件夹

    1
    2
    3
    4
    5
    6
    7
    Dim Fso
    Dim GetParentFolderName
    '创建FileSystemObject对象
    Set Fso = CreateObject("Scripting.FileSystemObject")
    '使用GetParentFolderName获取文件当前所在的文件夹
    GetParentFolderName = Fso.GetParentFolderName("c: est est.txt")
    MsgBox GetParentFolderName '输出c: est

     

  • 相关阅读:
    修改mysql数据库字段内容默认值为当前时间
    报错 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
    python标准库之ConfigParser模块读写配置文件
    使用xshell连接liunx服务器遇到的问题( Connection failed)
    linux学习笔记(四)
    本地项目上传到github
    Java 获取昨天、前天时间
    Kibana 启动报错 Error: Unable to write Kibana UUID file, please check the uuid.server configuration value in kibana.yml
    Mybatis foreach 标签的使用
    RabbitMQ 延时队列
  • 原文地址:https://www.cnblogs.com/jinjiangongzuoshi/p/3834667.html
Copyright © 2011-2022 走看看