?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
FileSystemObject 對(duì)象用于訪問(wèn)服務(wù)器上的文件系統(tǒng)。
FileSystemObject 對(duì)象用于訪問(wèn)服務(wù)器上的文件系統(tǒng)。
此對(duì)象可對(duì)文件、文件夾和目錄路徑進(jìn)行操作。也可通過(guò)此對(duì)象獲取文件系統(tǒng)的信息。
下面的代碼會(huì)創(chuàng)建一個(gè)文本文件 (c:\test.txt),然后向這個(gè)文件寫(xiě)一些文本:
<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%>
FileSystemObject 對(duì)象的屬性和方法描述如下:
屬性 | 描述 |
---|---|
Drives | 返回本地計(jì)算機(jī)上所有驅(qū)動(dòng)器對(duì)象的集合。 |
方法 | 描述 |
---|---|
BuildPath | 將一個(gè)名稱(chēng)追加到已有的路徑后。 |
CopyFile | 從一個(gè)位置向另一個(gè)位置拷貝一個(gè)或多個(gè)文件。 |
CopyFolder | 從一個(gè)位置向另一個(gè)位置拷貝一個(gè)或多個(gè)文件夾。 |
CreateFolder | 創(chuàng)建新文件夾。 |
CreateTextFile | 創(chuàng)建文本文件,并返回一個(gè)可以讀取或者寫(xiě)入文件的 TextStream 對(duì)象。 |
DeleteFile | 刪除一個(gè)或者多個(gè)指定的文件。 |
DeleteFolder | 刪除一個(gè)或者多個(gè)指定的文件夾。 |
DriveExists | 檢查指定的驅(qū)動(dòng)器是否存在。 |
FileExists | 檢查指定的文件是否存在。 |
FolderExists | 檢查指定的文件夾是否存在。 |
GetAbsolutePathName | 針對(duì)指定的路徑返回從驅(qū)動(dòng)器根部起始的完整路徑。 |
GetBaseName | 返回指定文件或者文件夾的基名稱(chēng)。 |
GetDrive | 返回指定路徑中所對(duì)應(yīng)的驅(qū)動(dòng)器的 Drive 對(duì)象。 |
GetDriveName | 返回指定的路徑的驅(qū)動(dòng)器名稱(chēng)。 |
GetExtensionName | 返回在指定的路徑中最后一個(gè)成分的文件擴(kuò)展名。 |
GetFile | 返回一個(gè)針對(duì)指定路徑的 File 對(duì)象。 |
GetFileName | 返回在指定的路徑中最后一個(gè)成分的文件名或者文件夾名。 |
GetFolder | 返回一個(gè)針對(duì)指定路徑的 Folder 對(duì)象。 |
GetParentFolderName | 返回在指定的路徑中最后一個(gè)成分的父文件夾名稱(chēng)。 |
GetSpecialFolder | 返回某些 Windows 的特殊文件夾的路徑。 |
GetTempName | 返回一個(gè)隨機(jī)生成的文件或文件夾。 |
MoveFile | 從一個(gè)位置向另一個(gè)位置移動(dòng)一個(gè)或多個(gè)文件。 |
MoveFolder | 從一個(gè)位置向另一個(gè)位置移動(dòng)一個(gè)或多個(gè)文件夾。 |
OpenTextFile | 打開(kāi)文件,并返回一個(gè)用于訪問(wèn)此文件的 TextStream 對(duì)象。 |
指定的文件存在嗎?
本例演示如何檢查某個(gè)文件是否存在。
<!DOCTYPE?html> <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>
指定的文件夾存在嗎?
本例演示如何檢查某個(gè)文件夾是否存在。
<!DOCTYPE?html> <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>
指定的驅(qū)動(dòng)器存在嗎?
本例演示如何檢查某個(gè)驅(qū)動(dòng)器是否存在。
<!DOCTYPE?html> <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>
取得某個(gè)指定驅(qū)動(dòng)器的名稱(chēng)
本例演示如何獲取某個(gè)指定的驅(qū)動(dòng)器的名稱(chēng)。
<!DOCTYPE?html> <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>
取得某個(gè)指定路徑的父文件夾的名稱(chēng)
本例演示如何獲取某個(gè)指定的路徑的父文件夾的名稱(chēng)。
<!DOCTYPE?html> <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>
取得文件名
本例演示如何獲取指定的路徑中的最后一個(gè)成分的文件名。
<!DOCTYPE?html> <html> <body> <% Set?fs=Server.CreateObject("Scripting.FileSystemObject") Response.Write("The?file?name?of?the?last?component?is:?") Response.Write(fs.GetFileName("c:\winnt\cursors\3dgarro.cur")) set?fs=nothing %> </body> </html>
取得文件擴(kuò)展名
本例演示如何獲取指定的路徑中的最后一個(gè)成分的文件擴(kuò)展名。
<!DOCTYPE?html> <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>
取得文件或文件夾的基名稱(chēng)
本例演示如何獲取指定的路徑中文件或者文件夾的基名稱(chēng)。
<!DOCTYPE?html> <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>