Scripting.FileSystemObject Examples

By Robbe D. Morris

Printer Friendly Version


Robbe & Melisa Morris
Here are a few quick examples of how to use the COM object Scripting.FileSystemObject in ASP/VBscript.  This is not an all encompassing example.  It is just a few of the more common tasks.  Keep in mind, the FSO 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).
Need to know how to duplicate this functionality in .NET?  .NET System.IO FAQ Compared To Scripting.FileSystemObject.


Sample Code
  
 <% 

Sub WriteFile(sFilePathAndName,sFileContents)   

  Const ForWriting =2 

  Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
  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 = Server.CreateObject("Scripting.FileSystemObject") 

   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 = Server.CreateObject("Scripting.FileSystemObject") 

   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 = Server.CreateObject("Scripting.FileSystemObject") 

  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 = Server.CreateObject("Scripting.FileSystemObject") 
   
  If oFS.FileExists(sFilePathAndName) = True Then 
     oFS.DeleteFile sFilePathAndName, True 
  end if 

  Set oFS = Nothing 
   
End Sub 

%>

Robbe has been a Microsoft MVP in C# since 2004.  He is also the co-founder of EggHeadCafe which provides .NET articles, book reviews, software reviews, and software download and purchase advice.