使用单元格值在文件夹中查找文件,然后重命名为另一个单元格值

发布于 2025-02-03 10:14:53 字数 1053 浏览 5 评论 0原文

我在文件夹中有pdf文件(例如,c:\ myfiles”)。

在excel上,我有一个数字列表,该列中的数字列表与该文件夹中的文件名部分相关联(D列D上的单元格上的数字可以在文件名)。
在E列上,我有新的文件名,我想将其提供给具有列上数字的文件。

我需要:

  • 读取D列中的值,并在指定的 在文件名的任何部分中具有该值的文件夹。例如,
    如果D1具有“ 1234567”的数字,我想与 名称(xxxx1234567xxxxxxxx),“ x”是任何其他号码或字母。
  • 如果找到了匹配文件,请将其重命名为E列中的值 同时还要保持文件扩展名(.pdf)。
  • 仔细阅读整列直到列表结束,然后停止。
  • 如果在D列中没有特定值的匹配文件,请跳过然后转到下一个。

此代码没有错误,但不会更改任何名称。

Sub FindReplace()

Dim objFolder As Object
Dim objFile As Object
Dim i As Long
Set objFolder = CreateObject("Scripting.FileSystemObject").GetFolder("C:\MyFiles")
            
i = 1
            
For Each objFile In objFolder.Files
    If objFile.Name Like "*" & Cells(i, "D").Value & "*" Then
        objFile.Name = Cells(i, "E").Value & ".PDF"
    End If
                
    i = i + 1: If i > Cells(Rows.Count, "D").End(xlUp).Row Then Exit For
                    
Next objFile
    
End Sub

我还希望宏来使用户选择其选择的文件夹,而不是每次都必须使用相同的文件夹,但这是可选的。现在需要的是文件重命名。

I have PDF files in a folder (say, C:\MyFiles").

On Excel I have a list of numbers in column D which correlate partially to the filenames in that folder (the numbers on the cells on column D can be anywhere in the filenames).
On column E, I have new filenames I want to give to the files having the numbers on column D.

I need to:

  • Read the value in column D, and look for a file in the specified
    folder that has that value in any part of the filename. For example,
    if D1 has the number "1234567", I want to find the file with the
    name (xxxx1234567xxxxxxxxx), "x" being any other number or letter.
  • If a matching file is found, rename it to the value in column E,
    while also keeping the file extension (.pdf).
  • Read through the whole column until the end of list, then stop.
  • If no matching file for a specific value in column D, skip and go to the next one.

This code shows no error, but it doesn't change any names.

Sub FindReplace()

Dim objFolder As Object
Dim objFile As Object
Dim i As Long
Set objFolder = CreateObject("Scripting.FileSystemObject").GetFolder("C:\MyFiles")
            
i = 1
            
For Each objFile In objFolder.Files
    If objFile.Name Like "*" & Cells(i, "D").Value & "*" Then
        objFile.Name = Cells(i, "E").Value & ".PDF"
    End If
                
    i = i + 1: If i > Cells(Rows.Count, "D").End(xlUp).Row Then Exit For
                    
Next objFile
    
End Sub

I would also like the macro to make the user select a folder of their choosing, rather than having to use the same folder every time, but that is optional. What is needed right now is the file renaming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2025-02-10 10:14:53

我认为使用dir()查找部分匹配有点容易:

Sub FindReplace()

    Dim fPath As String, f, c As Range, ws As Worksheet
    Dim i As Long
    
    fPath = GetFolderPath("Select a folder for file renaming")
    If Len(fPath) = 0 Then Exit Sub 'no folder selected
    
    Set ws = ActiveSheet 'or some specific sheet
    For Each c In ws.Range("D2:D" & ws.Cells(Rows.Count, "D").End(xlUp).row).Cells
        If Len(c.Value) > 0 Then
            f = Dir(fPath & "*" & c.Value & "*.pdf", vbNormal)
            If Len(f) > 0 Then 'found a match?
                Name fPath & f As fPath & c.Offset(0, 1).Value & ".pdf"
            End If
        End If
    Next
       
End Sub

'get a folder from the user - returns empty string if no selection
Function GetFolderPath(msg As String) As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = msg
        If .Show = -1 Then GetFolderPath = .SelectedItems.Item(1) & "\"
    End With
End Function

It's a little easier I think to use Dir() to find partial matches:

Sub FindReplace()

    Dim fPath As String, f, c As Range, ws As Worksheet
    Dim i As Long
    
    fPath = GetFolderPath("Select a folder for file renaming")
    If Len(fPath) = 0 Then Exit Sub 'no folder selected
    
    Set ws = ActiveSheet 'or some specific sheet
    For Each c In ws.Range("D2:D" & ws.Cells(Rows.Count, "D").End(xlUp).row).Cells
        If Len(c.Value) > 0 Then
            f = Dir(fPath & "*" & c.Value & "*.pdf", vbNormal)
            If Len(f) > 0 Then 'found a match?
                Name fPath & f As fPath & c.Offset(0, 1).Value & ".pdf"
            End If
        End If
    Next
       
End Sub

'get a folder from the user - returns empty string if no selection
Function GetFolderPath(msg As String) As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = msg
        If .Show = -1 Then GetFolderPath = .SelectedItems.Item(1) & "\"
    End With
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文