VB如何批量修改文件名?
如D盘中N个.doc档文件名为:YE-PG-01、YT-PG-02、、、需在后面加上一个字母B,改为“YE-PG-01B"、“YT-PG-02B”、、、有源代码吗?...
如D盘中N个.doc档文件名为:YE-PG-01、YT-PG-02、、、需在后面加上一个字母B,改为“YE-PG-01B"、“YT-PG-02B”、、、 有源代码吗?
展开
2个回答
展开全部
列表框 List1
文本框 TxtFilePath
文本框 TxtFileName
按钮 CmdListFile
Private Sub CmdListFile_Click()
AutoListfiles TxtFilePath.txt, TxtFileName.Text
End Sub
'列出某个目录下所有指定类型文件(包含子目录)
'sDirName 目录名
'FileFilter 文件扩展名
'找到文件返回TRUE,一个也没找到则返回FALSE
Public Function AutoListfiles(ByVal sDirName As String, ByVal FileFilter As String) _
As Boolean
Dim sName As String
Dim sFile As String
Dim sExt As String
Dim sDirList() As String
Dim iDirNum As Integer
Dim i As Integer
On Error GoTo RF_ERROR
If Right$(sDirName, 1) <> "\ " Then sDirName = sDirName + "\ "
sFile = Dir(sDirName + FileFilter, vbNormal + vbArchive + vbHidden)
Do While Len(sFile) > 0
sFile = UCase(Trim(sFile))
'找到文件,在这里做你想做的
List1.AddItem sFile
sFile = Dir
Loop
iDirNum = 0
sName = Dir(sDirName + "*.* ", vbDirectory + vbNormal)
Do While Len(sName) > 0
If sName <> ". " And sName <> ".. " Then
iDirNum = iDirNum + 1
ReDim Preserve sDirList(1 To iDirNum)
sDirList(iDirNum) = sDirName + sName + "\ "
End If
sName = Dir
Loop
For i = 1 To iDirNum '递归调用
AutoListfiles sDirList(i), FileFilter
Next i
AutoListfiles = True
Exit Function
RF_ERROR:
MsgBox Err.Description, vbCritical, " "
AutoListfiles = False
End Function
后面的就很简单了。
文本框 TxtFilePath
文本框 TxtFileName
按钮 CmdListFile
Private Sub CmdListFile_Click()
AutoListfiles TxtFilePath.txt, TxtFileName.Text
End Sub
'列出某个目录下所有指定类型文件(包含子目录)
'sDirName 目录名
'FileFilter 文件扩展名
'找到文件返回TRUE,一个也没找到则返回FALSE
Public Function AutoListfiles(ByVal sDirName As String, ByVal FileFilter As String) _
As Boolean
Dim sName As String
Dim sFile As String
Dim sExt As String
Dim sDirList() As String
Dim iDirNum As Integer
Dim i As Integer
On Error GoTo RF_ERROR
If Right$(sDirName, 1) <> "\ " Then sDirName = sDirName + "\ "
sFile = Dir(sDirName + FileFilter, vbNormal + vbArchive + vbHidden)
Do While Len(sFile) > 0
sFile = UCase(Trim(sFile))
'找到文件,在这里做你想做的
List1.AddItem sFile
sFile = Dir
Loop
iDirNum = 0
sName = Dir(sDirName + "*.* ", vbDirectory + vbNormal)
Do While Len(sName) > 0
If sName <> ". " And sName <> ".. " Then
iDirNum = iDirNum + 1
ReDim Preserve sDirList(1 To iDirNum)
sDirList(iDirNum) = sDirName + sName + "\ "
End If
sName = Dir
Loop
For i = 1 To iDirNum '递归调用
AutoListfiles sDirList(i), FileFilter
Next i
AutoListfiles = True
Exit Function
RF_ERROR:
MsgBox Err.Description, vbCritical, " "
AutoListfiles = False
End Function
后面的就很简单了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询