16

我编写了一个 vbscript,用于将文件从源目录移动到目标目录。目前脚本的工作方式是我有一个读入的映射文件(将 id 映射到文件夹类型)。每个被移动的文件都以 id 开头,目的地将基于 id 映射到的内容。我读入映射文件并为每个要移动的文件建立目标路径。这一切都按预期工作,问题是当我尝试移动目标目录中已经存在的文件时,文件不会从源目录中移动。本质上,如果目标目录中的文件已经存在,我希望它覆盖它。目前,我的主要命令是:

fso.MoveFile ObjFile.Path, archiveTo & "\" & yearValue & "\" & monthValue & "\" & ObjFile.Name

如果目标目录中的文件已经存在,有没有办法默认它总是覆盖它?

4

3 回答 3

33

不幸的是,VBScriptMoveFile方法仅在目标文件不存在时才有效。当存在时它不能覆盖这样的文件,只是抛出错误。

所以唯一的选择是使用CopyFile(它确实有覆盖选项)然后DeleteFile:

fso.CopyFile ObjFile.Path, archiveTo & "\" & yearValue & "\" & monthValue & "\" & ObjFile.Name, True
fso.DeleteFile ObjFile.Path
于 2013-06-12T12:39:03.247 回答
1

如前所述,MoveFile 无法覆盖现有文件。但是您可以创建自己的函数:

Function MoveFile(source, target)
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")

  fso.CopyFile source, target, True
  fso.DeleteFile source
End Function

然后这样称呼它:

MoveFile ObjFile.Path, archiveTo & "\" & yearValue & "\" & monthValue & "\" & ObjFile.Name
于 2018-10-01T13:26:35.553 回答
1

上面的示例看起来不错,但存在源路径和目标路径可能相同的风险。然后文件会覆盖自身,实际上似乎没有复制任何内容。所以如果你在那之后删除源文件,那么文件可能会丢失!所以我使用的是下面的功能(我认为它更安全):

Public Function GrMoveFile(ByVal sMoveFrom As String, ByVal sMoveTo As String, Optional ByVal fOverride As Boolean = False) As Variant

' This function allows moving file between different drives or servers with possible overriding
' author: Tomasz Kubiak
' t.kubiak@engineer.com
'
' RETURNS:
'   - true (if moving successfull)
'   - error description (otherwise)
' ARGUMENTS:
'   - sMoveFrom - source file path
'   - sMoveTo - destination file path
'   - fOverride - allow for overriding (false by default)

    Dim FSO As New Scripting.FileSystemObject   ' File system object - requires reference to Microsoft Scripting Library (Tools -> References)
    Dim OrigFileAttr As VbFileAttribute         ' Holds attribute of the destination file

On Error GoTo EH

    ' if overriding is allowed:
    If fOverride Then

        ' It's necessary to prevent the destination file from deleting,
        ' in case of the source path and destination path points to the same file
        ' (it's possible e.g. when the network location is mapped as a drive).
        ' So the solution is to lock the file by setting fileattribute to ReadOnly

        ' Before locking file let's remember the original state of the destination file
        OrigFileAttr = GetAttr(sMoveFrom)

        ' Unlock file before copy
        SetAttr sMoveFrom, vbNormal

        ' Original FSO MoveFile method does not allow overriding, so we copy the file at first
        FSO.CopyFile source:=sMoveFrom, destination:=sMoveTo, overwritefiles:=True

        ' Set destination file attribute to read-only to prevent deletion
        SetAttr sMoveTo, vbReadOnly

On Error Resume Next
        ' Theoretically the condition below should not be required, because FSO.delete method with
        ' attribut "force" set to false shouldn't allow for deleting "Read-only files".
        ' But in practice, when you have a file located on the server location, it appeared to not
        ' work properly and delete also RO-files, so i've introduced that condition
        If GetAttr(sMoveFrom) <> vbReadOnly Then
            ' Try to delete source file
            FSO.DeleteFile sMoveFrom, False
        End If

        'restore previous file attribute
        SetAttr sMoveTo, OrigFileAttr
On Error GoTo EH

    ' if overriding is NOT allowed:
    Else
        'move using regular move method (does not allow override)
        FSO.MoveFile source:=sMoveFrom, destination:=sMoveTo
    End If

    'pReleaseFolder

    ' Moving succesfull, let function return true
    GrMoveFile = True
    Exit Function

'Error handler
EH:
    'pReleaseFolder

    ' An error occured, return error description
    GrMoveFile = Err.Description
End Function

于 2019-10-25T12:44:18.543 回答