Night Fire: Recently, the school taught about the FSO component in ASP, but I didn’t quite understand it… I fell asleep during class~. In the past, I had read some books about ASP, so even though I dozed off, I was still able to finish and submit the final assignment. The teaching pace at school is really not commendable, so I need to find more materials on FSO to enrich my knowledge. This can also serve as notes for future reference.
FSO stands for FileSystemObject or Scripting.FileSystemObject, which is a built-in component of IIS used to operate disks, folders, or text files. There are a lot of objects, methods, and properties for FSO. Here, I will list the commonly used ones with examples. Note: The "FileSystemObject User's Guide" in the "VBScript Language Reference" or "JScript Language Reference," and the "Scripting Runtime Library Reference" are complete references provided by Microsoft for FileSystemObject.
FSO cannot handle binary files. To handle binary files, use ADODB.Stream.
### Creating a File
```vbscript
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) 'The second parameter indicates whether to overwrite if the target file exists.
f.Write("Content to write")
f.WriteLine("Write content and add a newline")
f.WriteBlankLines(3) 'Write three blank lines (equivalent to pressing Enter three times in a text editor)
f.Close()
set f = nothing
set fso = nothing
```
### Opening and Reading a File
```vbscript
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) 'The second parameter 1 means open for reading only, the third parameter indicates whether to create the file if it does not exist.
f.Skip(3) 'Move the current position forward by three characters.
f.SkipLine() 'Move the current position to the first character of the next line. Note: No parameters.
response.Write f.Read(3) 'Read three characters from the current position and move the current position forward by three characters.
response.Write f.ReadLine() 'Read from the current position until encountering a newline character (excluding the newline), and move the current position to the first character of the next line. Note: No parameters.
response.Write f.ReadAll() 'Read from the current position until the end of the file, and move the current position to the end of the file.
if f.atEndOfLine then
response.Write("End of a line!")
end if
if f.atEndOfStream then
response.Write("End of file!")
end if
f.Close()
set f = nothing
set fso = nothing
```
### Opening and Writing to a File
```vbscript
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) 'The second parameter 2 means overwrite, if it’s 8, it means append.
f.Write("Write content")
f.WriteLine("Write content and add a newline")
f.WriteBlankLines(3) 'Write three blank lines (equivalent to pressing Enter three times in a text editor)
f.Close()
set f = nothing
set fso = nothing
```
### Checking if a File Exists
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
response.Write("Target file exists")
else
response.Write("Target file does not exist")
end if
set fso = nothing
```
### Moving a File
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") 'The filename parts of the two parameters can be different.
set fso = nothing
```
### Copying a File
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") 'The filename parts of the two parameters can be different.
set fso = nothing
```
### Deleting a File
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing
```
### Creating a Folder
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder("C:\test") 'The parent folder of the target folder must exist.
set fso = nothing
```
### Checking if a Folder Exists
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
response.Write("Target folder exists")
else
response.Write("Target folder does not exist")
end if
set fso = nothing
```
### Deleting a Folder
```vbscript
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") 'The folder does not have to be empty.
set fso = nothing
```