search
Twitter Rss Feeds
MicrosoftArticlesForumsGroups
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml/Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsGroups
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsGroups
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsGroups
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsGroups
Microsoft Excel
Microsoft Word
Microsoft Powerpoint
Publisher
Money

Operating SystemsArticlesForumsGroups
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsGroups
Share Point
BizTalk
Site Server
Exhange Server
IIS
Transaction Server

Graphic DesignArticlesForumsGroups
Macromedia Flash
Adobe PhotoShop
Microsoft Expression

OtherArticlesForumsGroups
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Reviews
Search Engines
Resumes

 
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.


Pete's Blog   |    Pete's Resume   |    Robbe's Blog   |    Robbe's Resume   |    Archive #2   |    Archive #3   |    Dotnetslackers   |    XmlPitStop   |    Advertise   |   Contact Us   |   Privacy   |   Copyright (c) 2000 - 2009 eggheadcafe.com  All rights reserved.