The following code includes a set of complex and simple functions to serve as examples of the possible uses and applications of Microsoft FSO.
' Create the file system object
set oFSO = CreateObject ("Scripting.FileSystemObject")
' *******************************************************************************************
' FilePath - location of the file and its name
' *******************************************************************************************
Function CreateFile (FilePath)
' Variable that will hold the new file object
set NewFile = oFSO.CreateTextFile(FilePath, True)
' *******************************************************************************************
' Check if a specific file exist
' FilePath - Location of the file and its name
' *******************************************************************************************
Function CheckFileExists (FilePath)
CheckFileExists = oFSO.FileExists(FilePath)
' *******************************************************************************************
' FileRef - Reference to the file
' str - Data to be written to the file
*******************************************************************************************
Function WriteToFile (byref FileRef,str)
' *******************************************************************************************
' FileRef - reference to the file
' *******************************************************************************************
Function ReadLineFromFile (byref FileRef)
ReadLineFromFile = FileRef.ReadLine
' *******************************************************************************************
' FileRef - Reference to the file
' *******************************************************************************************
Function CloseFile (byref FileRef)
'******************************************************************************************
' Opens a specified file and returns an object that can be used to
' read from, write to, or append to the file.
' FilePath - Location of the file and its name
' *******************************************************************************************
Function OpenFile (FilePath,mode)
' Open the txt file and return the File object
set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)
' *******************************************************************************************
' FilePathSource - Location of the source file and its name
' FilePathDest - Location of the destination file and its name
' *******************************************************************************************
Sub FileCopy ( FilePathSource,FilePathDest)
' copy source file to destination file
oFSO.CopyFile FilePathSource, FilePathDest
' *******************************************************************************************
' FilePath - Location of the file to be deleted
' *******************************************************************************************
' Copy source file to destination file
' *******************************************************************************************
' FilePath1 - Location of the first file to be compared
' FilePath2 - Location of the second file to be compared
' FilePathDiff - Location of the differences file
' ignoreWhiteSpace - Controls whether or ignore differences in white space characters
' true - Ignore differences in white space
' false - Do not ignore difference in white space
' Return Value: true if files are identical, false otherwise'
' *******************************************************************************************
Function FileCompare (byref FilePath1, byref FilePath2, byref FilePathDiff, ignoreWhiteSpace)
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)
set f_diff = OpenFile(FilePathDiff,8)
' Count how many lines there are in the first file
' Count how many lines there are in the second file
' Re-open the files to go back to the first line in the files
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)
' compare the number of lines in the two files.
' assign biggerFile - The file that contain more lines
' assign smallerFile - The file that contain fewer lines
If ( rowCountF1 < rowCountF2) Then
str = "Line" & vbTab & "File1" & vbTab & vbTab & "File2"
' Loop on all the lines in the smaller file
While not smallerFile.AtEndOfStream
' Check if we need to ignore white spaces, if yes, trim the two lines
' If there is a difference between the two lines, write them to the differences file
str = lineNum & vbTab & str1 & vbTab & vbTab & str2
' Loop through the bigger lines, to write its line to the different file
While not biggerFile.AtEndOfStream
str1 = ReadLineFromFile(biggerFile)
str = lineNum & vbTab & "" & vbTab & vbTab & str2
FileCompare = Not differentFiles
' ************** Example of using these functions **********************
FilePath1 = "D:\temp\FSO\txt1.txt"
FilePath2 = "D:\temp\FSO\txt2.txt"
FilePathDiff = "D:\temp\FSO\txt_diff.txt"
d = FileCompare(FilePath1,FilePath2,FilePathDiff,false)
FilePath = "D:\temp\FSO\txt.txt"
set fold = FolderCreate ( "D:\temp\FSO
' = WriteToFile(f,"test line")
Fexist= CheckFileExists(FilePath)
d = WriteToFile(f,"first line")
d = WriteToFile(f,"second line")
No comments:
Post a Comment