Open Directory Site ASP Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> ASP >>ASP File System Object Object

ASP File System Object Object

 

The FileSystemObject Object

The FileSystemObject object is used to access the file system on the server. This Object can manipulate files, folders, and directory paths, and it can get file system information.

Methods

Method Description

BuildPath Appends a name to an existing path

CopyFile Copies a file

CopyFolder Copies a folder

CreateFolder Creates a folder

CreateTextFile Creates a text file

DeleteFile Deletes a file

DeleteFolder Deletes a folder

DriveExists Checks if a drive exists

FileExists Checks if a file exists

FolderExists Checks if a folder exists

GetAbsolutePathName Returns the complete path

GetBaseName Returns the base name of the file or folder

GetDrive Returns a Drive Object corresponding to the drive in a specified path

GetDriveName Returns the name of the drive

GetExtensionName Returns the file extension

GetFile Returns a File Object corresponding to the file in a path

GetFileName Returns the last file name or folder that is not part of the drive

specification

GetFolder Returns a Folder Object corresponding to the folder in a path

GetParentFolderName Returns the name of the parent folder

GetSpecialFolder Returns the specified folder

GetTempName Returns a randomly generated temporary file or folder name

MoveFile Moves a file

MoveFolder Moves a folder

OpenTextFile Opens a file and returns a TextStream object to access the file

The BuildPath Method

The BuildPath method appends a name to an existing path.

Syntax

object.BuildPath(path, name)

Part Description

object Required. The name of a FileSystemObject

path Required. The path to append a name to

name Required. The name appended to the path

Example

dim fs, path

set fs=CreateObject("Scripting.FileSystemObject") path=fs.BuildPath("c:\mydocuments", "mydoc")

set fs=nothing

The CopyFile Method

The CopyFile method copies a file or files, from one location to another.

Syntax

object.CopyFile source, destination, overwrite

Part Description

object Required. The name of a FileSystemObject

source Required. The files to copy

destination Required. Where to copy the files

overwrite Optional. Sets whether an existing file can be overwritten. True indicates that files can be overwritten, False indicates that files can not be overwritten. True is default

Example

dim fs

set fs=CreateObject("Scripting.FileSystemObject")

fs.CopyFile "c:\mydocuments\web\*.htm", "c:\webpages\"

set fs=nothing

The CopyFolder Method

The CopyFolder method copies a folder or folders, from one location to another.

Syntax

object.CopyFolder source, destination, overwrite

Part Description

object Required. The name of a FileSystemObject

source Required. The folders to copy

destination Required. Where to copy the folders

overwrite Optional. Sets whether an existing folder can be overwritten. True indicates that folders can be overwritten, False indicates that folders can not be overwritten. True is default

Example

dim fs

set fs=CreateObject("Scripting.FileSystemObject")

fs.CopyFolder "c:\mydocuments\web\*", "c:\webpages\"

set fs=nothing

The CreateFolder Method

The CreateFolder method creates a folder.

Syntax

object.CreateFolder(name)

Part Description

object Required. The name of a FileSystemObject

name Required. Name of the folder

Example

dim fs, f

set fs=CreateObject("Scripting.FileSystemObject")

set f=fs.CreateFolder("c:\asp")

set f=nothing

set fs=nothing

The CreateTextFile Method

The CreateTextFile method creates a file and returns a TextStream object that can be used to read from or write to the file.

Syntax

object.CreateTextFile(filename, overwrite, unicode)

Part Description

object Required. The name of a FileSystemObject or Folder Object

filename Required. Name of the file to create

overwrite Optional. Sets whether an existing file can be overwritten. True indicates that

the file can be overwritten, False indicates that the file can not be overwritten.

False is default

unicode Optional. Sets whether the file is created as a Unicode or an ASCII file. True

indicates that the file is created as a Unicode file, False indicates that the file is

created as an ASCII file. False is default

Example

dim fs, txtfile

set fs=CreateObject("Scripting.FileSystemObject")

set txtfile=fs.CreateTextFile("c:\somefile.txt")

set txtfile=nothing

set fs=nothing

The DeleteFile Method

The DeleteFile method deletes a specified file.

Syntax

object.DeleteFile filename, force

Part Description

object Required. The name of a FileSystemObject

filename Required. The name of the file to delete

force Optional. Sets whether the read-only files are deleted. True indicates that the

read-only files are deleted, False indicates that they are not deleted. False is

default

Example

dim fs

set fs=CreateObject("Scripting.FileSystemObject") fs.DeleteFile("c:\asp\introduction.asp")

set fs=nothing

The DeleteFolder Method

The DeleteFolder method deletes a specified folder and its contents.

Syntax

object.DeleteFolder foldername, force

Part Description

object Required. The name of a FileSystemObject

foldername Required. The name of the folder to delete

force Optional. Sets whether the read-only folders are deleted. True indicates that the

read-only folders are deleted, False indicates that they are not deleted. False is

default

Example

dim fs

set fs=CreateObject("Scripting.FileSystemObject") fs.DeleteFolder("c:\temp")

set fs=nothing

The DriveExists Method

The DriveExists method returns "true" if the specified drive exists, "false" if not.

Syntax

object.DriveExists(drive)

Part Description

object Required. The name of a FileSystemObject

drive Required. A drive letter or a complete path specification

Example

dim fs

set fs=Server.CreateObject("Scripting.FileSystemObject")

if fs.DriveExists("c:") = true then

Response.Write("Drive c: exists.")

else

Response.Write("Drive c: does not exist.")

end If

set fs=nothing

The FileExists Method

The FileExists method returns "true" if the specified file exists, "false" if not.

Syntax

object.FileExists(filename)

Part Description

object Required. The name of a FileSystemObject

filename Required. The name of the file we want to check the existence of

Example

dim fs

set fs=Server.CreateObject("Scripting.FileSystemObject")

if fs.FileExists("c:\asp\introduction.asp") = true then Response.Write("File c:\asp\introduction.asp exists.")

else

Response.Write("File c:\asp\introduction.asp does not exist.")

end If

set fs=nothing

The FolderExists Method

The FolderExists method returns "true" if the specified folder exists, "false" if not.

Syntax

object.FolderExists(foldername)

Part Description

object Required. The name of a FileSystemObject

foldername Required. The name of the folder we want to check the existence of

Example

dim fs

set fs=Server.CreateObject("Scripting.FileSystemObject")

if fs.FolderExists("c:\asp") = true then

Response.Write("Folder c:\asp exists.")

else

Response.Write("Folder c:\asp does not exist.")

end If

set fs=nothing

The GetAbsolutePathName Method

The GetAbsolutePathName method returns a complete path in the path specification.

Syntax

object.GetAbsolutePathName(pathspec)

Part Description

object Required. The name of a FileSystemObject

pathspec Required. Path specification to change to a complete and unambiguous path

The GetBaseName Method

The GetBaseName method returns the base name of the file or folder, in the specified path.

Syntax

object.GetBaseName(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path for the file or folder whose base name is to be returned

Example

dim fs

set fs=Server.CreateObject("Scripting.FileSystemObject") Response.Write(fs.GetBaseName("c:\winnt\cursors\3dgarro.cur"))

set fs=nothing

The GetDrive Method

The GetDrive method returns a Drive Object corresponding to the drive in a specified path.

Syntax

object.GetDrive(drivespec)

Part Description

object Required. The name of a FileSystemObject

drivespec Required. Can be a drive letter (c), a drive letter followed by a colon (c:), a

drive letter followed by a colon and path separator (c:\), or any network share

specification (\\computer2\share1)

Example

Dim fs, drive

set fs=Server.CreateObject("Scripting.FileSystemObject") drive=fs.GetDrive("c:")

Response.Write(drive)

set fs=nothing

The GetDriveName Method

The GetDriveName method returns the name of the drive.

Syntax

object.GetDriveName(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path for the component whose drive name is to be returned

Example

dim fs,p

set fs=Server.CreateObject("Scripting.FileSystemObject") p=fs.GetDriveName("c:\winnt\cursors\3dgarro.cur")

Response.Write(p)

set fs=nothing

The GetExtensionName Method

The GetExtensionName method returns the file extension name for the last component in a path.

Syntax

object.GetExtensionName(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path for the component whose extension name is to be returned

Example

dim fs

set fs=Server.CreateObject("Scripting.FileSystemObject") Response.Write(fs.GetExtensionName("c:\winnt\cursors\3dgarro.cur"))

set fs=nothing

The GetFile Method

The GetFile method returns a File Object corresponding to the file in a specified path.

Syntax

object.GetFile(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path to the file

Example

dim fs

set fs=Server.CreateObject("Scripting.FileSystemObject") fs.getfile("c:\winnt\cursors\3dgarro.cur")

set fs=nothing

The GetFileName Method

The GetFileName method returns the last file name or folder of a specified path, that is not part of the drive specification.

Syntax

object.GetFileName(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path to a specific file

Example

dim fs,p

set fs=Server.CreateObject("Scripting.FileSystemObject") p=fs.getfilename("c:\winnt\cursors\3dgarro.cur")

response.write(p)

set fs=nothing

The GetFolder Method

The GetFolder method returns a Folder Object corresponding to the folder in a specified path.

Syntax

object.GetFolder(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path to the specific folder

Syntax

dim fs,p

set fs=Server.CreateObject("Scripting.FileSystemObject") p=fs.getfolder("c:\winnt\cursors") response.write(p)

set fs=nothing

The GetParentFolderName Method

The GetParentFolderName method returns the name of the parent folder in a specified path.

Syntax

object.GetParentFolderName(path)

Part Description

object Required. The name of a FileSystemObject

path Required. The path for the file or folder whose parent folder name is to be returned

Example

Dim fs,p

set fs=Server.CreateObject("Scripting.FileSystemObject") p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur") Response.Write(p)

set fs=nothing

The GetSpecialFolder Method

The GetSpecialFolder method returns the specified folder.

Syntax

object.GetSpecialFolder(foldername)

Part Description

object Required. The name of a FileSystemObject

foldername Required. The name of the folder to be returned. 0 = WindowsFolder Contains

files installed by the Windows operating system 1 = SystemFolder Contains

libraries, fonts, and device drivers 2 = TemporaryFolder Used to store

temporary files. Its path is found in the TMP environment variable

The GetTempName Method

The GetFolder method returns a randomly generated temporary file or folder name that is useful for performing operations that require a temporary file or folder.

Syntax

object.GetTempName

Part Description

object Required. The name of a FileSystemObject

The MoveFile Method

The MoveFile method moves a file or files, from one location to another.

Syntax

object.MoveFile source, destination

Part Description

object Required. The name of a FileSystemObject

source Required. The files to be moved

destination Required. Where to move the files

Example

dim fs

set fs=CreateObject("Scripting.FileSystemObject")

fs.MoveFile "c:\mydocuments\web\*.gif", "c:\mydocuments\images\"

set fs=nothing

The MoveFolder Method

The MoveFolder method moves a folder or folders, from one location to another.

Syntax

object.MoveFolder source, destination

Part Description

object Required. The name of a FileSystemObject

source Required. The folders to be moved

destination Required. Where to move the folders

Example

dim fs

set fs = CreateObject("Scripting.FileSystemObject")

fs.MoveFolder "c:\mydocuments\web\", "c:\windows\desktop\"

set fs=nothing

The OpenTextFile Method

The OpenTextFile method opens a file and returns a TextStream object to access the file.

Syntax

object.OpenTextFile(filename, iomode, create, format)

Part Description

object Required. The name of a FileSystemObject

filename Required. Name of the file to open

iomode Optional. How to open the file. 1 = ForReading Opens a file for reading. It is

not possible to write to this file 2 = ForWriting Opens a file for writing. It is not

possible to read from this file 8 = ForAppending Opens a file and write to the

end of the file

create Optional. Sets whether new file can be created if the filename does not exist.

Use True if a new file can be created, and False if not. False is default

format Optional. The format of the file. 0 = TristateFalse Opens the file as ASCII. This

is the default -1 = TristateTrue Opens the file as Unicode -2 =

TristateUseDefault Opens the file using the system default

Example

Dim fs, f

set fs=Server.CreateObject("Scripting.FileSystemObject")

set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1) Response.Write(f.readall)

f.Close

set f=Nothing

set fs=Nothing

Properties

Property Description

Drives Returns a collection of all Drive Objects available on the machine

The Drives Property

The Drives property returns a collection of all Drive Objects available on the local machine.

Syntax

object.Drives

Part Description

object Required. The name of a FileSystemObject

Examples

1.File Exist

This example demonstrates how to first create a FileSystemObject Object, and then use the FileExists method to check if the file exists.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

If (fs.FileExists("c:\winnt\cursors\3dgarro.cur"))=true Then

Response.Write("File c:\winnt\cursors\3dgarro.cur exists.")

Else

Response.Write("File c:\winnt\cursors\3dgarro.cur does not exist.")

End If

set fs=nothing

%>

</body>

</html>

Output

File c:\winnt\cursors\3dgarro.cur does not exist.

2.Folder Exist

This example demonstrates how to use the FolderExists method to check if a folder exists.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

If fs.FolderExists("c:\temp") = true Then

Response.Write("Folder c:\temp exists.")

Else

Response.Write("Folder c:\temp does not exist.")

End If

set fs=nothing

%>

</body>

</html>

Output

Folder c:\temp exists.

3.Drive Exist

This example demonstrates how to use the DriveExists method to check if a drive exists.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

if fs.driveexists("c:") = true then

Response.Write("Drive c: exists.")

Else

Response.Write("Drive c: does not exist.")

End If

Response.write("<br>")

if fs.driveexists("g:") = true then

Response.Write("Drive g: exists.")

Else

Response.Write("Drive g: does not exist.")

End If

set fs=nothing

%>

</body>

</html>

Output

Drive c: exists.

Drive g: does not exist.

4.getDriveName

This example demonstrates how to use the GetDriveName method to get the name of a specified drive.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

p=fs.GetDriveName("c:\winnt\cursors\3dgarro.cur")

Response.Write("The drive name is: " & p)

set fs=nothing

%>

</body>

</html>

Output

The drive name is: c:

5.getParentFolderName

This example demonstrates how to use the GetParentFolderName method to get the name of the parent folder of a specified path.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")

Response.Write("The parent folder name of c:\winnt\cursors\3dgarro.cur is: " & p)

set fs=nothing

%>

</body>

</html>

Output

The parent folder name of c:\winnt\cursors\3dgarro.cur is: c:\winnt\cursors

6.getExtensionName

This example demonstrates how to use the GetExtensionName method to get the file extension of the last component in a specified path.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write("The file extension of the file 3dgarro is: ")

Response.Write(fs.GetExtensionName("c:\winnt\cursors\3dgarro.cur"))

set fs=nothing

%>

</body>

</html>

Output

The file extension of the file 3dgarro is: cur

7.getBaseName

This example demonstrates how to use the GetBaseName method to return the base name of the file or folder, in a specified path.

Coding

<html>

<body>

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write(fs.GetBaseName("c:\winnt\cursors\3dgarro.cur"))

Response.Write("<br>")

Response.Write(fs.GetBaseName("c:\winnt\cursors\"))

Response.Write("<br>")

Response.Write(fs.GetBaseName("c:\winnt\"))

set fs=nothing

%>

</body>

</html>

Output

3dgarro

cursors

winnt

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting