Saturday, June 11, 2011

Using the File System Object (FSO)

The following code includes a set of complex and simple functions to serve as examples of the possible uses and applications of Microsoft FSO. The examples can be found in the UsingFSO.vbs file located in the \CodeSamplesPlus folder.
dim oFSO
' Create the file system object
set oFSO = CreateObject ("Scripting.FileSystemObject")
'Option Explicit
' *******************************************************************************************
' Create a new txt file
' Parameters:
' FilePath - location of the file and its name
' *******************************************************************************************
Function CreateFile (FilePath)
' Variable that will hold the new file object
dim NewFile
' Create the new text file
set NewFile = oFSO.CreateTextFile(FilePath, True)
set CreateFile = NewFile
End Function
' *******************************************************************************************
' Check if a specific file exist
' Parameters:
' FilePath - Location of the file and its name
' *******************************************************************************************
Function CheckFileExists (FilePath)
' Check if the file exists
CheckFileExists = oFSO.FileExists(FilePath)
End Function
' *******************************************************************************************
' Write data to file
' Parameters:
' FileRef - Reference to the file
' str - Data to be written to the file
*******************************************************************************************
Function WriteToFile (byref FileRef,str)
' Write str to the text file
FileRef.WriteLine(str)
End Function
' *******************************************************************************************
' Read line from file
' Parameters:
' FileRef - reference to the file
' *******************************************************************************************
Function ReadLineFromFile (byref FileRef)
' Read line from text file
ReadLineFromFile = FileRef.ReadLine
End Function
' *******************************************************************************************
' Closes an open file.
' Parameters:
' FileRef - Reference to the file
' *******************************************************************************************
Function CloseFile (byref FileRef)
FileRef.close
End Function
'******************************************************************************************
' Opens a specified file and returns an object that can be used to
' read from, write to, or append to the file.
' Parameters:
' FilePath - Location of the file and its name
' mode options are:
' ForReading - 1
' ForWriting - 2
' ForAppending - 8
' *******************************************************************************************
Function OpenFile (FilePath,mode)
' Open the txt file and return the File object
set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)
End Function
' *******************************************************************************************
' Copy an open file.
' Parameters:
' 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
End Sub
' *******************************************************************************************
' Delete a file.
' Parameters:
' FilePath - Location of the file to be deleted
' *******************************************************************************************
Sub FileDelete ( FilePath)
' Copy source file to destination file
oFSO.DeleteFile ( FilePath)
End Sub
' *******************************************************************************************
' Compare two text files.
'
' Parameters:
' 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)
dim differentFiles
differentFiles = false
dim f1, f2, f_diff
' Open the files
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)
set f_diff = OpenFile(FilePathDiff,8)
dim rowCountF1, rowCountF2
rowCountF1 = 0
rowCountF2 = 0
dim str
' Count how many lines there are in the first file
While not f1.AtEndOfStream
str = ReadLineFromFile(f1)
rowCountF1= rowCountF1 + 1
Wend
' Count how many lines there are in the second file
While not f2.AtEndOfStream
str = ReadLineFromFile(f2)
rowCountF2= rowCountF2 + 1
Wend
' 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
dim biggerFile, smallerFile
set biggerFile = f1
set smallerFile = f2
If ( rowCountF1 < rowCountF2) Then
set smallerFile = f1
set biggerFile = f2
End If
dim lineNum,str1, str2
lineNum = 1
str = "Line" & vbTab & "File1" & vbTab & vbTab & "File2"
WriteToFile f_diff,str
' Loop on all the lines in the smaller file
While not smallerFile.AtEndOfStream
' read line from both files
str1 = ReadLineFromFile(f1)
str2 = ReadLineFromFile(f2)
' Check if we need to ignore white spaces, if yes, trim the two lines
If Not ignoreWhiteSpace Then
Trim(str1)
Trim(str2)
End If
' If there is a difference between the two lines, write them to the differences file
If not (str1 = str2) Then
differentFiles = true
str = lineNum & vbTab & str1 & vbTab & vbTab & str2
WriteToFile f_diff,str
End If
lineNum = lineNum + 1
Wend
' 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
WriteToFile f_diff,str
lineNum = lineNum + 1
Wend
FileCompare = Not differentFiles
End function
' ************** Example of using these functions **********************
FolderPath = "C:\temp\FSO"
FilePath = "C:\temp\FSO\txt.txt"
FilePath1 = "C:\temp\FSO\txt1.txt"
FilePath2 = "C:\temp\FSO\txt2.txt"
FilePathDiff = "C:\temp\FSO\txt_diff.txt"
If (oFSO.FolderExists(FolderPath)) Then
Reporter.ReportEvent 0, "Folder Exists Step", FolderPath & " folder existed before."
if( CheckFileExists(FilePath) ) Then
FileDelete FilePath
Reporter.ReportEvent 0, "File Exists Step", FilePath & " file was deleted"
End If
if( CheckFileExists(FilePath1) ) Then
FileDelete FilePath1
Reporter.ReportEvent 0, "File Exists Step", FilePath1 & " file was deleted"
End If
if( CheckFileExists(FilePath2) ) Then
FileDelete FilePath2
Reporter.ReportEvent 0, "File Exists Step", FilePath2 & " file was deleted"
End If
if( CheckFileExists(FilePathDiff) ) Then
FileDelete FilePathDiff
Reporter.ReportEvent 0, "File Exists Step", FilePathDiff & " file was deleted"
End If
Else
Set fold = oFSO.CreateFolder(FolderPath)
Reporter.ReportEvent 0, "Folder Exists Step", FolderPath & " folder was created"
End If
set f = CreateFile(FilePath)
if( CheckFileExists(FilePath) ) Then
Reporter.ReportEvent 0, "File Exists Step", FilePath & " file was created"
End If
WriteToFile f,"first line"
WriteToFile f,"second line"
CloseFile f
set f = CreateFile(FilePath2)
CloseFile f
set f = OpenFile(FilePath2,2)
WriteToFile f,"test line"
CloseFile f
FileCopy FilePath, FilePath1
if(FileCompare(FilePath1,FilePath2,FilePathDiff,false)) Then
Reporter.ReportEvent 1, "Files Compare Step", FilePath1 & FilePath2 &" files are identical, but should not be"
Else
Reporter.ReportEvent 0, "Files Compare Step", FilePath1 & " & " & FilePath2 &" have difference."
End if

No comments:

Post a Comment