查询以选择所有非必需的临时文件 [英] Query to Select All Non-Essential Temporary Files

查看:103
本文介绍了查询以选择所有非必需的临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来删除不需要的临时文件.不过,我想专门针对.tmp.至少现在(是.因此,我试图编写一个WQL查询以返回一个集合,我可以使用它使用FOR EACH语句删除C:\ Users \\ AppData \ Local \ Temp中的所有.tmp.我只是最近才开始学习VBScript.但是我有使用C/C ++编写程序的经验(主要是"math-y"程序).

I am trying to write a script to delete unneeded temporary files. I am wanting to specifically target .tmp's, though. At least for now. So I am trying to write a WQL query to return a collection with which I can use a FOR EACH statement to delete all of the .tmp's in C:\Users\\AppData\Local\Temp. I've only recently started learning VBScript. But I have experience writing programs in C/C++ (mainly "math-y" programs).

Cscript查询本身似乎没有问题.但是,当我尝试在结果集合上使用Count方法时,cscript返回错误:(17,1)Microsoft VBVScript运行时错误:对象不支持此属性或方法:'colTempFiles.Count'.

Cscript seems to have no problem with the query itself. But when I try to use the Count method on the resulting collection, cscript returns an error: (17,1) Microsoft VBVScript runtime error: Object doesn't support this property or method: 'colTempFiles.Count'.

我已经阅读了一些有关WQL的内容,以为也许由于某种原因我没有得到任何收藏.但我似乎找不到任何错误的查询.我在想,也许我不应该从FileSystemObject中选择.但是我已经阅读了有关此内容的资料,这似乎是正确的做法(尽管MSDN上确实没有很多有用的信息).

I've read up on WQL a little bit, thinking that maybe I'm not getting a collection returned for some reason. But I can't seem to find anything wrong with the query. I'm thinking that maybe I shouldn't be selecting from FileSystemObject. But I've read what I can find about it, and it seems to be the right thing to do (although there really isn't a lot of helpful info on MSDN).

无论如何,这是我当前拥有的脚本,没有注释.第二行是我当前未使用的东西,但是稍后将尝试使用,因此我可以将变量定义为本地计算机的用户名,而不必专门指向本地Temp文件夹的路径.任何帮助将不胜感激:

Anyway, here's the script I currently have, without comments. The second line is something I am not currently using, but am going to try to use later, so that I can define a variable as the local computer's username and not have to point to the local Temp folder's path specifically. Any help would be greatly appreciated:

strComputer = "."
strUser="adam"


Set objFSO=CreateObject("Scripting.FileSystemObject")

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


Set colTempFiles = objWMIService.ExecQuery _
 ("SELECT * FROM FileSystemObject WHERE Name = '*.tmp' AND "_
 & "NOT Name LIKE 'Prf%' AND Path LIKE 'C:\Users\adam\AppData\Local\Temp\%'")

colTempFiles.Count



For Each objFile in colTempFiles
Wscript.Echo objFile.Name
 'Set objF=objFSO.GetFile("objFile.Path")
 'objF.Delete(True)
Next

推荐答案

我认为您正在混淆两种不同的技术. FileSystemObject COM类,需要使用VBScript中的CreateObject()实例化.对于WQL,您需要在查询中使用 WMI类.这是 WMI类的核心列表.为了您的目的,您将希望使用CIM_DataFile类来处理文件.

I think you're confusing two different technologies. A FileSystemObject is a COM class that needs to be instantiated using CreateObject() in VBScript. For WQL, you need to use a WMI class in your query. Here is a core list of WMI classes. For your purposes, you'll want to use the CIM_DataFile class to work with files.

您可以使用任何一种技术.如果使用本地文件系统,则FileSystemObject是首选方法.如果需要在远程计算机上使用文件,请使用WMI和WQL.

You can use either technology. The FileSystemObject is the preferred method if you're working with the local file system. If you need to work with files on a remote machine, use WMI and WQL.

下面是使用FileSystemObject的示例:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\adam\AppData\Local\Temp")

For Each objFile In objFolder.Files
    If StrComp(objFSO.GetExtensionName(objFile.Path), "tmp", vbTextCompare) = 0 Then
        objFile.Delete    ' This is the Delete() method of the FSO's "File" class
    End If
Next

这是使用WQL的示例:

And here's an example using WQL:

strComputer = "."

' Connect to the WMI service on the specified computer...
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

' Build our WQL query...
strQuery = "select * from CIM_DataFile "
strQuery = strQuery & "where Drive='C:' "
strQuery = strQuery & "and Path='\\Users\\adam\\AppData\\Local\\Temp\\' "
strQuery = strQuery & "and Name like '%.tmp'"

' Run the query...
Set colTempFiles = objWMIService.ExecQuery(strQuery)

' Delete each file...
For Each objFile In colTempFiles
    objFile.Delete    ' This is the Delete() method of the WMI "CIM_DataFile" class
Next

这篇关于查询以选择所有非必需的临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆