asp文本文件操作

2018-09-06 10:41

阅读:2823

  <%
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
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)
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 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)
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


评论


亲,登录后才可以留言!