vb如何遍历一个文件夹下的所有文件呀?
----------------------------------------------------
我想,从 A 目录拷贝文件到 B 目录,只拷贝 B目录不存在的文件。这就需要分别遍历 这两个目录呀,我如何遍历呢?
----------------------------------------------------
我想,从 A 目录拷贝文件到 B 目录,只拷贝 B目录不存在的文件。这就需要分别遍历 这两个目录呀,我如何遍历呢?
Dim filenames As String
filenames = Dir("c:\")
Do While filenames <> ""
Print filenames
filenames = Dir '再次调用dir函数,此时可以不带参数
Loop
End Subvb中固定写法
'窗体上添加一个filelistbox控件file1file1.path=目录A
file1.pattern="*.*"
file1.visible=falsefor i=0 to file1.listcount-1
if dir(目录B & file1.list(i))="" then
FileCopy 目录A & file1.list(i),目录B & file1.list(i)
endif
next i
Option ExplicitPrivate Declare Function FindFirstFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal wFD As Long) As Long
Private Declare Function FindNextFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal wFD As Long) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function CopyFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long, ByVal bFailIfExists As Long) As LongConst MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE As Long = -1Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As Currency
ftLastAccessTime As Currency
ftLastWriteTime As Currency
nFileSizeBig As Currency
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cShortFileName As String * 14
End TypePrivate Sub Form_Load()
Call test
End SubPublic Sub test()
Dim wFD As WIN32_FIND_DATA
Dim hFindFile As Long
Dim Filename As String
Dim CurPath As String
Dim TargetPath As String
Dim SourcePath As String CurPath = "\\?\c:\temp\source\*.*"
SourcePath = "\\?\c:\temp\source\"
TargetPath = "\\?\c:\temp\target\" hFindFile = FindFirstFileW(StrPtr(CurPath), VarPtr(wFD))
If hFindFile <> INVALID_HANDLE_VALUE Then
Do
Filename = StripNull(wFD.cFileName)
If AscW(Filename) <> 46 Then
Debug.Print Filename, CopyFileW(StrPtr(SourcePath & Filename), StrPtr(TargetPath & Filename), 0)
End If
Loop While FindNextFileW(hFindFile, VarPtr(wFD))
End If
FindClose hFindFileEnd SubPublic Function StripNull(ByVal StrIn As String) As String
Dim nul As Long
' Truncate input string at first null.
' If no nulls, perform ordinary Trim.
nul = InStr(1, StrIn, vbNullChar, vbBinaryCompare)
Select Case nul
Case Is > 1
StripNull = Left$(StrIn, nul - 1)
Case 1
StripNull = ""
Case 0
StripNull = Trim$(StrIn)
End Select
End Function你自己整理一下。API都有了。
'BY 嗷嗷叫的老马
'紫水晶工作室
'http://www.m5home.com/
Dim I() As String, J As Long<I = SearchFileInPath("c:\windows\web", "*.*")
For J = 0 To UBound(I)
Debug.Print I(J) '打印所有文件
Next
直接调用SearchFileInPath可以得到所有文件列表,保存在字符串数组里.然后想怎么处理就是你的事了.