尝试重命名文件夹中的文件(基于文件名内的string)

您好,我正在尝试创build一个Sub,我在文件夹中find名称中包含特定短语(如“test_file”)的文件,并用不同的名称/扩展名对其进行重命名。 我有下面的代码,似乎并不工作:(也我不知道如何search一个文件名中的特定string,并执行重命名根据该)

Sub ReName() Dim myFile As String Dim myPath As String Dim mVal As String mVal = "_12345678_" 'test string' myPath = "C:\Users\bf91955\Desktop\testfolder\" 'folder path' myFile = Dir(pathname & "*test_file*") 'real file name is 2222_test_test_file.xlsx' myFile = myPath & myFile NewName = "new_test_file & mVal & .xlsx" 'save it as .xlsx and with the new filename including myVal' Name myFile As myPath & NewName 'rename End Sub 

任何帮助,将不胜感激!

看看你有几个错误的意见

 Sub ReName() Dim myFile As String Dim myPath As String Dim mVal As String mVal = "_12345678_" 'test string' myPath = "C:\Users\bf91955\Desktop\testfolder\" 'folder path' '' You weren't referencing MyPath here - guessing copy and paste error myFile = Dir(myPath & "*test_file*") 'real file name is 2222_test_test_file.xlsx' myFile = myPath & myFile '' This was passing the variable as part of the string newname = "new_test_file" & mVal & ".xlsx" 'save it as .xlsx and with the new filename including myVal' Name myFile As myPath & newname 'rename End Sub