20-1 檔??路徑???

b ASP Aɮ׻PؿBzADnOa FileSystemObject A󴣨ѹɮשMؿإߡBRBƻs\C

Aڭ̬ݬݦbBzɮשΥؿ|ɡA FileSystemObject ҴѪ@ǤkAШUCdҡG

Example]fileAccess/pathFunction01.asp^G

bWzdҤAڭ̥ physicalPath = Request.ServerVariables("PATH_TRANSLATED") ӨXdɮת|AMAϥΦUبƨӨX|nAdҪlXpUG

lɡ]fileAccess/pathFunction01.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title="|Bz\" %>
<!--#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
WG
  • |G@~tΩҬݨ쪺|C
  • Web |GAҬݨ쪺|C
@ӨAڭ̦b ASP {XҥΪɮצWٳOAҬݨ쪺W١Ag Server.MapPath() ơA~зǪ|Aɤ~ɮ׶iUسBzC

WzdҬOH||ҡAP˪ơA]iHΦb Web |AOo쪺GjPpAdҦpUG

Example]fileAccess/pathFunction02.asp^G

bWzdҤAڭ̥ webPath = Request.ServerVariables("SCRIPT_NAME"); ӨXdɮת Web |AMAϥΦUبƨӨX|nAdҪlXpUG

lɡ]fileAccess/pathFunction02.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title=" Web |Bz\" %>
<!--#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"-->

Yns{ϺоBɮשθƧAШϥ FileSystemObject 󤤬kApUG

oTӨ禡JO@Ӹ|AXhOAڭ̴NiHϥΦӨoʽάOIskC

ҦpAڭ̥iHϥ FileSystemObject GetFile() kAXɮתAMC|ɮתݩʡCҦpG

Example]fileAccess/fileProp01.asp^G

dҪlXpUG

lɡ]fileAccess/fileProp01.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title="CXɮתݩ" %>
<!--#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"-->

Ynгy@ɮרöiŪgAiHϥ FileSystemObject OpenTextFile() kAШUCdҡG

Example]fileAccess/openTextFile01.asp^G

dҪlXpUG

lɡ]fileAccess/openTextFile01.asp^G]ǦϰUYi^
<%@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");
// gJɮסG2 NgJAtrue NYɮפsbAh۰ʲͷsɮ
fid = fso.OpenTextFile(absPath, 2, true);
string = "oO@ӴաI";
fid.WriteLine(string);
fid.Close();
Response.Write("<p>wgɮסu" + fileName + "vügJru" + string + "vI");
// ŪXɮסG1 NŪ
fid = fso.OpenTextFile(absPath, 1);
output = fid.ReadAll();
fid.Close();
Response.Write("<p>ŪXeOGu" + output + "v");
%>

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

ҲͪɮפepUG

lɡ]fileAccess/test.txt^G]ǦϰUYi^
oO@ӴաI

YnˬdwЪŶAidҡG

Example]fileAccess/diskSpace01.asp^G

dҪlXpUG

lɡ]fileAccess/diskSpace01.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title="ˬdwЩҳѪŶ" %>
<!--#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 {]pPΡGΩAݪ ASP