将文件重命名后移动到新文件夹

Move a file to a new folder after it has been renamed

我需要一个 VBScript 来重命名一个文件,然后将它从一个文件夹移动到另一个文件夹。该脚本目前正确地重命名了文件,但我不知道如何在重命名后将文件移动到新文件夹。

下面是现有的脚本。

Option Explicit

Const SAVE_LOCATION = "\pccit2\Int\PC\Inbox"
Const strPath       = "D:\Files\pak\VP\"
Const StrPrefix     = "VP"

Dim FSO
Dim FLD
Dim fil
Dim strOldName
Dim strNewName

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLD = FSO.GetFolder(strPath)

For Each fil In FLD.Files
    strOldName = fil.Path
    strNewName = strPath & strPrefix & Right(strOldName, 10)
    FSO.MoveFile strOldName, strNewName
Next

For Each fil In FLD.Files
    If strNewName = 1 Then
        FSO.MoveFile "\pccit2\Int\PC\Inbox"
    End If
Next

Set FLD = Nothing
Set FSO = Nothing

我尝试了多种方法来移动文件。以下是一些其他尝试:

If FSO.FileExists("D:\Files\pak\VP\*.*") Then
    FSO.MoveFile "D:\Files\pak\VP\*.*", "\pccit2\Int\PC\Inbox\*.*"
End If

再次尝试

If fil.FileExists("D:\Files\pak\VP\*.*") Then
    fil.MoveFile "D:\Files\pak\VP\*.*" , "\pccit2\Int\PC\Inbox\*.*"
End If

使用这个

dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFile "c:\myfolder\*.*","c:\anotherfolder\"
set fs=nothing

MoveFile is a method of the FileSystemObject object. It expects at least 2 arguments (source and destination), and wildcards can only be used in the source path, not in the destination path. The destination must be a file or folder path (with a trailing backslash if it's a folder). The respective method of file objects is Move,只需一个参数(目标路径)即可调用。此外,您可以一步移动和重命名文件。只需用新文件名指定目标路径即可。

For Each fil In FLD.Files
    strNewName = FSO.BuildPath(SAVE_LOCATION, strPrefix & Right(fil.Name, 10))
    fil.Move strNewName
Next

如果您想将重命名与移动分开,您可以通过简单地更改文件名称来重命名文件:

For Each fil In FLD.Files
    fil.Name = strPrefix & Right(fil.Name, 10)
    fil.Move SAVE_LOCATION & "\"
Next