用VB批量重命名文件,如何实现?

2024-11-18 03:18:20
推荐回答(3个)
回答1:

方法一:Dim fpPrivate Sub refilename(folderspec)Dim fs, f, f1, fc, sm, hzSet fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)Set fc = f.FilesFor Each f1 In fc
hz = Mid(f1.Name, InStrRev(f1.Name, "."))
Name f1 As fp & "20101007-" & hz
NextMsgBox "修改完成"End SubPrivate Sub Command1_Click()fp = "E:\emot\xhh\"'这里设置文件夹路径,根的需要来修改
If Right(fp, 1) <> "\" And Right(fp, 1) <> "/"
Then fp = fp & "\"Call refilename(fp)
’调用上面的自定义函数,改名End Sub........

方法二: '将f:\123\文件下的txt文件改名
Dim files() As String '用于贮存文件名
Dim length As Integer = 0 'files数组长度
Dim fileName As String = Dir$("f:\123\*.txt") '得到第一个.txt文件的文件名
'Dim g As Graphics = Me.CreateGraphics
Do While fileName <> ""
'g.DrawString(length, Font, Brushes.Red, length * 10, length * 10)
ReDim Preserve files(length)
files(length) = fileName
length = files.Length
fileName = Dir$() '得到下一个.txt文件的文件名
Loop
For i = 0 To length - 1
Dim oldfileName As String = "f:\123\" + files(i)
Dim newfileName As String = "f:\123\" + "20101007_"+ files(i)
Rename(oldfileName, newfileName) '修改文件名
Next

回答2:

Replace(a,b,c)
在a中寻找b 替换成c

private Sub Command1_Click()
Dim oldName
Dim NewName As String, i As Integer, a As Integer
oldName = Array("z:\1.txt", "z:\2.txt")
For i = 0 To UBound(oldName)
a = InStr(1, oldName(i), "\")
NewName = Replace(oldName(i), Mid(oldName(i), 1, a), Mid(oldName(i), 1, a) & Chr(97 + i))
Name oldName(i) As NewName
Next
End Sub

自己再加工一下吧

回答3:

思路一:
Dim fp
Private Sub refilename(folderspec)
Dim fs, f, f1, fc, sm, hz
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
hz = Mid(f1.Name, InStrRev(f1.Name, "."))
Name f1 As fp & "20101007-" & hz
Next
MsgBox "修改完成"
End Sub

Private Sub Command1_Click()
fp = "E:\emot\xhh\"'这里设置文件夹路径,根据你的需要来修改
If Right(fp, 1) <> "\" And Right(fp, 1) <> "/" Then fp = fp & "\"
Call refilename(fp)’调用上面的自定义函数,改名
End Sub
........
思路二:
'将f:\123\文件下的txt文件改名

Dim files() As String '用于贮存文件名
Dim length As Integer = 0 'files数组长度
Dim fileName As String = Dir$("f:\123\*.txt") '得到第一个.txt文件的文件名
'Dim g As Graphics = Me.CreateGraphics

Do While fileName <> ""
'g.DrawString(length, Font, Brushes.Red, length * 10, length * 10)
ReDim Preserve files(length)
files(length) = fileName
length = files.Length
fileName = Dir$() '得到下一个.txt文件的文件名
Loop

For i = 0 To length - 1
Dim oldfileName As String = "f:\123\" + files(i)
Dim newfileName As String = "f:\123\" + "20101007_"+ files(i)
Rename(oldfileName, newfileName) '修改文件名
Next

..........
仅供参考。........黑龙江省..张志晨.........