VB批量修改文件名

2025-01-02 19:32:20
推荐回答(5个)
回答1:

假如一个文件为1a.txt一个为1b.txt呢,该怎么改,你总得说清楚。改了第一个就改不了第二个了。

====================================
'给你一个代码,不用控件的,你没说遇到改后文件名一样怎么办,所以我的做法是文件名一样就跳过,如果不含数字的也跳过。

'*************************************************************************
'**模 块 名:Form1
'**说 明:厦门大学化学系 版权所有2009 - 2010(C)
'**创 建 人:吴志明(寒江雪)
'**日 期:2009-03-10 19:15:18
'**联系方式:verywzm@163.com
'**主 页:http://hi.baidu.com/cfans
'**描 述:
'**版 本:V1.0.0
'*************************************************************************
Private Sub Command1_Click()
On Error GoTo ToExit '打开错误陷阱
'------------------------------------------------
Dim FileName, NewFileName As String
FileName = Dir("C:\temp\", vbReadOnly + vbHidden + vbSystem + vbNormal)

Do While Not FileName = ""

NewFileName = NUM(FileName)
If NewFileName <> "" And Left(NewFileName, 1) <> "." Then
Name "C:\temp\" & FileName As "c:\temp\" & NewFileName
End If
FileName = Dir()
Loop

'------------------------------------------------
Exit Sub
'----------------
ToExit:
Resume Next
End Sub

Private Function NUM(ByVal strTest As String) As String
Dim strExtend As String
Dim strRet As String
Dim bytArray() As Byte
Dim intcount As Integer
If Len(strTest) > 3 Then
If Mid(strTest, Len(strTest) - 3, 1) = "." Then strExtend = Right(strTest, 4)
End If
bytArray = strTest
For intcount = 0 To UBound(bytArray)
If bytArray(intcount) >= Asc("0") And bytArray(intcount) <= Asc("9") Then
strRet = strRet + Chr(bytArray(intcount))
End If
Next
strRet = strRet + strExtend
NUM = strRet
End Function

==================================
目录要任意的话把代码中"C:\temp\"换掉就好了嘛,比如换成text1.text

回答2:

我有个现成的
可以发给你看代码
你的代码明显的高复杂了
控件和你的一样。
要的话hi me

回答3:

Dim Buff() As String

Private Sub FilePath_Change()
FileList.Path = FilePath.Path
End Sub

Private Sub Done_Click()
Dim Cnt As Long
Dim FileName As String

Cnt = FileList.ListCount
ReDim Buff(Cnt) As String

For i = 0 To Cnt - 1
Buff(i) = FilePath.Path & "\" & FileList.List(i)
Next i

For i = 1 To Cnt
FileName = FilePath.Path & "\" & i & GetExt(Buff(i - 1))
If Dir(FileName, vbArchive) <> "" Then
mIndex = GetIndex(FileName)
Buff(mIndex) = Buff(i - 1)
Buff(i - 1) = FileName
Else
Name Buff(i - 1) As FileName
End If
FileList.Refresh
Next i

End Sub

Private Sub Drive1_Change()
FilePath.Path = Drive1.Drive
End Sub

Private Sub Form_Load()
FilePath.Path = Drive1.Drive
End Sub

Private Function GetExt(name As String) As String
For i = Len(name) To 1 Step -1
If Mid(name, i, 1) = "." Then
GetExt = Mid(name, i, Len(name) - i + 1)
Exit For
End If
Next i
End Function

Private Function GetIndex(name As String) As Long
GetIndex = -1
For i = 0 To FileList.ListCount - 1
If Buff(i) = name Then
GetIndex = i
Exit For
End If
Next i
End Function

回答4:

我有dir搜索指定目录所有文件的代码。改名也很简单,但是你的要求很模糊,如果有意思请详细说明要求,我给你代码,发消息即可。

回答5:

循环用dir函数遍历d:\a目录下所有文件得到文件名然后改名即可