ASP instance Tutorial: FileSystemObject object

Source: Internet
Author: User

FileSystemObject object

Does the specified file exist?

This example shows how to create a FileSystemObject object first, and then use the FileExists method to detect whether a file exists.

The sample code is as follows:

Reference content is as follows:

<Html>
<Body>
<%
Set fs = Server. CreateObject ("Scripting. FileSystemObject ")
If (fs. FileExists ("c: \ windows \ cursors \ xxx. cur") = true Then
Response. Write ("file c: \ windows \ cursors \ xxx. cur exists. ")
Else
Response. Write ("file c: \ windows \ cursors \ xxx. cur does not exist. ")
End If
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

File c: \ windows \ cursors \ xxx. cur does not exist.

Does the specified folder exist?

This example shows how to use the FolderExists method to check whether a folder exists.

The sample code is as follows:

Reference content is as follows:

<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>

The running result of this instance is as follows:

The folder c: \ temp does not exist.

Does the specified drive exist?

This example shows how to use the DriveExists method to detect whether a drive exists.
 
The sample code is as follows:

Reference content is as follows:

<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: Yes. ")
Else
Response. Write ("drive g: does not exist. ")
End If
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

Drive c: exists.
Drive g: exists.

Obtains the name of a specified drive.

This example shows how to use the GetDriveName method to obtain the name of a specified drive.

The sample code is as follows:

Reference content is as follows:

<Html>
<Body>
<%
Set fs = Server. CreateObject ("Scripting. FileSystemObject ")
P = fs. GetDriveName ("c: \ windows \ cursors \ abc. cur ")
Response. Write ("drive name:" & p)
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

Drive name: c:

Obtains the name of the parent folder of a specified path.

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

The sample code is as follows:

Reference content is as follows:

<Html>
<Body>
<%
Set fs = Server. CreateObject ("Scripting. FileSystemObject ")
P = fs. GetParentFolderName ("c: \ winnt \ cursors \ 3dgarro. cur ")
Response. Write ("c: \ windows \ cursors \ abc. cur's parent folder name is:" & p)
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

The parent folder name of c: \ windows \ cursors \ abc. cur is c: \ winnt \ cursors.

Get the folder Extension

This example shows how to use the GetExtensionName method to obtain the file extension of the last component in the specified path.

The sample code is as follows:

Reference content is as follows:

<Html>
<Body>
<%
Set fs = Server. CreateObject ("Scripting. FileSystemObject ")
Response. Write ("file 3 dgarro file extension is :")
Response. Write (fs. GetExtensionName ("c: \ windows \ cursors \ abc. cur "))
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

File 3 dgarro File Extension: cur

Get File Name

This example shows how to use the GetFileName method to obtain the name of the last component in the specified path.

The sample code is as follows:

Reference content is as follows:

<Html>
<Body>
<%
Set fs = Server. CreateObject ("Scripting. FileSystemObject ")
Response. Write ("the last component of this file name is :")
Response. Write (fs. GetFileName ("c: \ windows \ cursors \ abc. cur "))
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

The last component of the file name is abc. cur.

Obtains the base name of a file or folder.

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

The sample code is as follows:

Reference content is as follows:

<Html>
<Body>
<%
Set fs = Server. CreateObject ("Scripting. FileSystemObject ")
Response. Write (fs. GetBaseName ("c: \ windows \ cursors \ abc. cur "))
Response. Write ("<br/> ")
Response. Write (fs. GetBaseName ("c: \ windows \ cursors \"))
Response. Write ("<br/> ")
Response. Write (fs. GetBaseName ("c: \ windows \"))
Set fs = nothing
%>
</Body>
</Html>

The running result of this instance is as follows:

Reference content is as follows:

Abc
Cursors
Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.