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

问题描述:

我需要一个重命名文件并将其从一个文件夹移动到另一个文件夹的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 

MoveFileFileSystemObject对象的一个​​方法。它期望至少有两个参数(源和目标),并且通配符只能用于目标路径中的源路径而不是。目标必须是文件或文件夹路径(如果是文件夹,则使用尾部反斜杠)。文件对象的相应方法是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 
+0

谢谢!这非常有用,我会参考你的回应。你提供的第一个脚本完美地工作。只有修改是在VP中添加一个前缀。我在这里添加了它。 strNewName = FSO.BuildPath(SAVE_LOCATION,**“VP”**&Right(fil.Name,10)) – jodies

使用此

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

我已经修改了剧本,你所指出的,现在我得到这个错误“变量未定义:'服务器'“为这一行: 设置fs = Server.CreateObject(”Scripting.FileSystemObject“) – jodies

+0

我不认为他使用ASP。是什么给了你这个想法?在普通的VBScript中,你只需使用'Set fs = CreateObject(“Scripting.FileSystemObject”)'。 –