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 |