| last article | table of contents | next article |
|---|
FileSystemObject Tutorial by Kefi
I think I covered every thing the FileSystemObject is capable of doing in this.
If not, please mail me.
This tutorial should give you great knowlegde of the FileSystemObject, and make
your vbs/js's alot better. (I hope)
1.Drive:
1.AvailableSpace:
Description:
Determines the amount of free space left on the drive in bytes.
Important:
No
Example:
Set fso = CreateObject("Scripting. FileSystemObject ")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.AvailableSpace
2.DriveLetter:
Description:
Determines the drive letter associated with the drive.
Important:
Yes
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.DriveLetter
3.DriveType:
Description:
Returns the drive type Unknown: 0, Removable: 1, Fixed Drive: 2,
Network: 3, CD-ROM: 4, RAM Disk: 5.
Important:
Yes
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.Type
4.FileSystem:
Description:
Returns the type of filesystem in which the drive is formatted
(FAT16, FAT32, NTFS).
Important:
No
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.FileSystem
5.FreeSpace:
Description:
Returns the amount of free space available on the drive.
Important:
No
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.FreeSpace
6.IsReady:
Description:
Determines if the information on the drive can be currently read
or written. If the drive contains removeable media the value
becomes true when readable media is placed in the drive.
Important:
Yes
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.IsReady
7.Path:
Description:
Returns the Drive Path
Important:
Yes
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.Path
8.RootFolder:
Description:
Determines the root folder of the drive.
Important:
Yes
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.RootFolder
9.ShareName:
Description:
If the drive is shared this returns the share name.
Important:
No
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.ShareName
10.TotalSize:
Description:
Determines the total amount of bytes on the drive.
Important:
No
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.TotalSize
11.VolumeName:
Description:
Determines the name associated with the drive.
Important:
No
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
CDrv="c:\"
set DrvC=fso.GetDrive(CDrv)
msgbox DrvC.VolumeName
2.File:
1.Attributes:
Description:
Returns a value that corresponds to the various
attributes of the file.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.Attributes
2.DateCreated:
Description:
Determines the creation date of the file.
Important:
No
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.DateCreated
3.DateLastAccessed:
Description:
Determines the date the file was last accessed by a process.
Important:
No
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.DateAccessed
4.DateLastModified:
Description:
Determines the date the file was last modified by a process.
Important:
No
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.DateLastModified
5.Name:
Description:
Determines the name associated with the file.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.Name
6.ParentFolder:
Description:
Determines the folder in which the file resides.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.ParentFolder
7.Path:
Description:
Determines the full path of the file.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.Path
10.Size:
Description:
Determines the size of the file in bytes.
Important:
No
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.Size
11.Type:
Description:
Returns the file type.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
MsgBox File.Type
12.Copy:
Description:
Copies the selected file object to another location.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
File.Copy "C:\Kefi.Owns"
13.Delete:
Description:
Deletes the selected file.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.GetFile(f)
File.Delete
14.OpenAsTextStream:
Description:
Opens the selected file.
Important:
Yes
Example:
f="c:\rRlf.Owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set File=fso.OpenAsTextStream(f)
3.Folder:
1.Attributes:
Description:
Returns a value that corresponds to the various
attributes of the folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.Attributes
2.DateCreated:
Description:
Determines the creation date of the folder.
Important:
No
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.DateCreated
3.DateLastAccessed:
Description:
Determines the date the folder was last accessed
by a process.
Important:
No
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.DateLastAccessed
4.DateLastModified:
Description:
Determines the date the folder was last modified
by a process.
Important:
No
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Set Files = Folder.Files
MsgBox Folder.DateLastModified
5.Files:
Description:
Returns the files that are stored in the selected
folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Set Files = Folder.Files
For Each file In Files
MsgBox file.Name
Next
6.IsRootFolder:
Description:
Determines whether the specified folder is the root folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.IsRootFolder
7.Name:
Description:
Determines the name associated with the folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.Name
8.ParentFolder:
Description:
Determines the folder in which the folder resides.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.ParentFolder
9.Path:
Description:
Determines the full path of the folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.Path
10.Size:
Description:
Determines the size of the folder in bytes.
Important:
No
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.Size
11.SubFolders:
Description:
Returns the subfolders contained in the specified folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.SubFolders
12.Copy:
Description:
Copys the selected folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.Copy "C:\rRlf2\"
13.Delete:
Description:
Deletes the selected folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
Folder.delete
14.CreateTextFile:
Description:
Creates a text file in a specified folder.
Important:
Yes
Example:
DirName="c:\rRlf\"
Set fso = CreateObject("Scripting.FileSystemObject")
set Folder=fso.GetFolder(DirName)
FO.CreateTextFile("rRlf.owns",true,false)
4.FileSystem:
1.CopyFolder:
Description:
Copies a folder to a new location.
Important:
Yes
Example:
dir= "c:\rRlf"
newdir= "c:\rRlf"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.copyfolder dir, newdir
2.CreateFolder:
Description:
Creates a new folder in the specified location.
Important:
Yes
Example:
dir= "c:\rRlf"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.createfolder(dir)
3.CreateTextFile:
Description:
Creates a textfile in the specified location.
Important:
Yes
Example:
File= "c:\rRlf.ownz"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.createtextfile(File)
4.DeleteFile:
Description:
Deletes the specified file.
Important:
Yes
Example:
File= "c:\rRlf.ows"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(File)
5.DeleteFolder:
Description:
Deletes the specified folder.
Important:
Yes
Example:
dir= "c:\rRlf"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(dir)
6.FileExists:
Description:
Determines whether or not the specified file exists.
Important:
Yes
Example:
File= "c:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.FileExists(File)
7.FolderExists:
Description:
Determines whether or not the specified directory exists.
Important:
Yes
Example:
dir= "c:\rRlf"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.FolderExists(dir)
8.GetSpecialFolder:
Description:
Determines the specified folder based on the
input. Windows folder: 0, System Folder: 1,
Temporary folder: 2
Important:
Yes
Example:
dir=0
Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetSpecialFolder(dir)
9.GetExtensionName:
Description:
Determines the extension of a file or folder.
Important:
Yes
Example:
File= "c:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetExtensionName(File)
10.GetFile:
Description:
Returns a file object on which the file methods and
properties can be performed.
Important:
Yes
Example:
File= "c:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetFile(File)
12.GetFolder:
Description:
Returns a file object on which the file methods and
properties can be performed.
Important:
Yes
Example:
dir= "c:\rRlf"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetFolder(dir)
13.OpenTextFile:
Description:
Opens the specified file and returns an object on
which the textstream properties and methods can be
performed.
Important:
Yes
Example:
File= "c:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.opentextfile(File)
Notes:
The optional mode variable refers to the way the
file is opened. It can be opened in the following
manners:
1 For Reading Only
2 For Writing
8 For Appending
The optional create boolean variable indicates
whether to create the file if it already does
not exist. The acceptable values are:
True Create the File
False Do Note Create the File
5.TextStream:
1.Close:
Description:
Closes a currently open text file.
Important:
Yes
Example:
File = "C:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set FileWriter = fso.OpenTextFile(File,1,false,0)
FileWriter.Close
2.ReadAll:
Description:
Reads the entire text file.
Important:
Yes
Example:
File = "C:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set FileWriter = fso.OpenTextFile(File,1,false,0)
FileWriter.ReadAll
3.ReadLine:
Description:
Reads one line of a text file.
Important:
Yes
Example:
File = "C:\rRlf.owns"
Set fso = CreateObject("Scripting.FileSystemObject")
set FileWriter = fso.OpenTextFile(File,1,false,0)
FileWriter.ReadLine
4.Write:
Description:
Writes the specified string to a text file.
Important:
Yes
Example:
File = "C:\rRlf.owns"
Text = "the rRlf owns you"
Set fso = CreateObject("Scripting.FileSystemObject")
set FileWriter = fso.OpenTextFile(File,1,false,0)
FileWriter.Write(Text)
5.WriteLine:
Description:
Writes the specified string as a line to a text file.
Important:
Yes
Example:
File = "C:\rRlf.owns"
Text = "the rRlf owns you"
Set fso = CreateObject("Scripting.FileSystemObject")
set FileWriter = fso.OpenTextFile(File,1,false,0)
FileWriter.WriteLine(Text)
FileWriter.Close
Notes:
The Close method must follow the WriteLine method
or the text will not appear in the file.
6.WriteBlankLines:
Description:
Writes the specified number of blank lines to
a text file.
Important:
No
Example:
File = "C:\rRlf.owns"
Text = "the rRlf owns you"
Set fso = CreateObject("Scripting.FileSystemObject")
set FileWriter = fso.OpenTextFile(File,1,false,0)
FileWriter.WriteBlankLines(7)
Hope you learned something from this. It took me about 2 and a half hours to make.
Kefi.[rRlf]
http://vx.netlux.org/~kefi