ASP读取TXT(zt)

2015-01-21 13:26:26  阅读 5503 次 评论 0 条

 

代码

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%
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
End select
ShowFolderAccessInfo = s
ELse
ShowFolderAccessInfo = -1
End if
End Function

'遍历目录
Public Function DisplayLevelDepth(pathspec)
Dim f, n ,Path
Set f = objFSO.GetFolder(pathspec)
If f.IsRootFolder Then
DisplayLevelDepth ="指定的文件夹是根文件夹。"&RootFolder
Else
Do Until f.IsRootFolder
Path = Path & f.Name &"<br>"
Set f = f.ParentFolder
n = n + 1
Loop
DisplayLevelDepth ="指定的文件夹是嵌套级为 " & n & " 的文件夹。<br>" & Path
End If
End Function

'========磁盘操作========
'驱动器是否存在?
Public Function ReportDriveStatus(drv)
Dim msg
msg = -1
If objFSO.DriveExists(drv) Then
msg = 1
Else
msg = -1
End If
ReportDriveStatus = msg
End Function

'可用的返回类型包括 FAT、NTFS 和 CDFS。
Public Function ShowFileSystemType(drvspec)
Dim d
If ReportDriveStatus(drvspec) = 1 Then
Set d = objFSO.GetDrive(drvspec)
ShowFileSystemType = d.FileSystem
ELse
ShowFileSystemType = -1
End if
End Function
End Class
%>

 


做了个简单的 读取一个txt文件
<%
dim line
set fs=server.createobject("scripting.filesystemobject") 
file=server.mappath("69863.txt") 
set txt=fs.opentextfile(file,1,true) 
do while not txt.atendofstream
line=txt.readline
response.write line & "<br>" 
loop
%>


<%
function HTMLEncode(fString)
fString = replace(fString, ">", "&gt;")
fString = replace(fString, "<", "&lt;")
fString = Replace(fString, CHR(32), "&nbsp;")
fString = Replace(fString, CHR(9), "&nbsp;")
fString = Replace(fString, CHR(34), "&quot;")
fString = Replace(fString, CHR(39), "&#39;")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
fString = Replace(fString, CHR(10), "<BR> ")
HTMLEncode = fString
end function
set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("00000001.TXT")
set txt=fs.opentextfile(file,1,true)
line=HTMLEncode(txt.ReadAll)
response.Write line
%>

本文地址:http://jinesc.net/?id=130
免责声明:本文为原创文章,版权归 jinesc 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?