您的位置:首页 > 编程语言 > VB

使用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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐