Scripting.FileSystemObject Examples

Here's how to use the Scripting.FileSystemObject in ASP/VBscript. The FileSystemObject needs regular paths and filenames. It does not recognize relative or root addressing in the same way files are referenced in your web pages (C:\TEMP).

Sample Code
  
<%

Function GetFSO()
   ' Use CreateObject instead  for non web VBScript apps
   GetFSO = Server.CreateObject("Scripting.FileSystemObject")
End Function

Sub WriteFile(sFilePathAndName,sFileContents)  

  Const ForWriting =2

  Set oFS = GetFSO()
  Set oFSFile = oFS.OpenTextFile(sFilePathAndName,ForWriting,True)

  oFSFile.Write(sFileContents)
  oFSFile.Close

  Set oFSFile = Nothing
  Set oFS = Nothing

End Sub

Function ReadFile(sFilePathAndName)

   dim sFileContents

  Set oFS = GetFSO()

   If oFS.FileExists(sFilePathAndName) = True Then
      
       Set oTextStream = oFS.OpenTextFile(sFilePathAndName,1)
      
      sFileContents = oTextStream.ReadAll
    
      oTextStream.Close

      Set oTextStream = nothing
  
   End if
  
   Set oFS = nothing

   ReadFile = sFileContents

End Function

Sub ReadFileLineByLine(sFilePathAndName)

   Const ForReading = 1
   Const ForWriting = 2
   Const ForAppending = 8
   Const TristateUseDefault = -2
   Const TristateTrue = -1
   Const TristateFalse = 0

   Dim oFS
   Dim oFile
   Dim oStream

     Set oFS = GetFSO()

   Set oFile = oFS.GetFile(sFilePathAndName)
  
   Set oStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)

   Do While Not oStream.AtEndOfStream
    
      sRecord=oStream.ReadLine

      Response.Write sRecord

   Loop

   oStream.Close

  End Sub


Sub RemoveFolder(sPath,fRemoveSelf)

  Dim oFS  
  Dim oFSFolder  
  
    Set oFS = GetFSO()

  If oFS.FolderExists(sPath)  <> True Then
    Set oFS = Nothing
    Exit Sub
  End If
  
  Set oFSFolder = oFS.GetFolder(sPath)
  
  RemoveSubFolders oFSFolder
  
  If fRemoveSelf = True Then

     If oFS.FolderExists(sPath) = True Then
        oFSFolder.Delete True
     Else
        Set oFSFolder = Nothing
        Set oFS = Nothing
        Exit Sub
     End If

  End If
  
   Set oFSFolder = Nothing
   Set oFS = Nothing

End Sub


Sub RemoveSubFolders(oFSFolder)

   Dim oFSFile
   Dim oFSSubFolder  
  
   For Each oFSFile In oFSFolder.Files
         oFSFile.Delete True
   Next

   For Each oFSSubFolder In oFSFolder.SubFolders
         RemoveSubFolders oFSSubFolder
         oFSSubFolder.Delete True
   Next
    
   Set oFSFile = Nothing
  
End Sub


Sub RemoveFile(sFilePathAndName)

    Set oFS = GetFSO()
  
  If oFS.FileExists(sFilePathAndName) = True Then
     oFS.DeleteFile sFilePathAndName, True
  end if

  Set oFS = Nothing
  
End Sub

%>

By Robbe Morris   Popularity  (23083 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP