使用 Excel VBA 重命名文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7508605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:55:31  来源:igfitidea点击:

Renaming Files with Excel VBA

excelvbarename

提问by redGREENblue

Here's what I need to do. I have these two columns in an excel sheet. With file names. First column has the current filename and the second column has the names I want the files to be renamed to. I need to use this as there's no pattern in renaming. For example, the below may be a set of files ...

这是我需要做的。我在excel表中有这两列。带文件名。第一列具有当前文件名,第二列具有我希望将文件重命名为的名称。我需要使用它,因为重命名没有模式。例如,下面可能是一组文件...

Current Name >  Rename To
---------------------------
Abc.jpg       >   Dinner.jpg

Xyz.jpg       >  Driving.jpg

123.jpg       >  Sunset.jpg

I know it should be easy to do this in VBA, but not exactly sure how. Any help would be much appreciated.

我知道在 VBA 中应该很容易做到这一点,但不完全确定如何。任何帮助将非常感激。

回答by PaulStock

I think you could do something like this, using the Namefunction to rename the files, however, you will probably need to make sure the 2 columns have the complete file path, i.e. "C:\Temp\ABC.jpg"

我想你可以做这样的事情,使用Name函数重命名文件,但是,你可能需要确保 2 列具有完整的文件路径,即“C:\Temp\ABC.jpg”

Dim Source As Range
Dim OldFile As String
Dim NewFile As String

Set Source = Cells(1, 1).CurrentRegion

For Row = 1 To Source.Rows.Count
    OldFile = ActiveSheet.Cells(Row, 1)
    NewFile = ActiveSheet.Cells(Row, 2)

    ' rename files
    Name OldFile As Newfile

Next