3

I was wondering if there is a way to make it so my vbs script can check for and delete any files with a certain word in it's name. This is what I have so far:

x=MsgBox ("Searching for any infected files...",64,"Search") 

DIM filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("C:\Documents and Settings\Name\Desktop\example.txt") Then
    WScript.Sleep 1500
    x=MsgBox ("Warning! A infected file was found!",48,"Warning") 
    filesys.DeleteFile "C:\Documents and Settings\Name\Desktop\example.txt"
    x=MsgBox ("File was deleted!",48,"File Deleted")
    WScript.Sleep 1000
    x=MsgBox ("This Computer is now clean!",64,"Hooray!") 
Else 
    WScript.Sleep 500
    x=MsgBox ("File not found! This Computer is clean!",64,"Hooray!")
End If

Is there also a way to make the username/file path work on any computer? I know it is

"C:\Documents and Settings\%username%\Desktop\example.txt"

in batch, but is there something like that in vbscript? Is there also a way to delete files with ANY extension that have 'example' in the name as well? For example:

filesys.DeleteFile "C:\Documents and Settings\Name\Desktop\example.anyextension"

Thanks so much! I hope you don't mind my huge amount of questions, I am just starting to code with VBS/VBScript and really appreciate your help! :)

  • I guess that you want writing something like a cleaner , so you can get inspired by this ==> http://stackoverflow.com/questions/23832414/how-to-move-file-while-renaming-it-with-incrementation-if-the-file-exists-in-vbs – Hackoo May 09 '15 at 09:18

1 Answers1

1

ExpandEnvironmentStrings method returns an environment variable's expanded value. Environment variable names, which must be enclosed between % characters, are not case-sensitive:

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "Current user name: " _
    & WshShell.ExpandEnvironmentStrings("%USERNAME%")
WScript.Echo "Desktop folder: " _
    & WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop"

All next code snippets pasted here unchanged from linked sources. Could become a starting point for any VBScript beginner. Inspire yourself at next huge script repository: Script resources for IT professionals.


Stolen from Search for Files Using a Wildcard Query. Uses the Like keyword to search for all files on a computer that contain a tilde (~). However, read CIM_DataFile class at MSDN:

The following VBS code sample describes how to perform a standard wildcard search on a datafile. Note that the backslash delimiters must be escaped with another backslash (\\). Also, when using "CIM_DataFile.FileName" in the WHERE clause, the WMIPRVSE process will scan all directories on any available storage device. This may take some time, especially if you have mapped remote shares, and can trigger antivirus warnings.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2") 

Set colFiles = objWMIService.ExecQuery _ 
    ("Select * from CIM_DataFile where FileName Like '%~%'") 

For Each objFile in colFiles 
    Wscript.Echo objFile.Name 
Next 

Here's a smarter and faster solution with comprehensive comments: How Can I Delete Specific Files in a Specific Folder? at Hey, Scripting Guy! blog (next to ten years old):

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='T:\Act'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In colFileList
    If InStr(objFile.FileName, "current") Then
        objFile.Delete
    End If
Next

Of course, like most WMI scripts, this one can also be run against a remote computer.

JosefZ
  • 28,460
  • 5
  • 44
  • 83