当前位置: 移动技术网 > IT编程>开发语言>.net > asp文本文件操作

asp文本文件操作

2018年09月18日  | 移动技术网IT编程  | 我要评论

宰相刘罗锅荔浦芋头,浙江中都集团,男性营养健德堂

<%
dim ss(20)
set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("qq.txt")
set txt=fs.opentextfile(file,1,true)
for n=1 to 20 ‘读取前二十行
if txt.atendofstream then exit for
do not txt.atendofstream
line=txt.readline
a=instr(line,"-")
s=left(line,a)
response.write line & "<br>"
loop
next
%>

readline:从文件中读取一行数据
read(n):从文件中读取n个字节的数据
readall:读取文件中的所有数据

========================fso=============================

究级fso函数,推荐使用。
<%
class cls_fso
public objfso
private sub class_initialize()
set objfso = server.createobject("scripting.filesystemobject")
end sub
private sub class_terminate()
set objfso = nothing
end sub

=======文件操作========
取文件大小
public function getfilesize(filename)
dim f
if reportfilestatus(filename) = 1 then
set f = objfso.getfile(filename)
getfilesize = f.size
else
getfilesize = -1
end if
end function

文件删除
public function deleteafile(filespec)
if reportfilestatus(filespec) = 1 then
objfso.deletefile(filespec)
deleteafile = 1
else
deleteafile = -1
end if
end function

显示文件列表
public function showfilelist(folderspec)
dim f, f1, fc, s
if reportfolderstatus(folderspec) = 1 then
set f = objfso.getfolder(folderspec)
set fc = f.files
for each f1 in fc
s = s & f1.name
s = s & "|"
next
showfilelist = s
else
showfilelist = -1
end if
end function

文件复制
public function copyafile(sourcefile, destinationfile)
dim myfile
if reportfilestatus(sourcefile) = 1 then
set myfile = objfso.getfile(sourcefile)
myfile.copy (destinationfile)
copyafile = 1
else
copyafile = -1
end if
end function

文件移动
public function moveafile(sourcefile,destinationfile)
if reportfilestatus(sourcefile) = 1 and reportfilestatus(destinationfileorpath) = -1 then
objfso.movefile sourcefile,destinationfileorpath
moveafile = 1
else
moveafile = -1
end if
end function

文件是否存在?
public function reportfilestatus(filename)
dim msg
msg = -1
if (objfso.fileexists(filename)) then
msg = 1
else
msg = -1
end if
reportfilestatus = msg
end function

文件创建日期
public function showdatecreated(filespec)
dim f
if reportfilestatus(filespec) = 1 then
set f = objfso.getfile(filespec)
showdatecreated = f.datecreated
else
showdatecreated = -1
end if
end function

文件属性
public function getattributes(filename)
dim f
dim strfileattributes
if reportfilestatus(filename) = 1 then
set f = objfso.getfile(filename)
select case f.attributes
case 0 strfileattributes = "普通文件。没有设置任何属性。 "
case 1 strfileattributes = "只读文件。可读写。 "
case 2 strfileattributes = "隐藏文件。可读写。 "
case 4 strfileattributes = "文件。可读写。 "
case 16 strfileattributes = "文件夹或目录。只读。 "
case 32 strfileattributes = "上次备份后已更改的文件。可读写。 "
case 1024 strfileattributes = "链接或快捷方式。只读。 "
case 2048 strfileattributes = " 压缩文件。只读。"
end select
getattributes = strfileattributes
else
getattributes = -1
end if
end function

最后一次访问/最后一次修改时间
public function showfileaccessinfo(filename,infotype)
//功能:显示文件创建时信息
//形参:文件名,信息类别
// 1 -----创建时间
// 2 -----上次访问时间
// 3 -----上次修改时间
// 4 -----文件路径
// 5 -----文件名称
// 6 -----文件类型
// 7 -----文件大小
// 8 -----父目录
// 9 -----根目录
dim f, s
if reportfilestatus(filename) = 1 then
set f = objfso.getfile(filename)
select case infotype
case 1 s = f.datecreated
case 2 s = f.datelastaccessed
case 3 s = f.datelastmodified
case 4 s = f.path
case 5 s = f.name
case 6 s = f.type
case 7 s = f.size
case 8 s = f.parentfolder
case 9 s = f.rootfolder
end select
showfileaccessinfo = s
else
showfileaccessinfo = -1
end if
end function

写文本文件
public function writetxtfile(filename,textstr,writeorappendtype)
const forreading = 1, forwriting = 2 , forappending = 8
dim f, m
select case writeorappendtype
case 1: 文件进行写操作
set f = objfso.opentextfile(filename, forwriting, true)
f.write textstr
f.close
if reportfilestatus(filename) = 1 then
writetxtfile = 1
else
writetxtfile = -1
end if
case 2: 文件末尾进行写操作
if reportfilestatus(filename) = 1 then
set f = objfso.opentextfile(filename, forappending)
f.write textstr
f.close
writetxtfile = 1
else
writetxtfile = -1
end if
end select
end function

读文本文件
public function readtxtfile(filename)
const forreading = 1, forwriting = 2
dim f, m
if reportfilestatus(filename) = 1 then
set f = objfso.opentextfile(filename, forreading)
m = f.readline
readtxtfile = m
f.close
else
readtxtfile = -1
end if
end function

建立文本文件

=======目录操作========
取目录大小
public function getfoldersize(foldername)
dim f
if reportfolderstatus(foldername) = 1 then
set f = objfso.getfolder(foldername)
getfoldersize = f.size
else
getfoldersize = -1
end if
end function

创建的文件夹
public function createfolderdemo(foldername)
dim f
if reportfolderstatus(folderspec) = 1 then
createfolderdemo = -1
else
set f = objfso.createfolder(foldername)
createfolderdemo = 1
end if
end function

目录删除
public function deleteafolder(folderspec)
response.write folderspec
if reportfolderstatus(folderspec) = 1 then
objfso.deletefolder (folderspec)
deleteafolder = 1
else
deleteafolder = -1
end if
end function

显示目录列表
public function showfolderlist(folderspec)
dim f, f1, fc, s
if reportfolderstatus(folderspec) = 1 then
set f = objfso.getfolder(folderspec)
set fc = f.subfolders
for each f1 in fc
s = s & f1.name
s = s & "|"
next
showfolderlist = s
else
showfolderlist = -1
end if
end function

目录复制
public function copyafolder(sourcefolder,destinationfolder)
objfso.copyfolder sourcefolder,destinationfolder
copyafolder = 1
copyafolder = -1
end function


目录进行移动
public function moveafolder(sourcepath,destinationpath)
if reportfolderstatus(sourcepath)=1 and reportfolderstatus(destinationpath)=0 then
objfso.movefolder sourcepath, destinationpath
moveafolder = 1
else
moveafolder = -1
end if
end function

判断目录是否存在
public function reportfolderstatus(fldr)
dim msg
msg = -1
if (objfso.folderexists(fldr)) then
msg = 1
else
msg = -1
end if
reportfolderstatus = msg
end function

目录创建时信息
public function showfolderaccessinfo(foldername,infotype)
//功能:显示目录创建时信息
//形参:目录名,信息类别
// 1 -----创建时间
// 2 -----上次访问时间
// 3 -----上次修改时间
// 4 -----目录路径
// 5 -----目录名称
// 6 -----目录类型
// 7 -----目录大小
// 8 -----父目录
// 9 -----根目录
dim f, s
if reportfolderstatus(foldername) = 1 then
set f = objfso.getfolder(foldername)
select case infotype
case 1 s = f.datecreated
case 2 s = f.datelastaccessed
case 3 s = f.datelastmodified
case 4 s = f.path
case 5 s = f.name
case 6 s = f.type
case 7 s = f.size
case 8 s = f.parentfolder
case 9 s = f.rootfolder

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网