我遇到了一個正在處理的vbscript問題。 腳本重命名,然後將一些FTP日誌文件從一個文件夾移動到另一個文件夾。 該腳本正在工作,但後來我意識到我有一個名爲delete_junkfiles.log的文件,但我不希望此文件被重命名或移動,只是保留在源文件夾中。使用VBScript進行重命名並移動日誌文件
目前腳本正在重命名並移動所有文件。 腳本也會拋出一個文件未找到錯誤,即使文件被重命名然後移動,如果可能的話,我想修復此以及....
我知道你的VBScript專家,這可能很容易,但我對vbs相當陌生,而且我無法弄清楚如何忽略delete_junkfiles.log,只是將它放在一邊。 任何幫助你的傢伙可以把我的將不勝感激。 下面是我的腳本....
Dim WshShell, FileManagement, BrowseDialogBox, SelectedFolder, OldString, NewString, FullPath, TheFolder, FileList
Dim File, ThisFile, TheString, AlreadyRenamed, TempName, FlagName, Success, FindFlag, NewName, Dummy
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FileManagement = WScript.CreateObject ("Scripting.FileSystemObject")
Set BrowseDialogBox = WScript.CreateObject("Shell.Application")
Set SelectedFolder = BrowseDialogBox.BrowseForFolder(0, "Select the folder containing the files you want to rename.", &H0001)
If InStr(1, TypeName(SelectedFolder), "Folder") = 0 Then
Wscript.Quit
Else
OldString = InputBox("Enter the characters in the filename that you want to replace","Rename Files")
If OldString = "" Then Wscript.Quit
NewString = InputBox("Enter the characters that you want to replace them with","Rename Files")
If NewString = "" Then Wscript.Quit
End If
FullPath = SelectedFolder.ParentFolder.ParseName(SelectedFolder.Title).Path
Set TheFolder = FileManagement.GetFolder(FullPath)
Set FileList = TheFolder.Files
Success = 0
ThisFile = File.Name
TheString = InStr(ThisFile, OldString)
AlreadyRenamed = InStr(ThisFile, "%")
If (TheString <> 0) AND (AlreadyRenamed = 0) Then
Success = 1
TempName = Replace(ThisFile, OldString, NewString)
FlagName = "%" + TempName
File.Name = FlagName
End If
Next
For Each File in FileList
ThisFile = File.Name
FindFlag = InStr(ThisFile, "%")
If FindFlag <> 0 Then
NewName = Replace(ThisFile, "%", "")
File.Name = NewName
End If
Next
'Move the files
For Each File in FileList
FileManagement.MoveFile "C:\Users\lislej\Desktop\test_move\*.log", "C:\Users\lislej\Desktop\test_move_to\"
Next
If Success = 1 Then
Dummy = WshShell.Popup ("Rename Files operation complete!",5,"Rename Files",64)
Else
Dummy = WshShell.Popup ("Rename Files operation failed! Please repeat the operation.",0,"Rename Files",16)
End If
Wscript.Quit
感謝@PatricK,你是對的,當我複製並粘貼代碼時,它遺漏了部分for循環....感謝您的幫助,但我認爲我需要更好地解釋我的問題 –
我已經描述了我有點更好的問題,這是回覆@Oracle上面的評論的一部分,再次感謝 –
@JohnLisle經過一番思考,我認爲'FileList'在重命名後需要重新加載?你應該可以調用'File.Move'。 – PatricK