使用VBS对FSO对象进行类封装

''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'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"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值