FileSystemObject - Practical Examples

Example 1: Check File Existence

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If FSO.FileExists("/data/users.txt") Then
    Response.Write "File found"
End If

Example 2: Get File Information

Set fileObj = FSO.GetFile("/test_basics.asp")
Response.Write "Name: " & fileObj.Name & vbCrLf
Response.Write "Size: " & fileObj.Size & " bytes" & vbCrLf
Result:

Example 3: Path Manipulation

basePath = "/documents"
fileName = "report.txt"
fullPath = FSO.BuildPath(basePath, fileName)
Result: /documents/report.txt

Example 4: Extract Path Components

filePath = "/www/data/important.doc"
Response.Write "Base Name: " & FSO.GetBaseName(filePath)
Response.Write "Extension: " & FSO.GetExtensionName(filePath)
Response.Write "Parent: " & FSO.GetParentFolderName(filePath)
Result:
Base Name: important
Extension: doc
Parent: /www/data

Example 5: Folder Operations

If FSO.FolderExists("/") Then
    Set folderObj = FSO.GetFolder("/") 
    Response.Write "Folder path: " & folderObj.Path
End If
Result:

Example 6: Generate Temporary Filename

tempName = FSO.GetTempName()
Response.Write "Temp file: " & tempName
Result: rad5B1879DA.tmp

Example 7: Get Absolute Path

relativePath = "/data/file.txt"
absolutePath = FSO.GetAbsolutePathName(relativePath)
Response.Write absolutePath
Result:

Common Patterns

Pattern 1: Safe File Reading

If FSO.FileExists("/myfile.txt") Then
    Set textFile = FSO.OpenTextFile("/myfile.txt", 1)
    content = textFile.ReadAll()
    textFile.Close()
    Response.Write content
End If

Pattern 2: File Copy with Overwrite Check

sourcePath = "/source.txt"
destPath = "/backup.txt"
If FSO.FileExists(sourcePath) Then
    If Not FSO.FileExists(destPath) Then
        FSO.CopyFile sourcePath, destPath, False
        Response.Write "File copied successfully"
    Else
        Response.Write "Destination file already exists"
    End If
End If

Pattern 3: Create and Write to File

Set newFile = FSO.CreateTextFile("/output.txt", True)
newFile.WriteLine "Line 1: Hello World"
newFile.WriteLine "Line 2: Welcome to Go-ASP"
newFile.Close()
Response.Write "File created successfully"

Supported CreateObject Aliases

' Classic ASP syntax (recommended)
Set FSO = Server.CreateObject("Scripting.FileSystemObject")

' Direct G3Files library (faster)
Set FSO = Server.CreateObject("G3FILES")

All Available Methods

Summary

The FileSystemObject provides complete file and folder manipulation capabilities

All paths are relative to the web application root for security

Use Server.MapPath() to convert virtual paths if needed