使VBScript检查文件名中带有特定单词的文件,然后查找并删除该文件 [英] Make VBScript check for a file with a certain word in it's file name and then find and delete that file
问题描述
我想知道是否有一种方法可以使我的vbs脚本可以检查并删除名称中带有特定单词的任何文件.这是我到目前为止的内容:
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"
批处理,但是vbscript中有类似的东西吗?还有一种方法可以删除名称中带有"example"的带有ANY扩展名的文件吗?例如:
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"
非常感谢!希望您不要介意我的大量问题,我刚刚开始使用VBS/VBScript进行编码,非常感谢您的帮助! :)
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! :)
推荐答案
ExpandEnvironmentStrings
方法返回环境变量的扩展值.必须用%
字符括起来的环境变量名称不区分大小写:
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"
从链接源粘贴到此处的所有下一个代码片段均保持不变.可以成为任何VBScript
初学者的起点.在下一个庞大的脚本存储库中激发自己的灵感: IT专业人员的脚本资源.
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.
从Like
关键字搜索包含代字号(~
)的计算机上的所有文件.但是,请阅读 CIM_DataFile
类 MSDN:
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:
下面的
VBS
代码示例描述了如何执行标准 在数据文件上进行通配符搜索.请注意,反斜杠定界符必须 用另一个反斜杠(\\
)进行转义.另外,当使用WHERE
子句中的"CIM_DataFile.FileName
",WMIPRVSE
进程将扫描任何可用存储设备上的所有目录. 这可能需要一些时间,尤其是在您映射了远程共享的情况下, 并可以触发防病毒警告.
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 theWHERE
clause, theWMIPRVSE
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
当然,与大多数WMI
脚本一样,该脚本也可以在远程计算机上运行.
Of course, like most WMI
scripts, this one can also be run against a remote computer.
这篇关于使VBScript检查文件名中带有特定单词的文件,然后查找并删除该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!