为什么会出现运行时错误58-在VBA中复制文件时文件已经存在?

jhgreen965

我正在尝试使用此代码在VBA中复制文件

Sub move_data()

Dim FSO As Object
Dim FileInFromFolder As Object

Dim FromPath As String
Dim ToPath As String
Dim ws As Worksheet:

Set FSO = CreateObject("scripting.filesystemobject")
Set ws = ThisWorkbook.Sheets("Sheet1")

FromPath = FSO.GetFolder(ws.Range("E1").Value)
ToPath = FSO.GetFolder(ws.Range("E3").Value)

For Each FileInFromFolder In FSO.GetFolder(FromPath).Files
FileInFromFolder.Move ToPath
Next FileInFromFolder

End Sub

我正进入(状态

运行时错误58-文件已存在

尽管该文件中没有文件 ToPath

如果我直接引用文件夹而不是引用Sheet1的单元格E1和E3中的文件夹名称,则该代码有效。

VBasic2008

移动所有文件

上的Microsoft文档Move

备注
在文件或文件夹执行Move方法的结果使用FileSystemObject.MoveFile或FileSystemObject.MoveFolder执行的操作相同但是,您应该注意,其他方法也可以移动多个文件或文件夹。

上的Microsoft文档MoveFile

备注
如果source包含通配符,或者destination以路径分隔符结尾,则假定destination指定了一个现有文件夹,可在其中移动匹配的文件。否则,假定destination是要创建的目标文件的名称。在这两种情况下,移动单个文件都可能发生三件事:

  • 如果目标不存在,则文件将被移动。这是通常的情况。
  • 如果目标是现有文件,则会发生错误。
  • 如果目标是目录,则会发生错误。

调查
fso.MoveFile

  • 乍一看似乎fso.MoveFile是要走的路,但这并不是那么简单。
  • 第一个问题是,如果源中没有文件,则会发生错误:用解决If .GetFolder(FromPath).Files.Count > 0 Then
  • 第二个(更糟糕的)问题是,如果目标中已存在文件,则无法确定在发生错误之前将复制的内容:未解决。
  • 当然,如果您知道目标为空,或者至少没有任何现有文件,那么这是更有效的方法。但是我不会打赌。
  • FromPath必须紧跟在PathSeparator且至少要加上一个*,而ToPath必须以结束PathSeparator

fso.File.Move

  • fso.File.Move解决方案必须通过文件循环。但是您无需检查文件计数,可以实现If Not .FileExists(ToPath & fsoFile.Name) Then检查目标中是否存在相同名称的文件。
  • ToPath必须以结尾PathSeparator,但可以选择后面跟文件名(fsoFile.Name)。

编码

Option Explicit

Sub moveAllFilesFSO()
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
    Dim FromPath As String: FromPath = ws.Range("E1").Value
    Dim ToPath As String: ToPath = ws.Range("E3").Value
    
    With CreateObject("Scripting.FileSystemObject")
        If .FolderExists(FromPath) And .FolderExists(ToPath) Then
            If .GetFolder(FromPath).Files.Count > 0 Then
                Dim Sep As String: Sep = Application.PathSeparator
                If Right(FromPath, 1) <> Sep Then
                    FromPath = FromPath & Sep
                End If
                FromPath = FromPath & "*" ' "*.*"
                If Right(ToPath, 1) <> Sep Then
                    ToPath = ToPath & Sep
                End If
                .MoveFile FromPath, ToPath
            Else
                Debug.Print "No files found in " & "'" & FromPath & "'."
            End If
        Else
            Debug.Print "At least one of the folders does not exist."
        End If
    End With

End Sub

Sub moveAllFilesFile()
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
    Dim FromPath As String: FromPath = ws.Range("E1").Value
    Dim ToPath As String: ToPath = ws.Range("E3").Value
    
    With CreateObject("Scripting.FileSystemObject")
        If .FolderExists(FromPath) And .FolderExists(ToPath) Then
            ToPath = .GetFolder(ToPath) & Application.PathSeparator
            Dim fsoFile As Object
            For Each fsoFile In .GetFolder(FromPath).Files
                If Not .FileExists(ToPath & fsoFile.Name) Then
                    fsoFile.Move ToPath ' & fsoFile.Name
                'Else
                    ' File already exists.
                End If
            Next fsoFile
        Else
            Debug.Print "At least one of the folders does not exist."
        End If
    End With

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Word- VBA-为什么会出现运行时错误451“属性让过程未定义且属性获取过程未返回对象”?

从 MS Word 文档中读取单选按钮值时,为什么会出现“运行时错误‘91’:对象变量或块变量未设置”?

接收到的运行时错误58文件已经存在

为什么在读取文件时出现运行时错误

在函数参数中,为什么 (i++) 在 (i+1) 运行时出现错误

运行此文件时,为什么会出现doctest错误?

vba 网页抓取 - 即使设置了对象变量,也会出现运行时错误 91

为什么在IntelliJ中运行Scala 2.13测试而不是在Scala 2.12中运行时出现此错误?

C++ 中的堆栈:如果切换“或”条件,则会出现运行时错误

即使整个代码中没有除法,hackerrank 也会出现运行时错误

在 C 中访问越界数组不会出现运行时错误

为什么 bfs c ++ 中的运行时错误

如果文件已经存在,则在Windows中复制文件时为文件创建的日期错误

为什么会出现运行时错误?

为什么会出现访问冲突运行时错误?

为什么我在 PC^2 上出现运行时错误?(替换某些字符串中的某些单词或字符)

当我可以在MySQL中成功执行时,为什么会出现sql语法错误

仅在脚本中运行时才会出现“查找:路径必须在表达式之前:”错误

为什么在 VBA 中运行以下公式时会出现“类型不匹配”错误?

为什么在Rust中处理自定义错误类型时会出现运行错误?

在Hive中处理行时出现Hive运行时错误

为什么在运行时找不到python显示文件错误?

运行“粘贴”时,出现VBA运行时错误438

打开Word文件时出现运行时错误'1004'

在 VBA 中执行 SQL 时出现运行时错误

运行时错误“3075”VBA:检查表中是否已存在文件

当它在Cloud Shell中成功运行时,为什么我的终端上的gcloud出现“获取凭据需要编辑权限”错误?

为什么即使未运行fsck,磁盘错误也会出现在dmesg输出中?

为什么在Python + Seleniuim中运行Javascript会出现未定义的错误?