20-1 �ɮ׻P���|�B�z

¦b ASP ¤¤¡A¹ï©óÀÉ®×»P¥Ø¿ýªº³B²z¡A¥D­n¬O¾a FileSystemObject ª«¥ó¡A¦¹ª«¥ó´£¨Ñ¹ï©óÀɮשM¥Ø¿ýªº«Ø¥ß¡B§R°£¡B½Æ»sµ¥¥\¯à¡C

­º¥ý¡A§Ú­Ì¬Ý¬Ý¦b³B²zÀɮשΥؿýªº¸ô®|®É¡A FileSystemObject ª«¥ó©Ò´£¨Ñªº¤@¨Ç¤èªk¡A½Ð¨£¤U¦C½d¨Ò¡G

Example¡]fileAccess/pathFunction01.asp¡^¡G

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¥ý¥Î physicalPath = Request.ServerVariables("PATH_TRANSLATED") ¨Ó¨ú¥X½d¨ÒÀɮתº¹êÅé¸ô®|¡AµM«á¦A¨Ï¥Î¦UºØ¨ç¼Æ¨Ó¨ú¥X¦¹¸ô®|ªº­«­n³¡¤À¡A¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/pathFunction01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="¹ï©ó¹êÅé¸ô®|ªº³B²z¥\¯à" %>
<!--#include file="../head.inc"-->
<hr>

<%
physicalPath=Request.ServerVariables("PATH_TRANSLATED");
fso = Server.CreateObject("Scripting.FileSystemObject");
methods = [
	"GetAbsolutePathName",
	"GetFileName",
	"GetBaseName",
	"GetExtensionName",
	"GetDriveName",
	"GetParentFolderName"];
%>
<h3 align=center>physicalPath = <%=physicalPath%></h3>
<table border=1 align=center>
<% for (i=0; i<methods.length; i++){%>
	<tr><td><%cmd="fso." + methods[i] + "(physicalPath)";%><%=cmd%><td>&nbsp;<font color=green><%=eval(cmd)%></font>
<%}%>
</table>

<hr>
<!--#include file="../foot.inc"-->

¡]¥»½d¨Ò¤]¦³ VBScript ªºª©¥»¡A½Ð¨£ fileAccess/pathFunction01_vbs.asp¡^

Hint
¦Wµü»¡©ú¡G
  • ¹êÅé¸ô®|¡G¥»¾÷§@·~¨t²Î©Ò¬Ý¨ìªº¸ô®|¡C
  • Web ¸ô®|¡Gºô­¶¦øªA¾¹©Ò¬Ý¨ìªº¸ô®|¡C
¤@¯ë¦Ó¨¥¡A§Ú­Ì¦b ASP µ{¦¡½X¤¤©Ò¥ÎªºÀɮצWºÙ³£¬O¦øªA¾¹©Ò¬Ý¨ìªº¦WºÙ¡A¥²¶·¸g¥Ñ Server.MapPath() ¨ç¼Æ¡A¤~¯àÂà¨ì¼Ð·Çªº§¹¾ã¸ô®|¡A¦¹®É¤~¯à¹ïÀÉ®×¶i¦æ¦UºØ³B²z¡C

¤W­z½d¨Ò¬O¥H¹êÅé¸ô®|Á|¨Ò¡A¦P¼Ëªº¨ç¼Æ¡A¤]¥i¥H¥Î¦b Web ¸ô®|¡A¦ý¬O±o¨ìªºµ²ªG¤j¦P¤p²§¡A½d¨Ò¦p¤U¡G

Example¡]fileAccess/pathFunction02.asp¡^¡G

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¥ý¥Î webPath = Request.ServerVariables("SCRIPT_NAME"); ¨Ó¨ú¥X½d¨ÒÀɮתº Web ¸ô®|¡AµM«á¦A¨Ï¥Î¦UºØ¨ç¼Æ¨Ó¨ú¥X¦¹¸ô®|ªº­«­n³¡¤À¡A¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/pathFunction02.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="¹ï©ó Web ¸ô®|ªº³B²z¥\¯à" %>
<!--#include file="../head.inc"-->
<hr>

<%
webPath=Request.ServerVariables("SCRIPT_NAME");
fso = Server.CreateObject("Scripting.FileSystemObject");
methods = [
	"GetAbsolutePathName",
	"GetFileName",
	"GetBaseName",
	"GetExtensionName",
	"GetDriveName",
	"GetParentFolderName"];
%>
<h3 align=center>webPath = <%=webPath%></h3>
<table border=1 align=center>
<% for (i=0; i<methods.length; i++){%>
	<tr><td><%cmd="fso." + methods[i] + "(webPath)";%><%=cmd%><td>&nbsp;<font color=green><%=eval(cmd)%></font>
<%}%>
</table>

<hr>
<!--#include file="../foot.inc"-->

­Y­n¦s¨ú²{¦³ªººÏºÐ¾÷¡BÀÉ®×©Î¸ê®Æ§¨¡A½Ð¨Ï¥Î FileSystemObject ª«¥ó¤¤¬ÛÃöªº¤èªk¡A¦p¤U¡G

³o¤T­Ó¨ç¦¡ªº¿é¤J³£¬O¤@­Ó¸ô®|¡A¿é¥X«h¬O¬ÛÃöªºª«¥ó¡A§Ú­Ì´N¥i¥H¨Ï¥Î¦¹ª«¥ó¨Ó¨ú±o¬ÛÃöªº©Ê½è©Î¬O©I¥s¬ÛÃöªº¤èªk¡C

¨Ò¦p¡A§Ú­Ì¥i¥H¨Ï¥Î FileSystemObject ª«¥óªº GetFile() ¤èªk¡A§ì¥XÀɮת«¥ó¡AµM«á¦CÁ|¦¹Àɮת«¥óªºÄݩʡC¨Ò¦p¡G

Example¡]fileAccess/fileProp01.asp¡^¡G

¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/fileProp01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="¦C¥XÀɮתºÄÝ©Ê" %>
<!--#include file="../head.inc"-->
<hr>

<%
// Reference: http://www.ezineasp.net/post/Javascript-FSO-GetFile-Method.aspx
fso = Server.CreateObject("Scripting.FileSystemObject");
fullPath = Request.ServerVariables("PATH_TRANSLATED");
file = fso.GetFile(fullPath);
methods = [
	"Attributes",
	"DateCreated",
	"DateLastAccessed",
	"DateLastModified",
	"Drive",
	"Name",
	"ParentFolder",
	"Path",
	"ShortName",
	"ShortPath",
	"Size",
	"Type"];
%>
<h3 align=center>file = <%=file%></h3>
<table border=1 align=center>
<% for (i=0; i<methods.length; i++){
	cmd = "file." + methods[i]; %>
	<tr><td><%=cmd%><td>&nbsp;<font color=green><%=eval(cmd)%></font>
<%}%>
</table>

<hr>
<!--#include file="../foot.inc"-->

­Y­n³Ð³y¤@­ÓÀɮרöi¦æÅª¼g¡A¥i¥H¨Ï¥Î FileSystemObject ª«¥óªº OpenTextFile() ¤èªk¡A½Ð¨£¤U¦C½d¨Ò¡G

Example¡]fileAccess/openTextFile01.asp¡^¡G

¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/openTextFile01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="¶}±ÒÀÉ®×¶i¦æÅª¼g" %>
<!--#include file="../head.inc"-->
<hr>

<%
fileName = "test.txt";
// Âন¹êÅéµ´¹ï¸ô®|
absPath=Server.MapPath(fileName);
Response.Write("absPath="+absPath);
fso = new ActiveXObject("Scripting.FileSystemObject");
// ¼g¤JÀɮסG2 ¥Nªí¼g¤J¡Atrue ¥Nªí­YÀɮפ£¦s¦b¡A«h¦Û°Ê²£¥Í·sÀÉ®×
fid = fso.OpenTextFile(absPath, 2, true);
string = "³o¬O¤@­Ó´ú¸Õ¡I";
fid.WriteLine(string);
fid.Close();
Response.Write("<p>¤w¸g²£¥ÍÀɮסu" + fileName + "¡v¨Ã¼g¤J¤å¦r¡u" + string + "¡v¡I");
// Ū¥XÀɮסG1 ¥Nªí°ßŪ
fid = fso.OpenTextFile(absPath, 1);
output = fid.ReadAll();
fid.Close();
Response.Write("<p>Ū¥Xªº¤º®e¬O¡G¡u" + output + "¡v");
%>

<hr>
<!--#include file="../foot.inc"-->

©Ò²£¥ÍªºÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]fileAccess/test.txt¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
³o¬O¤@­Ó´ú¸Õ¡I

­Y­nÀˬdµwºÐªÅ¶¡¡A¥i¨£¦¹½d¨Ò¡G

Example¡]fileAccess/diskSpace01.asp¡^¡G

¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/diskSpace01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="ÀˬdµwºÐ©Ò³ÑªÅ¶¡" %>
<!--#include file="../head.inc"-->
<hr>

<%
function showFreeSpace(drivePath){
	var fso, d, out;
	fso = new ActiveXObject("Scripting.FileSystemObject");
	d = fso.GetDrive(fso.GetDriveName(drivePath));
	out = d.VolumeName + " (" + drivePath + ") ";
	out = out + " ===> Free Space: " + d.FreeSpace/1024/1024 + " MB<br>";
	return(out);
}
%>

<%Response.write(showFreeSpace("c:"))%>
<%Response.write(showFreeSpace("d:"))%>

<hr>
<!--#include file="../foot.inc"-->


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò