我有一个问题,我正在研究一个vbscript。 该脚本重命名,然后将一些FTP日志文件从一个文件夹移动到另一个文件夹。 该脚本正在运行,但后来我意识到我有一个名为delete_junkfiles.log的文件,但我不希望重命名或移动此文件,只是将其保留在源文件夹中。
目前脚本正在重命名并移动所有文件。 该脚本还会抛出一个找不到文件的错误,即使文件被重命名然后移动,如果可能的话我也想修复它....
我知道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
答案 0 :(得分:0)
看起来显示的代码缺少某些内容,因为Next
没有For
。但文件移动部分是完整的,您应该检查要跳过的文件名:
Set FileList = TheFolder.Files
'Move the files
For Each File in FileList
If InStr(File, "delete_junkfiles") = 0 Then
' If "delete_junkfiles" is not find in the filename then do move file
File.Move "C:\Users\lislej\Desktop\test_move_to\"
End If
Next
更新:
我认为你需要在重命名文件后刷新FileList吗?
答案 1 :(得分:0)
我认为忽略delete_junkfiles.log
文件和您的文件未找到错误都可以通过更改来解决
'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
到
'Move the files
For Each File in FileList
if File.Name <> "delete_junkfiles.log" then
FileManagement.MoveFile File.Path, "C:\Users\lislej\Desktop\test_move_to\"
end if
Next
基本上,你正在做的是循环遍历每个文件,但无论如何在MoveFile()
调用中使用通配符。这可能导致找不到文件,因为第一次迭代会移动 all 与模式匹配的文件。第二次迭代将找不到文件,因此错误。我添加了if
以避免您要忽略的文件。