Do a VBScript validation for a file with a specific word in it filename and then locate and delete that file

I was wondering if there is a way to make it so that my vbs script can check and delete any files with a specific word in it. 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 a way to make the username / file name path on any computer? I know what is it

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

      

in batch mode, but is there something similar in vbscript? Is there a way to delete files with ANY extension that has "example" in the name? For example:

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

      

Many thanks! I hope you don’t mind my huge number of questions, I am just starting to coding VBS / VBScript and really appreciate your help! :)

+3


source to share


1 answer


ExpandEnvironmentStrings

returns the value of the extended environment variable. Environment variable names that 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 subsequent code snippets pasted here unchanged from related sources. Can be a starting point for any beginner VBScript

. Breathe yourself in the following huge repository of script: Script resources for IT pros .


Stolen from Searching for files using a wildcard query . Uses a keyword Like

to find all files on a computer that contains a tilde ( ~

). However, read the CIM_DataFile

class
on MSDN:



The following code example VBS

describes how to perform a standard wildcard search on a data file. Note that backslash delimiters must escape with another backslash ( \\

). Also, when used CIM_DataFile.FileName

in a sentence WHERE

, the WMIPRVSE

process scans all directories on any available storage device. This may take a while, especially if you have configured remote resources, and may trigger anti-virus alerts.

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 do I delete certain files in a specific folder? For more information, see the "Hello, Screenwriter!" blog (about ten years):

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 scripts WMI

, this one can also be run on a remote computer.

+1


source







All Articles