VB 删除某些文件的脚本,如果找到文件,则将其他文件复制到目录

VB Script to delete certain files and if files are found copy other files to directory

我的硬盘感染了病毒。该病毒加密文件,然后要求赎金来解密它们。这些文件是 HELP_DECRYPT.HTML、HELP_DECRYPT.PNG、HELP_DECRYPT.TXT 和 HELP_DECRYPT.URL。

驱动器上有数千个受感染的文件。我正在尝试编写一个脚本来遍历驱动器上的所有文件夹,如果它发现任何恶意文件,就会将其删除。然后我想是否从同一目录中的备份驱动器复制文件,即。如果在 I\Folder\ 中找到,如果将从 F\Folder\ 中获取文件。

在我的例子中,受感染的驱动器是 Y,备份驱动器是 X。

我对 VBScripts 比较陌生,这是我目前所掌握的:

    set fso = CreateObject("Scripting.FileSystemObject")
ShowSubFolders FSO.GetFolder("Y:\"), 3

Sub ShowSubFolders(Folder, Depth)
    If Depth > 0 then
        For Each Subfolder in Folder.SubFolders
            'Wscript.Echo Subfolder.Path
    DeleteFiles(subFolder.path)
  On Error Resume Next
            ShowSubFolders Subfolder, Depth -1 

        Next
    End if
End Sub


  'deletes the malicious files and calls the copy function'
Function DeleteFiles(path)
'wscript.echo("in delete method")
  set FSO2 = Createobject("Scripting.FileSystemObject")
  set ofolder = createobject("Scripting.FileSystemObject")
  set ofolder = FSO2.GetFolder(path)

  if FSO2.FileExists("HELP_DECRYPT.URL") Then
    ofolder.DeleteFile("HELP_DECRYPT.PNG")
    ofolder.DeleteFile("HELP_DECRYPT.HTML")
    ofolder.DeleteFile("HELP_DECRYPT.URL")
    ofolder.DeleteFile("HELP_DECRYPT.TXT")      
      wscript.echo("DeletedFiles")
  copyFiles(FSO.GetParentFolder) 
    end if

End Function


  'copies files from the backup'
Function CopyFiles(from)
    dim to1 'where we're copying to
    to1=from 'where we're copying from
    Call Replace (from, "Y:", "X:")
    SET FSO3 = CreateObject("Scripting.FileSystemObject")
    For Each file In from 'not sure about "file"
        FSO3 = file
        Call FSO3.CopyFile (from, to1, true)'copies file and overwrites if already there
    Next
End Function

这是我要使用的:

Option Explicit

Dim FSO, badFiles
Set FSO = CreateObject("Scripting.FileSystemObject")
badFiles = Array("HELP_DECRYPT.PNG", "HELP_DECRYPT.URL", "HELP_DECRYPT.HTML", "HELP_DECRYPT.TXT")

Walk FSO.GetFolder("Y:\")

Sub Walk(folder)
  Dim subFolder
  For Each subFolder in folder.SubFolders
    DeleteFiles subFolder, badFiles
    RestoreFiles "X:", subFolder
    Walk subFolder
  Next
End Sub

Sub DeleteFiles(folder, filesToDelete)
  Dim file
  For Each file In filesToDelete
    file = FSO.BuildPath(folder.Path, file)
    If FSO.FileExists(file) Then FSO.DeleteFile file, True
  Next
End Sub

Sub RestoreFiles(sourceRoot, destinationFolder)
  Dim sourcePath, file
  WScript.Echo "Restoring " & destinationFolder.Path & " ..."
  sourcePath = Replace(destinationFolder.Path, destinationFolder.Drive, sourceRoot)
  If FSO.FolderExists(sourcePath) Then
    For Each file In FSO.GetFolder(sourcePath).Files
      WScript.Echo file.Name
      ' maybe add a DateLastModified check here?
      file.Copy FSO.BuildPath(destinationFolder.Path, file.Name), True
    Next
  Else
    WScript.Echo "Warning! Folder not found: " & sourcePath
  End If
End Sub

使用 VBScript 的一般提示:

  • 始终使用Option Explicit
  • 避免On Error Resume Next,除非在非常非常狭窄的情况下。简单地抑制任何错误绝不是一个好主意。
  • 运行 在命令行上使用 cscript.exe 脚本,这样您就可以看到脚本的 Echo 输出,而无需单击 1000 个消息框。
  • 使用全局 FSO 对象。无需在每个函数中定义一个新的
  • 尽量通用。看看上面的 DeleteFiles() RestoreFiles() 实际上根本不适合您当前的问题。您或许可以在不同的脚本中重新使用这些函数,而无需更改它们。