Methods
With
PowerShell, it is possible to find the methods of a
Scripting.FileSystemObject
object:
$fso = new-object -com Scripting.FileSystemObject
$fso | get-member -type method | foreach-object {
$null = $_.definition -match '(\S+)\s+(\S+)\s+\(([^)]*)\)'
'{0,-20} {1,-30} {2}' -f $matches[1], $matches[2], $matches[3]
}
string | BuildPath | string , string |
void | CopyFile | string , string , bool |
void | CopyFolder | string , string , bool |
IFolder | CreateFolder | string |
ITextStream | CreateTextFile | string , bool , bool |
void | DeleteFile | string , bool |
void | DeleteFolder | string , bool |
bool | DriveExists | string |
bool | FileExists | string |
bool | FolderExists | string |
string | GetAbsolutePathName | string |
string | GetBaseName | string |
IDrive | GetDrive | string |
string | GetDriveName | string |
string | GetExtensionName | string |
IFile | GetFile | string |
string | GetFileName | string |
string | GetFileVersion | string |
IFolder | GetFolder | string |
string | GetParentFolderName | string |
IFolder | GetSpecialFolder | SpecialFolderConst |
ITextStream | GetStandardStream | StandardStreamTypes , bool |
string | GetTempName | |
void | MoveFile | string , string |
void | MoveFolder | string , string |
ITextStream | OpenTextFile | string , IOMode , bool , Tristate |
Create or delete text file
The following simple example uses fileExists
to check whether a given file exists.
If it exists, it deletes it using deleteFile
.
If the file does not exist, it first creates a text file using createTextFile
which returns a textStream
object. With this object's writeLine
methods, some text lines can be added to the newly created text file.
Finally, the file (text stream) is closed.
option explicit
sub main() ' {
dim pathToFile as string
pathToFile = environ$("TEMP") & "\someFile.txt"
dim fs as new scripting.fileSystemObject
if fs.fileExists(pathToFile) then ' {
msgBox(pathToFile & " exists." & vbcrlf & "Going to delete it.")
fs.deleteFile pathToFile
' }
else ' {
msgBox(pathToFile & " does not exists." & vbcrlf & "Going to create it.")
dim ts as scripting.textStream
set ts = fs.createTextFile(pathToFile)
ts.writeLine("First line" )
ts.writeLine("Second line")
ts.writeLine("Third line" )
ts.close
end if ' }
end sub ' }