0

我正在处理的 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
4

2 回答 2

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 吗?

于 2013-09-23T07:30:41.533 回答
0

我认为忽略delete_junkfiles.log 文件和您的 File Not Found 错误都可以通过更改来解决

'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()无论如何在调用中使用通配符。这可能是导致找不到文件的原因,因为第一次迭代会移动所有匹配模式的文件。第二次迭代将找不到文件,因此出现错误。我添加了if以避免您想要忽略的文件。

于 2013-09-23T07:31:24.857 回答