2016-03-04 206 views
1

我需要一個重命名文件並將其從一個文件夾移動到另一個文件夾的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 

回答

1

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

0

使用此

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」)'。 –