使用VBS对FSO对象进行类封装
2014-01-09 17:35
295 查看
'''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Des : Package of FSO Object 'Creator : Eric_1991 'Date : 2014/1/9 '''''''''''''''''''''''''''''''''''''''''''''''''''''''' Class FSOUtil Private FsoObj 'init class Private Sub Class_Initialize() Err.Clear On Error Goto 0 Set FsoObj = CreateObject("scripting.filesystemobject") End Sub 'destory resources Private Sub Class_Terminate() Set FsoObj = Nothing End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des: create the new forder by forder path 'param: forderpath as string 'return true or false ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function CreateForder(forderpath) On Error Resume Next Set oFloder = FsoObj.CreateForder(forderpath) If(Err.Number <> 0) Then CreateForder = False Else CreateForder = True End If End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des:check file is exists 'param: filepath as string 'return true or false '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function FileExists(filepath) FileExists = FsoObj.FileExists(filepath) End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des:check forder is exists 'param: forderpath as string 'return true || false ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function FolderExists(forderpath) FolderExists = FsoObj.FolderExists(forderpath) End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des:Copy Forder 'param: sourceForderPath as string, bool as boolean 'param: targetForderPath as string, 'param: bool as boolean ---- whether override targetForder 'return if error return error msg "Error:the sourceForderPath is not exists" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function CopyForder(sourceForderPath, targetForderPath, bool) On Error Resume Next If(FolderExists(sourceForderPath)) Then FsoObj.CopyFolder sourceForderPath, targetForderPath, bool CopyForder = "True" Else CopyForder = "Error:the sourceForderPath is not exists" End If End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des: delete exists forder 'param: targetForderPath as string 'return if error return error msg "Error:the targetForderPath is not exists" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function DeleteForder(targetForderPath) On Error Resume Next If(FolderExists(targetForderPath)) Then FsoObj.DeleteFolder(targetForderPath) DeleteForder = "True" Else DeleteForder = "Error:the targetForderPath is not exists" End If End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des: get sub forders by parent forder 'param: parentFolder as string 'return if error return Error the parentFolder is not exists", not error is return oFolders Object '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function getSubFoldersByParentFolder(parentFolder) Dim oFolders If(FolderExists(parentFolder)) Then Set oFolders = FsoObj.GetFolder(parentFolder).SubFolders Set getSubFoldersByParentFolder = oFolders Set oFolders = Nothing Else getSubFoldersByParentFolder = "Error the parentFolder is not exists" End If End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des: Create txt File and Write the Msg 'param: filePath as string 'writeMsg : the content msg '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub CreateTextFileAndWrite(filePath,writeMsg) Set txtFile = FsoObj.CreateTextFile(filePath, True) txtFile.WriteLine writeMsg txtFile.Close Set txtFile = Nothing End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des: Read the txt File 'param: targetfilePath as string 'writeMsg : the txt file content 'return : if error return the msg "Error the targetfilePath is not exists" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function ReadFileContent(targetfilePath) Dim ForReading ForReading = 1 If(FileExists(targetfilePath)) Then Set txtFileStream = FsoObj.OpenTextFile(targetfilePath, ForReading) ReadFileContent = txtFileStream.ReadAll Set txtFileStream = Nothing Else ReadFileContent = "Error the targetfilePath is not exists" End If End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'des: Append write the Msg to the text file 'param: targetfilePath as string 'param: WriteMsg as string '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub WriteFileForAppend(targetfilePath, WriteMsg) Dim ForAppending ForAppending = 8 Set txtFileStream = FsoObj.OpenTextFile(targetfilePath, ForAppending, True) txtFileStream.WriteLine WriteMsg Set txtFileStream = Nothing End Sub End Class 'use eg: Set abc = New FSOUtil WSH.Echo abc.CopyForder("D:\VBS Libary\EOM","C:\A", True) WSH.Echo abc.DeleteForder("C:\A") Set objFolder = abc.getSubFoldersByParentFolder("D:\VBS Libary\") For Each folder In objFolder WSH.Echo folder.Name Next WSH.Echo abc.ReadFileContent("D:\VBS Libary\parseXML.vbs") abc.WriteFileForAppend "D:\1.txt", "LYJ"
相关文章推荐
- 使用dll进行对象的封装(1)
- 使用cjson进行对象的嵌套封装
- 面向对象设计三大原则(封装变化点,对接口进行编程,多使用组合而不是继承)
- Spring中封装将对象使用注解方式进行封装
- 用一个实例学习VBS中使用fso对象的基本方法
- 使用Hibernate进行对象的关系映射(2)
- 递归算法在javascript中使用的小技巧 (javascript的对象封装方法介绍)
- 使用uiautomator的python封装进行android的UI测试
- javaseday31补充(反射 三种获取字节码对象 获取空参有参对象 进行构造函数和无参有参函数使用)
- 使用for...in进行对象循环
- 关于在struts2中interceptor 中使用ActionInvocation 对象获得HttpServletRequest中封装的参数,以Map的形式
- python使用json格式进行数据封装
- 五星-原型模式常使用于以下场景--而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝
- JS中使用var fso = new ActiveXObject("LoadingOilICCard.ICCardActiveX");报automation服务器不能创建对象
- 面向对象设计三大原则(封装变化点,对接口进行编程,多使用组合而不是继承)
- Java之使用getXxx()或setXxx对类进行封装text1
- 在mfc使用ADO对象进行数据库的交互
- ORM对象关系映射之使用GreenDAO进行CRUD操作
- 对象文件[置顶] 如何在 ASM 存储中使用 Data Pump (expdp impdp)进行数据导出导入操作
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互