我有代码从A列中找到文件名,并像在源文件夹中的B列中那样重命名文件,然后将其复制到新文件夹中。
代码如下。
Sub Rename_Files()
Dim SourcePath, DestPath, Fname, NewFName
SourcePath = "C:\Invoices\"
DestPath = "C:\Invoices\Renamed\"
For i = 1 To 100
Fname = Range("A" & i).Value
NewFName = Range("B" & i).Value
If Not Dir(SourcePath & Fname, vbDirectory) = vbNullString Then
FileCopy SourcePath & Fname, DestPath & NewFName
Else
MsgBox (Fname & " Not Exists in Folder")
End If
Next i
End Sub
问题在于,源目录中的文件名很长,'INVOICEDUMP_OFND_4294819_ABC Corp.pdf'
而且像这样数百个。
我想找到名称中包含4294819(来自A列)的文件,然后仅用名称替换'INV 4294819.pdf'
(如B列中所述)。
谢谢
除非我的DOS技能非常生锈,否则您应该可以使用
Sub Rename_Files()
Dim SourcePath As String, DestPath As String, Fname As String, NewFName As String
Dim i As Long
SourcePath = "C:\Invoices\"
DestPath = "C:\Invoices\Renamed\"
For i = 1 To 100
If Not IsEmpty(Range("A" & i).Value) Then
NewFName = Range("B" & i).Value
'Search for the first file containing the string in column A
Fname = Dir(SourcePath & "*" & Range("A" & i).Value & "*")
If Fname <> vbNullString Then
FileCopy SourcePath & Fname, DestPath & NewFName
Else
MsgBox Range("A" & i).Value & " Not Exists in Folder"
End If
End If
Next i
End Sub
假设A列具有诸如的条目,4294819
并且B列中的对应条目类似于INV 4294819.pdf
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句