|
发表于 2014-8-16 19:10:18
|
显示全部楼层
本帖最后由 kx25 于 2014-8-16 19:55 编辑
Name xx As xx 用 MoveFileW StrPtr(是Unicode), StrPtr(非Unicode) 代替:
Private Declare Function MoveFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long) As Long
将【Unicode文件路径】改成【 非 Unicode文件路径】,然后想干什么就干什么!
MoveFileW 和 枚举 支持带“.\”的畸形路径- Private Declare Function MoveFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long) As Long
- Private Function SearchFilesDirW(ByRef CustomArray() As String, ByRef Amount As Long, _
- Optional ByVal FileFormat As String = "*", Optional ByVal sPath As String, _
- Optional vNote As String = "递归文件") As Boolean ' DIR递归文件(包括子目录、隐藏文件夹、系统文件夹)
- '
- Dim s As String, s1 As String, T$$$$, A&, V$$$$(), K&, L&, b As Boolean, R$$$$, NewDir$$$$, nd&, fold$$$$()
- Dim i As VbFileAttribute
- '
- If Left(FileFormat, 1) <> ";" Then FileFormat = ";" & FileFormat
- If Right(FileFormat, 1) <> ";" Then FileFormat = FileFormat & ";"
- If Right(sPath, 1) <> "" Then sPath = sPath & ""
- '
- i = vbDirectory Or vbHidden Or vbSystem Or vbReadOnly Or vbNormal Or vbArchive
- s = DirW(sPath, i)
- Do Until s = vbNullString
- If s Like "?*.?*" Then
- If FileFormat = ";*;" Or Len(FileFormat) < 1 Then
- Call SetValueToStrArray(CustomArray, Amount, sPath & s)
- Else
- A = InStrRev(s, ".")
- If A > 1 Then
- T = ";" & UCase(Mid(s, A + 1)) & ";"
- b = InStr(FileFormat, T)
- If b > 0 Then Call SetValueToStrArray(CustomArray, Amount, sPath & s)
- End If
- End If
- Else
- If s <> "." And s <> ".." Then
- nd = nd + 1
- ReDim Preserve fold$$$$(nd)
- fold(nd) = sPath & s & ""
- fold(nd) = Replace(fold(nd), "\", "")
- If Right(fold(nd), 2) = "." Then fold(nd) = Left(fold(nd), Len(fold(nd)) - 2) & ".."
- End If
- End If
- DoEvents
- s = DirW
- Loop
- '
- If nd > 0 Then
- For K = 1 To nd
- Call SearchFilesDirW(CustomArray, Amount, FileFormat, fold(K))
- List1.AddItem fold(K)
- Next
- End If
- End Function
-
- Private Function SetValueToStrArray(ArrayName() As String, ArrayAmount As Long, _
- ByVal ArrayValue As String, Optional vNote As String = "字符串数组赋值") As Boolean
- '
- ArrayAmount = ArrayAmount + 1
- ReDim Preserve ArrayName(1 To ArrayAmount) As String
- ArrayName(ArrayAmount) = ArrayValue
- End Function
- Private Function SearchFolderDirW(ByRef CustomArray() As String, ByRef Amount As Long, _
- Optional ByVal sPath As String, Optional vNote As String = "递归子目录") As Boolean ' DIR递归子目录(包括子目录、隐藏文件夹、系统文件夹)
- '
- Dim s As String, s1 As String, T$$$$, A&, V$$$$(), K&, L&, b As Boolean, R$$$$, NewDir$$$$, nd&, fold$$$$()
- Dim i As VbFileAttribute
- '
- If Right(sPath, 1) <> "" Then sPath = sPath & ""
- '
- i = vbDirectory Or vbHidden Or vbSystem Or vbReadOnly Or vbNormal Or vbArchive
- s = DirW(sPath, i)
- Do Until s = vbNullString
- If s Like "?*.?*" Then
- '
- Else
- If s <> "." And s <> ".." Then
- nd = nd + 1
- ReDim Preserve fold$$$$(nd)
- fold(nd) = sPath & s & ""
- fold(nd) = Replace(fold(nd), "\", "")
- If Right(fold(nd), 2) = "." Then fold(nd) = Left(fold(nd), Len(fold(nd)) - 2) & ".."
- Call SetValueToStrArray(CustomArray, Amount, fold(nd))
- End If
- End If
- DoEvents
- s = DirW
- Loop
- '
- If nd > 0 Then
- For K = 1 To nd
- Call SearchFolderDirW(CustomArray, Amount, fold(K))
- Next
- End If
- End Function
- Private Sub RenameFile(ByVal sOldName As String, ByVal sNewName As String) ' 移动、重新命名文件夹或文件
- MoveFileW StrPtr(sOldName), StrPtr(sNewName)
- End Sub
-
- Private Sub Command4_Click()
- Dim DF$$$$(), A&, i&, L&, K&, R$$$$, b As Boolean
- '
- 'SearchFilesDirW DF, A, "*", TextBox1.Text ' 递归文件
- SearchFolderDirW DF, A, TextBox1.Text ' 递归子目录
- If A < 1 Then Exit Sub
- '
- List1.Clear
- For i = A To 1 Step -1
- Text2.Text = DF(i) ' TextBox 会把不支持的字符换成: 英文问号
- If InStr(1, Text2.Text, "?") > 0 Then ' 检查是否含有不支持的字符
- R = Replace(Text2.Text, "?", "?") ' 把不支持的字符换成: 中文问号
- '最好循环检测一下【路径 R 】是否已经存在,存在就在文件名末端加编号。
- Call RenameFile(DF(i), R) ' 将【Unicode 路径】改成【 非 Unicode 路径】,
- DF(i) = R ' 将路径写回数组
- End If
- List1.AddItem DF(i)
- Next
- End Sub
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|