ASP執行個體教程:FileSystemObject對象

來源:互聯網
上載者:User

FileSystemObject 對象

指定的檔案存在嗎?

本例示範如何首先建立FileSystemObject對象,然後使用FileExists方法來探測某檔案是否存在。

本範例程式碼如下:

以下為引用的內容:

<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists("c:\windows\cursors\xxx.cur"))=true Then
Response.Write("檔案 c:\windows\cursors\xxx.cur 存在。")
Else
      Response.Write("檔案 c:\windows\cursors\xxx.cur 不存在。")
End If
set fs=nothing
%>
</body>
</html>

本執行個體運行結果如下:

 檔案 c:\windows\cursors\xxx.cur 不存在。

指定的檔案夾存在嗎?

本例示範如何使用FolderExists方法探測某檔案夾是否存在。

本範例程式碼如下:

以下為引用的內容:

<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:\temp") = true Then
      Response.Write("檔案夾 c:\temp 存在。")
Else
      Response.Write("檔案夾 c:\temp 不存在。")
End If
set fs=nothing
%>
</body>
</html>

本執行個體運行結果如下:

 檔案夾 c:\temp 不存在。

指定的磁碟機存在嗎?

本例示範如何使用DriveExists方法來探測某個磁碟機是否存在。
 
本範例程式碼如下:

以下為引用的內容:

<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.driveexists("c:") = true then
      Response.Write("磁碟機 c: 存在。")
Else
      Response.Write("磁碟機 c: 不存在。")
End If
Response.write("<br>")
if fs.driveexists("g:") = true then
      Response.Write("磁碟機 g: 存在。")
Else
      Response.Write("磁碟機 g: 不存在。")
End If
set fs=nothing
%>
</body>
</html>

本執行個體運行結果如下:

 磁碟機 c: 存在。
磁碟機 g: 存在。

取得某個指定磁碟機的名稱

本例示範如何使用GetDriveName方法來取得某個指定的磁碟機的名稱。

本範例程式碼如下:

以下為引用的內容:

<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetDriveName("c:\windows\cursors\abc.cur")
Response.Write("磁碟機名稱是:" & p)
set fs=nothing
%>
</body>
</html>

本執行個體運行結果如下:

 磁碟機名稱是:c:

取得某個指定路徑的父資料夾的名稱

本例示範如何使用GetParentFolderName方法來取得某個指定的路徑的父資料夾的名稱。

本範例程式碼如下:

以下為引用的內容:

 <html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")
Response.Write("c:\windows\cursors\abc.cur 的父資料夾名稱是:" & p)
set fs=nothing
%>
</body>
</html>

本執行個體運行結果如下:

c:\windows\cursors\abc.cur 的父資料夾名稱是:c:\winnt\cursors

取得檔案夾副檔名

本例示範如何使用GetExtensionName方法來取得指定的路徑中的最後一個成分的副檔名。

本範例程式碼如下:

以下為引用的內容:

<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write("檔案 3dgarro 的副檔名是:")
Response.Write(fs.GetExtensionName("c:\windows\cursors\abc.cur"))
set fs=nothing
%>
</body>
</html>

 本執行個體運行結果如下:

 檔案 3dgarro 的副檔名是:cur

取得檔案名稱

本例示範如何使用GetFileName方法來取得指定的路徑中的最後一個成分的檔案名稱。

本範例程式碼如下:

以下為引用的內容:

 <html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write("這個檔案名稱的最後一個成分是:")
Response.Write(fs.GetFileName("c:\windows\cursors\abc.cur"))
set fs=nothing
%>
</body>
</html>

本執行個體運行結果如下:

 這個檔案名稱的最後一個成分是:abc.cur

取得檔案或檔案夾的基名稱

本例示範如何使用GetBaseName方法來返回在指定的路徑中檔案或者檔案夾的基名稱。

本範例程式碼如下:

以下為引用的內容:

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

本執行個體運行結果如下:

以下為引用的內容:

 abc
cursors
windows

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.