Declare Function GetFileTime Lib "kernel32" Alias "GetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
说明:Long,非零表示成功,零表示失败。会设置GetLastError
如果不需要特定的信息,那么lpCreationTime,lpLastAccessTime,lpLastWriteTime都可以设置为零(用ByVal As Long)。这个函数返回的文件时间采用UTC格式
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FileTime, lpLastAccessTime As FileTime, lpLastWriteTime As FileTime) As Long
Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
‘定义变量
Dim file As Long
Dim CreationTime As FileTime
Dim lastaccesstime As FileTime
Dim lastaccesstime As FileTime
‘定义结构
Private Type FileTime
dwLowDateTime As Long
dwHighDateTime As Long
Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Sub Form_Load()
CopyFile "c:\test1.txt", "c:\test2.txt", 1
End Sub
以上代码将c:\test1.txt 拷贝到c:\test2.txt
6、MoveFile, MoveFileEx
作用:移动文件。如dwFlags设为零,则MoveFile完全等价于MoveFileEx
声明形式:
Declare Function MoveFile& Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String)
Declare Function MoveFileEx& Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long)
Private Declare Function MoveFile& Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String)
Private Declare Function MoveFileEx& Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long)
Private Sub Form_Load()
MoveFile "c:\test.txt", "d:\test1.txt" ‘移动文件
MoveFileEx "d:\test1.txt", "c:\test.txt", MOVEFILE_REPLACE_EXISTING ‘再一次移动
End Sub
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Function Replice(ByVal S As String) As String
Dim tempS as String
If Len(tailChr) > 0 Then ' 检索成对标记
Do While Len(S) > 0
If Not flag Then ' 不处在检索“终点”状态
If InStr(S, headChr) > 0 Then
Replice = Replice & StrHead(S, headChr)
flag = True
Else ' 处在检索“终点”状态
Replice = Replice & S
Exit Do
End If
End If
If InStr(S, tailChr) > 0 Then ' 检索“终点”
tempS=StrHead(S, tailChr)
If mapObj Is Nothing Then
Replice = Replice & reStr
Else
' 如果存在映射类的话,调用映射类的Map函数
Replice = Replice & mapObj.Map(tempS)
End If
flag = False
Else
Exit Do
End If
Loop
Else ' 检索单字符串
Do While Len(S) > 0
If InStr(S, headChr) > 0 Then
Replice = Replice & StrHead(S, headChr)
Replice = Replice & reStr
Else
Replice = Replice & S
Exit Do
End If
Loop
End If
End Function
---- 三、完成文件替换
Public Function InitUnits
(ByVal fn As String) As Boolean
Dim F As New TextFile, S As String
Dim unit As reUnit
If F.OpenRead(fn) Then
Set reUnits = New Collection
Do While F.GetLine(S)
If Len(S) > 0 Then
Set unit = New reUnit
unit.Init S
reUnits.Add unit
End If
Loop
InitUnits = True
Else
InitUnits = False
End If
End Function
---- 2、单个文件替换
Public Function ReplaceFile(ByVal sFn As String,
ByVal dFn As String) As Boolean
Dim sFile As New TextFile, dFile As New TextFile
If Not sFile.OpenRead(sFn) Then GoTo reErr
If Not dFile.OpenWrit(dFn) Then GoTo reErr
Dim unit As reUnit
Dim buff As String
Do While sFile.GetLine(buff)
For Each unit In reUnits
'调用集合中的每一个单元进行替换
buff = unit.Replice(buff)
Next
If Len(buff) > 0 Then dFile.PutLine buff
Loop
ReplaceFile = True
Exit Function
reErr:
ReplaceFile = False
End Function
---- 3、替换过程
---- 利用前两个函数,实现文件替换的全过程,该过程需要“源文件”、“目标文件”及“条件定义文件”三个参数,也是整个程序所需的参数: Public Sub RepFile(ByVal sFn As String, ByVal dFn As String, ByVal defFn As String) Dim info As String If InitUnits(defFn) Then If ReplaceFile(sFn, dFn) Then info = "替换成功!" Else info = "文件不能打开!" End If Else info = "定义文件不能打开!" End If MsgBox info End Sub
Public Sub Main()
Dim cm As String
Dim sFn As String, dFn As String, defFn As String
cm = Command
If Len(cm) > 0 Then sFn = StrHead(cm, " ")
If Len(cm) > 0 Then dFn = StrHead(cm, " ")
If Len(cm) > 0 Then defFn = StrHead(cm, " ")
If Len(sFn) > 0 And Len(dFn) > 0 And Len(defFn) > 0 Then
RepFile sFn, dFn, defFn '条件满足,进行替换
Else
Dim frm As New frmRep '条件不足,建立窗体
frm.txtFn(0) = sFn '把已有条件预先填上
frm.txtFn(1) = dFn
frm.txtFn(2) = defFn
Public Function StrHead(ss As String,
ByVal c As String) As String
Dim i As Long
i = InStr(ss, c)
If i > 0 Then
StrHead = Mid(ss, 1, i - 1)
ss = Mid(ss, i + Len(c))
Else
StrHead = ss
ss = ""
End If
End Function
---- 2、文本文件类(TextFile)
Dim headChr As String '启始符
Dim tailChr As String '结束符
Dim reStr As String '替换为
Dim mapObj as Object '映射对象
---- 通过上面三个变量来描述替换条件:headChr和tailChr代表检索的起点和终点,当tailChr为零长时,表示只对某字符串进行替换。reStr是替换后的字符串,为零长时即表示清除。该类用下面两个函数来设置上面的三个变量:
Public Sub Init(ByVal S As String)
headChr = EscStr(StrHead(S, ","))
tailChr = EscStr(StrHead(S, ","))
reStr = EscStr(S)
If Left(reStr,1)="@" Then
'第一个字符是"@",它所代表的是一个映射类
Set mapObj = CreateObject(Mid(reStr,2))
End If
End Sub
Private Function EscStr(ByVal S As String) As String
Dim h As Long
EscStr = ""
Do While Len(S) > 0
EscStr = EscStr & StrHead(S, "&")
If Len(S) > 0 Then
h = "&H" & StrHead(S, ";")
EscStr = EscStr & Chr(h)
End If
Loop
End Function
---- 函数Init接受一个字符串S,其中应包含“起点”、“终点”和替换后的字符串,他们之间用逗号分隔。为了能将非键盘字符加入其中,可以用“&HHHH;”的形式将任意字符插入其间,HHHH表示一个十六进制数,通过EscStr函数来完成这种替代。
Public Function Map(Byval ss as String) as String
---- 我们规定,如果reStr的第一个字符是"@"的话,它所代表的就不是一直接替换为的字符串,而是的个映射类的标识名,我们将通过CreateObject函数来建立这个对象,在替换时调用它的Map函数。
Dim name As String, nianji As String, age As Integer
Dim name1 As String, nianji1 As String, age1 As Integer
Open "student.txt" For Input As #filenum
Input #filenum, name, nianji, age
Input #filenum, name1, nianji1, age1
Close #filenum
Dim ar As String * 1, i As Integer
Open "c:\student.txt" For Binary As #1
Open "c:\student2.txt" For Binary As #2
For i = 1 To LOF(1)
Get #1, , ar
Put #2, , ar
Next i