ASP--文件和文件夹操作

来源:互联网 发布:知世故而不世故出自哪 编辑:程序博客网 时间:2024/05/19 20:40

执行指定的ASP程序----Server.Execute方法
语法:Server.Execute(path)
用途:这个方法是IIS5.0 新增的功能,用途类似程序语言中的函数调用,也就是说,
您可以在ASP 程序中使用Server.Execute(path)方法调用Path 指定的ASP 程序,待

被调用
的程序执行完毕之后再返回原来的程序,继续执行接下来的指令。
page1.asp:
<html>
<head></head>
<body>
<p><%Response.write "调用Execute方法之前"%></p>
<%Server.Execute("page2.asp")%> '执行page2.asp后会再返回
<p><%Response.write "调用Execute方法之后"%></p>
</body>
</html>
page2.asp:
<html>
<head></head>
<body>
<p><%Response.write "这是page2.asp执行结果"%></p>
</body>
</html>
转移控制权至其它ASP 程序------Server.Transfer 方法
语法:Server.Transfer(path)
用途:这个方法也是IIS5.0 新增功能,用途是将目前ASP 程序的控制权转移至path
指定的ASP 程序,即使转移之后的程序已经执行完毕,控制权后权仍不会返回原来

的程序。
page3.asp
<html>
<head></head>
<body>
<p><%Response.write "调用Execute方法之前"%></p>
<%Server.Transfer("page2.asp")%> '执行page2.asp后会再返回
<p><%Response.write "调用Execute方法之后"%></p>
</body>
</html>
字符串编码-----Server.HTMLEncode 和Server.URLEncode 方法
Server.HTMLEncode(string)方法可以将string 进行编码,使它不会被浏览器解释为

HTML
语法。
范例:Response.Write<Server.HTMLEncode(“<p>”)>
此时浏览器收到的HTML 编译后的结果使&lt;p&gt;
因此在画面上显示为<p>
相反的,Server.URLEncode(string)方法可以将String 进行编码,以放入

QueryString
<html>
<head></head>
<body>
<p><%Response.write "这是page2.asp执行结果"%></p>
</body>
</html>
<html>
<head></head>
<body>
<p><%Response.write “调用Execute方法之前”%></p>
<%Server.Transfer(“page2.asp”)%> ‘执行page2.asp后会再返回
<p><%Response.write “调用Execute方法之后”%></p>
</body>
</html>
<html>
<head></head>
<body>
<p><%Response.write "这是page2.asp执行结果"%></p>
</body>
</html>


返回服务器。
范例:vote=<%=server.URLEncode("梁程鹏Kenan")%>
将虚拟路径转换为实际路径---Server.MapPath 方法
语法:Server.MapPth(path)
用途:将Path 指定的虚拟路径转换为实际路径
范例:Server.MapPath(“page.asp”) ‘找出page.asp 的实际路径
使用外挂对象----Server.CreateObject 方法
语法:Server.CreateObject(component)
用途:建立服务器组件得实例
范例:Server.CreateObject(“Scripting.FileSystemObject”)
<%
Set fso=server.createobject("scripting.filesystemobject")
'其中Set 是VBScript 关键字, 用来建立对象实例, fso 是这个对象实例的名称,
'“scripting.filesystemobject”是filesystemobject 服务器组件登陆在Registry 的

ID。
'我们可以使用这个对象的对象的DriveExists(Path)、
'FolderExists(Path)和
'FileExists(Path)方法来检查磁盘、文件夹或文件是否存在。
if fso.driveexists("c:") then
response.write "c:磁盘是存在的!"
else
response.write "c:磁盘是不存在的!"
end if
'set fso=nothing '释放资源
if fso.folderexists(server.mappath("\web")) then
response.write "web这个文件夹是存在的!"
else
response.write "web这个文件夹是不存在的!"
end if
if fso.fileexists(server.mappath("/index.asp")) then
response.write "index.asp这个文件是存在的!"
else
response.write "index.asp这个文件是不存在的!"
end if
%>
文件夹的建立、移动、重新命名、删除与复制

<%
Set fso=server.createobject("scripting.filesystemobject")
'1. 建立文件夹
'语法:fso.createfolder(foldername)
'用途:FileSystemObject 对象的CreateFolder 方法可以建立一个foldername 文件

夹,
'而且参数Foldername 必须使用实际路径。
'范例:
'fso.CreateFolder(Server.Mappath("\web"))

'2.移动、重新命名文件夹
'语法:fso.MoveFolder Source,Destination
'用途:FileSystemObject对象的MoveFolder方法可以将Source指定的文件夹移动


'Destination,而且参数Source、Destination必须使用实际路径。
'范例:fso.MoveFolder Server.MapPath("\web"),Server.MapPath("\koko\move")
'fso.CreateFolder(Server.Mappath("\koko"))
'fso.MoveFolder Server.MapPath("\web"),Server.MapPath("\koko\move")

'3.删除文件夹
'语法:fso.DeleteFolder Path.Force
'用途:FileSystemObject的DeleteFolder方法可以删除path指定的文件夹;Force

为布
'尔值,默认为False,不删除只读文件夹,若要采用默认值,可省略不写,若要删除只

读文件
'夹,Force的值要设置为True;参数path必须使用实际路径。
'范例:fso.DeleteFolder Server.mapPth("\web") '删除非只读文件夹web
'fso.DeleteFolder Server.mapPath("\web")
'要删除只读文件夹,必须改写fso.deletefolder
'server.mappath(“\web”),True

'4.复制文件夹
'语法:fso.CopyFolder Source,Destination
'用途:FileSystemObject 对象的CopyFolder 方法可以将source 指定的文件夹复制


'Destination,而且参数Soure\Destination 必须使用实际路径.
'范例:
fso.copyfolder server.mappath("\web"),server.mappath("\web2")

%>

文件的建立、移动、重命名、删除与复制

<%
Set fso=server.createobject("scripting.filesystemobject")

'1.建立新文件
'语法:fso.CreateTextFile(Filename,Overwrite,Unicode)
'用途:FileSystemObject 对象的CreateTextFile 方法可以建立文字文件,并返回一


'TextStream 对象实例;Filename 为文字文件的名称(必须使用实际路径);

Overwrite 为布
'尔值,若值为True,表示如存在着同名文件,便将其覆盖,否则布覆盖;Unicode 为

布尔值,
'若值为True,表示为Unicode 文本文件,否则为ASCII 文本文件;Overwrite 和

Unicode 的
'默认值皆为Flase,若要采用默认值,可省略不写。
'fso.createtextfile(server.mappath("\web\index.asp"))

'2. 移动、重新命名文件
'语法:fso.MoveFile Source,Destination
'用途: FileSystemObject 对象的MoveFile 方法将Source 指定的文件移动到
'Destination 中,而且参数Source、Destination 必须使用实际路径。
'范例:
'fso.movefile server.mappath("\web\index.asp"),server.mappath("\web

\kokoro.asp")

'3. 删除文件
'语法:fso.DeleteFile path,force
'用途:FileSystemObject 的DeleteFile 方法可以删除Path 指定的文件;Force 为布


'值,默认为False,不删除只读文件,若要采用默认值,可省略不写,若要删除制度文
'件,Force 的值要设置为True;参数path 必须使用实际路径。
'范例:
'fso.deletefile server.mappath("\delete")         '删除非只读文件
'fso.deletefile server.mappath("\delete_onlyread.txt"),true '删除只读文件

'4. 复制文件
'语法:fso.CopyFile Source,Destination,Overwrite
'用途:FileSystemObject 对象的CopyFile 方法可以将source 指定的文件复制到
'Destination,若有同名文件,且Overwrite 的值为True,表示将其覆盖,否则不覆盖


'范例:
fso.CopyFile server.mappath("\index.asp"),server.mappath("\web\kokoro.asp")
%>

文件的打开、读取与写入
1. 打开文件
方法一:使用FileSystemObject 对象的OpenTextFile 方法
语法:fso.OpenTextFile(Filename,Iomode,Create,Format)
用途:FileSystemObject 对象的OpenTextFile 方法可以打开Filename 指定的文字

文件,
并传回一个TextStream 对象实例;Iomode 为文本文件的打开方式,1 表示只读,

2 表示可写
(清楚文字文件的原始内容),8 表示附加到文本文件的后面(不清除文本文件的原

始内容),
默认值为1;Create 表示当文本文件不存在时,是否要加以建立,默认值为False;

Format
为文字文件的格式,1-表示Unicode 文本文件,0 表示ASCII 文件,-2 表示采用系统

默认值,
这个参数通常不会用到。
范例: Dim fso,ts ‘以只读模式打开index.txt 文件,若文件不存在,便建立
set fso=Server.CreateObject(“Scripting.FileSystemObject”)
set ts=fso.OpenTextFile(Server.MapPath(“index.txt”),1,true)
方法二:使用File 对象的OpenAsTextStream 方法
语法:objfile.OpenAsTextStream(Iomode,Format)
用途:File 对象的OpenAsTextStream 方法可以返回代表文件的TextStream 对象案

例;
Iomode 为文件的打开方式,1 表示只读,2 表示可写,8 表示附加到文本文件的后

面,默认
值为1;Format 为文本文件的格式,-1 表示Unicode 文本文件,ASCII 文本文件,

-2 表示采
用系统默认值,这个参数通常不会用到。
范例:Dim fso,objfile,ts ‘以只读模式打开index.txt,若文件不存在,便建立
Set fso=Server.CreateObject(“Scripting.FileSystemObject”)
Set objfile=fso.GetFile(Server.MapPath(“index.txt“))
Set ts=objfile.OpenAsTextStream(1,True)

一.读取文件
在您成功地打开文件并取得一个TextSteam 对象实例后,您可以分别使用

TextStream
对象提供的Read(Num)、ReadLine、ReadAll 方法从文件读取Num 个字符、一行

或整个文件。
1. 从文件读取Num 个字符
response.write "kokoro.asp文件不存在无法进行复制"
end if
set fso = nothing
%>
</body>
</html>


1. 从文件读取一行
语法:ts.ReadLine
用途:从文件指针的位置读取一行,然后存放至字符串变量中。
范例:Dim Aline
Aline=ts.ReadLine
Response.Write Aline
<html>
<head></head>
<body>
<%
dim fso,ts,chars
set fso=server.createobject(“scripting.filesystemobject”)
set ts=fso.opentextfile(server.mappath(“index.txt”),1)
do while not ts.AtEndOfStream ‘检查是否到达文件结尾
chars=ts.read(6) ‘读取6个字符,再派给字符串变量chars
response.write chars & “<br />” ‘输出chars的值和强制换行
loop
ts.close ‘关闭打开的文件
set ts=nothing ‘释放实例对象
set fso=nothing ‘释放实例对象
%>
</body>
</html>
<%
dim fso,ts,aline
set fso = server.createobject("scripting.filesystemobject")
set ts = fso.opentextfile(server.mappath("index.txt"), 1)
do while not ts.atendofstream '检查是否到达文件结尾
aline = ts.readline '读取一行,再指派给字符串变量Aline
response.write aline '输出Aline的值
response.write "<br />" '输出强制换行标记
loop
ts.close '关闭已打开的文件
set ts = nothing '释放Texstream对象
set fso = nothing '释放filesystemobject 对象
%>


3. 从文件读取全部内容
语法:ts.ReadAll
用途:读取整个文件的内容,然后存放至字符串变量
范例:Dim AllLines
AllLines=ts.ReadAll
Response.write AllLines
4. 写入文件
在您成功地打开文件并取得一个TextStream 对象实例后,您可以分别使用

TextStream
对象提供的Write(Sting)、WriteLine(String)、WriteBlankLines(Num)方法,在文

件内写
入字符串和换行符、Num 个换行字符。
语法:ts.Write(string)
用途:在文件内写入字符串
范例:ts.Write(“ASP 动态网页设计”)
语法:ts.WriteLine(string)
用途:在文件内写入字符串和换行符
范例:ts.Write(“ASP 动态网页设计”)
语法:ts.WriteBlankLines(num)
用途:在文件内写入Num 个换行字符
范例:ts.WriteBlankLines(“3”)
<html>
<body>
<%
dim fso,ts,alllines,result
set fso = server.createobject("scripting.filesystemobject")
set ts = fso.opentextfile(server.mappath("index.txt"), 1)
'在读取文件内容之前先使用If语句检查是否到达文件结尾
if not ts.atendofstream then
'读取全部内容,再指派给字符串变量AllLines
alllines = ts.readall
'使用Replace函数将字符串变量中的换行字符置换成强制
result= replace(alllines, vbcrlf, "<br>")
response.write result
end if
ts.close '关闭已打开的文件
set ts = nothing '释放TextStream对象
set fso = nothing '释放FileSystemObject对象
%>
</body>
</html>


8.11 如何设置Server-Side Include
语法:<!--#include file=”top.asp”-->

本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1076168

原创粉丝点击