FileCopy orgPath & "*", newPath 这里报错,请重写
时间: 2024-03-19 21:46:11 浏览: 129
ffc.zip_FileCopy_NIO_java nio
如果 FileCopy orgPath & "*", newPath 报错,可以尝试使用 Dir 函数来获取原始文件夹路径中所有的文件名,然后逐个将其复制到新文件夹路径中。修改后的代码如下:
```
Sub RenameFiles()
Dim orgPath As String, newPath As String, renameSheet As Worksheet
Dim fileCell As Range, renameCell As Range, newName As String
Dim fileName As String, fileExtension As String
'设置文件夹路径和工作表
orgPath = "C:\Users\DELL\Desktop\ZTE FILES\ORG_FILES\"
newPath = "C:\Users\DELL\Desktop\ZTE FILES\NEW_FILES\"
Set renameSheet = ThisWorkbook.Sheets("ZTE RENAME")
'清空工作簿C2:C列
renameSheet.Range("C2:C" & renameSheet.Cells(Rows.Count, "C").End(xlUp).Row).ClearContents
'查找文件夹里的所有文件名
fileName = Dir(orgPath & "*")
Do While fileName <> ""
'获取文件名和文件扩展名
fileExtension = Right(fileName, Len(fileName) - InStrRev(fileName, "."))
fileName = Left(fileName, InStrRev(fileName, ".") - 1)
'将文件名放入工作表的 C 列中
Set fileCell = renameSheet.Range("C:C").Find(fileName, LookIn:=xlValues)
If Not fileCell Is Nothing Then
fileCell.Value = fileName & "." & fileExtension
End If
'复制文件到新文件夹路径中
FileCopy orgPath & fileName & "." & fileExtension, newPath & fileName & "." & fileExtension
'查找下一个文件名
fileName = Dir
Loop
'将文件名按文件夹排序放入工作表C2:C列
renameSheet.Range("C2:C" & renameSheet.Cells(Rows.Count, "C").End(xlUp).Row).Sort key1:=renameSheet.Range("C2"), order1:=xlAscending, Header:=xlNo
'重命名文件
For Each renameCell In renameSheet.Range("C2:C" & renameSheet.Cells(Rows.Count, "C").End(xlUp).Row)
If renameCell.Value <> "" Then
newName = renameSheet.Range("H" & renameCell.Row).Value
Name newPath & renameCell.Value As newPath & newName
End If
Next renameCell
End Sub
```
修改后的代码中使用了 Do While 循环和 Dir 函数来实现文件的复制,同时也对获取文件名和文件扩展名的方式进行了修改。
阅读全文