文件、目录文本文件等多种操作类

2016-02-19 09:41 7 1 收藏

下面是个文件、目录文本文件等多种操作类教程,撑握了其技术要点,学起来就简单多了。赶紧跟着图老师小编一起来看看吧!

【 tulaoshi.com - Web开发 】

一个使用fso的class,前面的文章也有提到。这里有比较多的一些实例。
%
Dim MyFileFolder
Set MyFileFolder = New FileFolderCls
''Response.Write MyFileFolder.MoveAFile("f:1234561.exe","f:1234562.txt")
''Response.Write MyFileFolder.MoveAFolder("f:456","f:ditg456")
''Response.Write MyFileFolder.ShowFileSystemType("i:")
''Response.Write MyFileFolder.CopyAFile("f:1234562.txt","f:1234563.txt")
''!!!!
''Response.Write MyFileFolder.CopyAFolder("f:123","f:789")
''Response.Write MyFileFolder.ShowFolderList("f:ditg")
''Response.Write MyFileFolder.ShowFileList("f:123123")
''Response.Write MyFileFolder.DeleteAFile("f:1234562.txt")
''Response.Write MyFileFolder.DeleteAFolder("f:456")
''Response.Write MyFileFolder.CreateFolderDemo("f:147")
''Response.Write MyFileFolder.GetFileSize("f:1234563.txt")
''Response.Write MyFileFolder.GetFolderSize("f:123123.txt")
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",1)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",2)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",3)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",4)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",5)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",6)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",7)&"br"
''Response.Write MyFileFolder.ShowFileAccessInfo("f:123123.txt",8)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",1)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",2)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",3)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",4)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",5)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",6)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",7)&"br"
''Response.Write MyFileFolder.ShowFolderAccessInfo("f:123",8)&"br"
''Response.Write MyFileFolder.WriteTxtFile("f:123cexo.txt","",1)&"br"
''Response.Write MyFileFolder.WriteTxtFile("f:123cexo.txt","cexowxfdw",1)&"br"
''Response.Write MyFileFolder.WriteTxtFile("f:123cexo.txt",Chr(13)&Chr(10)
&"cexowxfdw",2)&"br"
''Response.Write MyFileFolder.ReadTxtFile("f:123cexo.txt")&"br"
Response.Write MyFileFolder.DisplayLevelDepth("F:ditgditgbumenimages")&"br"
Class FileFolderCls
Public fso
Private Sub Class_Initialize()
Set fso = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate()
Set fso = Nothing
End Sub
''//==================================文件操作==================================
''取文件大小
Function GetFileSize(FileName)
''//功能:取文件大小
''//形参:文件名
''//返回值:成功为文件大小,失败为-1
''//
Dim f
If ReportFileStatus(FileName) = 1 Then
Set f = fso.Getfile(FileName)
GetFileSize = f.Size
Else
GetFileSize = -1
End if
End Function
''文件删除
Function DeleteAFile(filespec)
''//功能:文件删除
''//形参:文件名
''//返回值:成功为1,失败为-1
''//
If ReportFileStatus(filespec) = 1 Then
fso.DeleteFile(filespec)
DeleteAFile = 1
Else
DeleteAFile = -1
End if
End Function
''显示文件列表
Function ShowFileList(folderspec)
''//功能:目录存在时显示此目录下的所有文件
''//形参:目录名
''//返回值:成功为文件列表,失败为-1
''//
Dim f, f1, fc, s
If ReportFolderStatus(folderspec) = 1 Then
Set f = fso.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
''!!!
''文件复制
Function CopyAFile(SourceFile,DestinationFile)
''//功能:源文件存在时,才能对文件进行复制,目的文件无影响
''//形参:源文件,目的文件
''//返回值:成功为1,失败为-1
''//
Dim MyFile
If ReportFileStatus(SourceFile) = 1 Then
Set MyFile = fso.GetFile(SourceFile)
MyFile.Copy (DestinationFile)
CopyAFile = 1
Else
CopyAFile = -1
End if
End Function
''文件移动
''Response.Write MoveAFile("f:1234561.exe","f:1234562.txt")
Function MoveAFile(SourceFile,DestinationFile)
''//功能:源文件存在时目的文件不存在时才能对文件进行移动
''//形参:源文件,目的文件
''//返回值:成功为1,失败为-1
''//
If ReportFileStatus(SourceFile)=1 And ReportFileStatus(DestinationFileORPath) =
-1 Then
fso.MoveFile SourceFile,DestinationFileORPath
MoveAFile = 1
Else
MoveAFile = -1
End if
End Function
''文件是否存在?
''Response.Write ReportFileStatus("G: oftdelphimy_pro代码库.exe")
Function ReportFileStatus(FileName)
''//功能:判断文件是否存在
''//形参:文件名
''//返回值:成功为1,失败为-1
''//
Dim msg
msg = -1
If (fso.FileExists(FileName)) Then
msg = 1
Else
msg = -1
End If
ReportFileStatus = msg
End Function
''文件创建日期
''Response.Write ShowDateCreated("G: oftdelphimy_pro代码库.exe")
''Response.Write ShowDateCreated("G: oftdelphimy_pro复件 代码库.exe")
Function ShowDateCreated(filespec)
''//功能:文件创建日期
''//形参:文件名
''//返回值:成功:文件创建日期,失败:-1
''//
Dim f
If ReportFileStatus(filespec) = 1 Then
Set f = fso.GetFile(filespec)
ShowDateCreated = f.DateCreated
Else
ShowDateCreated = -1
End if
End Function
''文件属性
''Response.Write GetAttributes("G: oftdelphimy_pro复件 代码库.exe")
Function GetAttributes(FileName)
''//功能:显示文件属性
''//形参:文件名
''//返回值:成功:文件属性,失败:-1
''//
Dim f,Str
If ReportFileStatus(FileName) = 1 Then
Set f = fso.GetFile(FileName)
Select Case f.attributes
Case 0 Str="普通文件。没有设置任何属性。 "
Case 1 Str="只读文件。可读写。 "
Case 2 Str="隐藏文件。可读写。 "
Case 4 Str="系统文件。可读写。 "
Case 16 Str="文件夹或目录。只读。 "
Case 32 Str="上次备份后已更改的文件。可读写。 "
Case 1024 Str="链接或快捷方式。只读。 "
Case 2048 Str=" 压缩文件。只读。"
End Select
GetAttributes = Str
Else
GetAttributes = -1
End if
End Function
''最后一次访问/最后一次修改时间
''Response.Write ShowFileAccessInfo("G: oftdelphimy_pro复件 代码库.exe")
Function ShowFileAccessInfo(FileName,InfoType)
''//功能:显示文件创建时信息
''//形参:文件名,信息类别
''// 1 -----创建时间
''// 2 -----上次访问时间
''// 3 -----上次修改时间
''// 4 -----文件路径
''// 5 -----文件名称
''// 6 -----文件类型
''// 7 -----文件大小
''// 8 -----父目录
''// 9 -----根目录
''//返回值:成功为文件创建时信息,失败:-1
''//
Dim f, s
If ReportFileStatus(FileName) = 1 then
Set f = fso.GetFile(FileName)
Select Case InfoType
Case 1 s = f.DateCreated ''// 1 -----
创建时间
Case 2 s = f.DateLastAccessed ''// 2 -----上次访问
时间
Case 3 s = f.DateLastModified ''// 3 -----上次修改
时间
Case 4 s = f.Path ''// 4
-----文件路径
Case 5 s = f.Name ''// 5
-----文件名称
Case 6 s = f.Type ''// 6
-----文件类型
Case 7 s = f.Size ''// 7
-----文件大小
Case 8 s = f.ParentFolder ''// 8 -----
父目录
Case 9 s = f.RootFolder ''// 8 -----
根目录
End Select
ShowFileAccessInfo = s
ELse
ShowFileAccessInfo = -1
End if
End Function
''写文本文件
Function WriteTxtFile(FileName,TextStr,WriteORAppendType)
Const ForReading = 1, ForWriting = 2 , ForAppending = 8
Dim f, m
Select Case WriteORAppendType
Case 1: ''文件进行写操作
Set f = fso.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 = fso.OpenTextFile(FileName, ForAppending)
f.Write TextStr
f.Close
WriteTxtFile = 1
Else
WriteTxtFile = -1
End if
End Select
End Function
''读文本文件
Function ReadTxtFile(FileName)
Const ForReading = 1, ForWriting = 2
Dim f, m
If ReportFileStatus(FileName) = 1 then
Set f = fso.OpenTextFile(FileName, ForReading)
m = f.ReadLine
''m = f.ReadAll
''f.SkipLine
ReadTxtFile = m
f.Close
Else
ReadTxtFile = -1
End if
End Function
''建立文本文件
''//==================================目录操作==================================
''取目录大小
Function GetFolderSize(FolderName)
''//功能:取目录大小
''//形参:目录名
''//返回值:成功为目录大小,失败为-1
''//
Dim f
If ReportFolderStatus(FolderName) = 1 Then
Set f = fso.GetFolder(FolderName)
GetFolderSize = f.Size
Else
GetFolderSize = -1
End if
End Function
''创建的文件夹
Function CreateFolderDemo(FolderName)
''//功能:创建的文件夹
''//形参:目录名
''//返回值:成功为1,失败为-1
''//
Dim f
If ReportFolderStatus(Folderspec) = 1 Then
CreateFolderDemo = -1
Else
Set f = fso.CreateFolder(FolderName)
CreateFolderDemo = 1
End if
End Function
''!!!
''目录删除
Function DeleteAFolder(Folderspec)
''//功能:目录删除
''//形参:目录名
''//返回值:成功为1,失败为-1
''//
Response.write Folderspec
If ReportFolderStatus(Folderspec) = 1 Then
fso.DeleteFolder (Folderspec)
DeleteAFolder = 1
Else
DeleteAFolder = -1
End if
End Function
''显示目录列表
Function ShowFolderList(folderspec)
''//功能:目录存在时显示此目录下的所有子目录
''//形参:目录名
''//返回值:成功为子目录列表,失败为-1
''//
Dim f, f1, fc, s
If ReportFolderStatus(folderspec) = 1 Then
Set f = fso.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
''!!!!
''目录复制
Function CopyAFolder(SourceFolder,DestinationFolder)
''//功能:源目录存在时,才能对目录进行复制,目的目录无影响
''//形参:源目录,目的目录
''//返回值:成功为1,失败为-1
''//
''Dim MyFolder
''If ReportFolderStatus(SourceFolder) = 1 and ReportFolderStatus
(DestinationFolder) = -1 Then
''Set MyFolder = fso.GetFolder(SourceFolder)
fso.CopyFolder SourceFolder,DestinationFolder
CopyAFolder = 1
''Else
CopyAFolder = -1
''End if
End Function
''目录进行移动
Function MoveAFolder(SourcePath,DestinationPath)
''//功能:源目录存在时目的目录不存在时才能对目录进行移动
''//形参:源目录,目的目录
''//返回值:成功为1,失败为-1
''//
If ReportFolderStatus(SourcePath)=1 And ReportFolderStatus(DestinationPath)=0
Then
fso.MoveFolder SourcePath, DestinationPath
MoveAFolder = 1
Else
MoveAFolder = -1
End if
End Function
''判断目录是否存在
''Response.Write ReportFolderStatus("G: oftdelphimy_pro")
Function ReportFolderStatus(fldr)
''//功能:判断目录是否存在
''//形参:目录
''//返回值:成功为1,失败为-1
''//
Dim msg
msg = -1
If (fso.FolderExists(fldr)) Then
msg = 1
Else
msg = -1
End If
ReportFolderStatus = msg
End Function
''目录创建时信息
Function ShowFolderAccessInfo(FolderName,InfoType)
''//功能:显示目录创建时信息
''//形参:目录名,信息类别
''// 1 -----创建时间
''// 2 -----上次访问时间
''// 3 -----上次修改时间
''// 4 -----目录路径
''// 5 -----目录名称
''// 6 -----目录类型
''// 7 -----目录大小
''// 8 -----父目录
''// 9 -----根目录
''//返回值:成功为目录创建时信息,失败:-1
''//
Dim f, s
If ReportFolderStatus(FolderName) = 1 then
Set f = fso.GetFolder(FolderName)
Select Case InfoType
Case 1 s = f.DateCreated ''// 1 -----
创建时间
Case 2 s = f.DateLastAccessed ''// 2 -----上次访问
时间
Case 3 s = f.DateLastModified ''// 3 -----上次修改
时间
Case 4 s = f.Path ''// 4
-----文件路径
Case 5 s = f.Name ''// 5
-----文件名称
Case 6 s = f.Type ''// 6
-----文件类型
Case 7 s = f.Size ''// 7
-----文件大小
Case 8 s = f.ParentFolder ''// 8 -----
父目录
Case 9 s = f.RootFolder ''// 9 -----
根目录
End Select
ShowFolderAccessInfo = s
ELse
ShowFolderAccessInfo = -1
End if
End Function
Function DisplayLevelDepth(pathspec)
Dim f, n ,Path
Set f = fso.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
''//==================================磁盘操作==================================
''驱动器是否存在?
''Response.Write ReportDriveStatus("C:")
Function ReportDriveStatus(drv)
''//功能:判断磁盘是否存在
''//形参:磁盘
''//返回值:成功为1,失败为-1
''//
Dim msg
msg = -1
If fso.DriveExists(drv) Then
msg = 1
Else
msg = -1
End If
ReportDriveStatus = msg
End Function
''--------可用的返回类型包括 FAT、NTFS 和 CDFS。
''Response.Write ShowFileSystemType("C:")
Function ShowFileSystemType(drvspec)
''//功能:磁盘类型
''//形参:磁盘名
''//返回值:成功为类型:FAT、NTFS 和 CDFS,失败:-1
''//
Dim d
If ReportDriveStatus(drvspec) = 1 Then
Set d = fso. GetDrive(drvspec)
ShowFileSystemType = d.FileSystem
ELse
ShowFileSystemType = -1
End if
End Function
End Class

来源:https://www.tulaoshi.com/n/20160219/1591764.html

延伸阅读
标签: 电脑入门
Linux系统中如果你想要搜索文本文件的话,可以使用grep命令,通过grep命令,你可以搜索相关关键词文件,也可对符合条件的文本文件进行筛选,是个比较常用的命令,下面图老师小编就给大家介绍下Linux使用grep命令搜索文本文件的方法。 grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后...
标签: Web开发
一、读取文本文件的步骤如下: 1、创建 FileSystemObject 对象实例; % Set fso=Server.CreateObject("Scripting.FileSystemObject") % 2、使用FileSystemObject对象的OpenTextFile方法返回一个 TextStream 对象实例; % Set txtFile=fso.OpenTextFile(filename[, iomode[, create[, format]]]) % ...
Java的IO操作都是基于流进行操作的,为了提高读写效率一般需要进行缓冲。     简单的示例程序如下:         /**  * 读出1.txt中的内容,写入2.txt中  *  */ import java.io.*; public class ReadWriteFile{  public static void main(String[] args){   try{ &nbs...
你需要做的第一件事是确定xp_cmdshell是可用的。你可以选择下面两种方法中的一种来实现。 1.你可以使用sp_configure并执行下面的脚本。 EXEC master.dbo.sp_configure 'show advanced options', 1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell', 1 RECONFIGURE 2.你可以使用Surface Area Configura...
标签: ASP
  昨天我问过这个问题,不过没人回答到点上,今天搞定了.贴出来给觉得有用的朋友,希望其他朋友不用走弯路. '文件内容读取. Function LoadFile(ByVal File)     Dim objStream     On Error Resume Next     Set objStream = Server.CreateObject("ADODB.Stream")     If Err...

经验教程

515

收藏

69
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部