【问题标题】:Issue with VBScript to rename and move log filesVBScript 重命名和移动日志文件的问题
【发布时间】:2013-09-22 14:50:13
【问题描述】:

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

【问题讨论】:

    标签: windows vbscript


    【解决方案1】:

    显示的代码似乎缺少某些内容,因为有一个 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?

    【讨论】:

    • 谢谢@PatricK,你说得对,当我复制并粘贴代码时,它遗漏了 for 循环的一部分......谢谢你的帮助,但我想我需要解释我的问题好一点
    • 我已经描述了我遇到的问题好一点,它是上面回复@Oracle的cmets的一部分,再次感谢
    • @JohnLisle 经过一番思考,我认为FileList 需要在重命名后重新加载?你应该可以打电话给File.Move
    • 你是一个传奇@PatricK,感谢一堆伙伴,这成功了,虽然它仍然重命名了 delete_junkfiles.log,但它并没有被移动,这主要是我所关心的,我可以解决重命名问题,现在你已经指出它看起来很明显了哈哈,再次感谢
    【解决方案2】:

    我认为忽略 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 以避免您想要忽略的文件。

    【讨论】:

    • 感谢@oracle 认证专业人士,我尝试将您添加到我的代码中,它已修复我的文件未找到问题,谢谢,但它仍然重命名和移动所有内容。
    • 感谢 @oracle 我尝试将您添加到我的代码中,它已修复我的文件未找到问题,谢谢,但它仍然重命名并移动所有内容。我想我需要澄清一点,基本上我有一个文件夹,其中包含名为 FTPPub1_aaplog.ftp.log、FTPPub1_netdesklog.ftp.log、TPurge.log、TPurge_Others.log 和 delete_junkfiles.log 的文件,除 delete_junkfiles.log 之外的所有文件都应该得到重命名然后移动,然后使用原始文件名重新生成日志文件,delete_junkfiles 必须保持不变。我知道有一种简单的方法可以做到这一点,但我认为我离问题太近了哈哈
    猜你喜欢
    • 2011-09-10
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    相关资源
    最近更新 更多