FSO Functions

dim oFSO
' creating the file system object
set oFSO = CreateObject ("Scripting.FileSystemObject") 
1. Create a new txt file
Function CreateFile (FilePath)
    ' varibale that will hold the new file object
    dim NewFile
    ' create the new text ile
    set NewFile = oFSO.CreateTextFile(FilePath, True)
    set CreateFile = NewFile
End Function
2. Check if a specific file exist
Function CheckFileExists (FilePath)
    ' Check if file exist
    CheckFileExists = oFSO.FileExists(FilePath)
End Function
3. Write data to file
Public Function WriteToFile (byref FileRef,str)
    ' write str to the text file
    FileRef.WriteLine(str)
End Function
4. Read line from file
Public Function ReadLineFromFile (byref FileRef)
    ' read line from text file
    ReadLineFromFile = FileRef.ReadLine
End Function
5. Closes an open file
Public Function CloseFile (byref FileRef)
    FileRef.close
End Function
6. Opens a specified file and returns an object that can be used to read from, write to, or append to the file.
' mode options are:
' ForReading - 1
' ForWriting - 2
' ForAppending - 8

Public Function OpenFile (FilePath,mode)
    ' open the txt file and retunr the File object
    set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)
End Function
7. Closes an open file
Public Function FileCopy ( FilePathSource,FilePathDest)
    ' copy source file to destination file
    oFSO.CopyFile FilePathSource, FilePathDest
End Function
8. Delete a file
Public Function FileDelete ( FilePath)
    ' copy source file to destination file
    oFSO.DeleteFile ( FilePath)
End Function
9. Compare two text files
'ARG(s):
  FilePath1 - location of the first file to be compared
  ' FilePath2 - location of the second file to be compared
  ' FilePathDiff - location of the diffrences file
  ' ignoreWhiteSpace - controls whether or not to ignore differences in whitespace characters
  ' true - ignore differences in whitespace
  ' false - do not ignore difference in whitespace
  ' Return Value: true if files are identical, false otherwise'
  ' returned values
  ' -1 - requested field index that doesn't exists in the recordset

Public 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 first file
    While not f1.AtEndOfStream
        str = ReadLineFromFile(f1)
        rowCountF1= rowCountF1 + 1
    Wend
    ' count how many lines there are in 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 less 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 samller 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 diffrence between the two lines, write them to the diffrences 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 on the bigger lines, to write its line two the diffrences 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
10. Example of using above 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\new")
set f = OpenFile(FilePath,8)
' = WriteToFile(f,"test line")
d = CloseFile(f)
set f = CreateFile(FilePath)
Fexist= CheckFileExists(FilePath)
d = WriteToFile(f,"first line")
d = WriteToFile(f,"second line")
d = CloseFile(f)
FileCopy "D:\temp\FSO\txt.txt","D:\temp\FSO\txt1.txt"
FileDelete "D:\temp\FSO\txt1.txt"
11. Create Folder if Not Exists.
Public Function ReportFolderStatus(fldr)
    Dim fso, msg
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not (fso.FolderExists(fldr)) Then
        Set f = fso.CreateFolder(fldr)
    End If
End Function
12. Count Number of Lines in txt file
Public Function NumberOfLines(FileName)
    i = 0
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)
    Do Until objTextFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objTextFile.ReadLine
    i = i +1
    Loop
    NumberOfLines = UBound(arrFileLines)
    objTextFile.Close
End Function
13. Count Number of files in any Folder
iFileCount = 0
Set objFileSysOb = CreateObject("Scripting.FileSystemObject")
Set colFolderName = objFileSysOb.GetFolder("C:\Automation") ' Folder Path
Set vFiles =colFolderName.Files
For each vFileItem in vFiles
   print vFileItem
   iFileCount = iFileCount + 1
Next
print iFileCount