Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SCRIPT

Posted on 2006-06-08
20
Medium Priority
?
1,481 Views
Last Modified: 2011-10-03
hi,

im looking to create a vb script which will search the entire drive based on filenames say support.exe, support.lnk and then delete them
reason it has to search the entire drive is we know path differs for all users

i have kind of mismatched but doesnt work?

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

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile where Name = 'support.exe' OR Name = 'support1.ico' OR Name = 'Support1.lnk'")

For Each objFile in colFiles
   objFile.Delete
Next

This was originally the following and will search all files with extn .ram and delete them...

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

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile where Extension = 'ram''")

For Each objFile in colFiles
   objFile.Delete
Next
0
Comment
Question by:amajidkh
  • 9
  • 6
  • 5
20 Comments
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 450 total points
ID: 16862391

You can't use OR statements in the Select I'm afraid - that's not a valid WQL (cut down version of SQL) statement.

You should be able to select select everything with support in it with (not sure if it's case sensitive so there's a wildcard for the first letter too):

SELECT * FROM cim_DataFile WHERE Name LIKE '%upport%'

Then during your Loop:

For Each objFile in colFiles
     If objFile.Name = "support.exe" Or objFile.Name = 'support1.ico' Or objFile.Name = 'Support1.lnk' Then
          objFile.Delete
     End If
Next

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 16862737

Finally finished running the test script... Name is perhaps a bad choice of attribute for this one, it's the full path to the file rather than filename itself. So perhaps a few changes:

The query will work as it stands and pull out everything with Support in it. But the If statements need some refinement as Name represents the full path to the file:

If objFile.FileName = "support.exe" Or objFile.FileName = "support1.ico" Or _
          objFile.FileName = "Support1.lnk" Then

And if we're doing that we may as well do the same with the initial select:

SELECT * FROM CIM_DataFile WHERE FileName LIKE '%upport%'

Chris
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16863424
thanks for the reply,

so basically it should be as follows:

======================

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

Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM cim_DataFile WHERE Name LIKE = 'support1.exe' Name LIKE = 'cnssupport1.exe'")

For Each objFile in colFiles
     If objFile.Name = 'support1.exe' Or objFile.Name = 'cnssupport1.exe' Then
          objFile.Delete
     End If
Next

======================
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Author Comment

by:amajidkh
ID: 16863427
i checked it i get an error
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 16863982

Hi again,

First off I should apologise for inserting a couple of errors into the original script. They certainly wouldn't help things along.

Anyway, there are still a few things that need clearing up in the code you've just posted. An update of the code is at the bottom so feel free to head straight down if you prefer; Anyway, in order:

1. "SELECT * FROM cim_DataFile WHERE Name LIKE = 'support1.exe' Name LIKE = 'cnssupport1.exe'"

This isn't a valid statement I'm afraid. You're still trying to do multiple statements for matching and unfortunately the query language you get to use just isn't going to let you (that is, as far as I've ever been able to work out and as far as the MS examples and documentation suggests).

Also, LIKE replaces = in your statement rather than being used in conjunction with.

Finally, LIKE really needs a Wildcard (%) or you may as well use =. The reason for choosing LIKE earlier was to allow you to select multiple values then finish off sorting them out with the If statements a little later on.

2. objFile.Name

This isn't the same as the File System Object; for WMI you don't get the file name back with Name, you get the Path and FileName (something like C:\Program Files\CNS Support\cnssupport1.exe). Fine for doing the SELECT statement if a Wildcard is used, but no good for the If statements.

3. objFile.Name = 'support1.exe'

Sorry that one was me... should have been If objFile.FileName = "support1.exe"...

That's all of those, this is an updated version that doesn't immediately jump out with an error. Still takes a long time to run though (depending on the size of your filesystem).


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

Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM cim_DataFile WHERE FileName LIKE '%upport1.exe'")

For Each objFile in colFiles
     If objFile.FileName = "support1.exe" Or objFile.FileName = "cnssupport1.exe" Then
          objFile.Delete
     End If
Next
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16864699
oki its makes more sense now...
however i would still need to have it search for multiple file names, let me explain basically the issues are as follows:

1. users may have download different version of our support tool in the past
2. it has had different names: cnsremotesupport.exe, support.exe, mpc.exe,
3. We dont know the exact location as to where the file may have been downloaded hence the search on entire file system
4. Shortcuts might have been created, so can we actually put a file name in with spaces ie 'remote support.lnk'
5. if the above query doesnt support multiple file names then do i just keep adding the

Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM cim_DataFile WHERE FileName LIKE 'remoteupport.exe'")

For Each objFile in colFiles
     If objFile.FileName = "remotesupport.exe" Then
          objFile.Delete

with each instance of file name then end if and next
0
 
LVL 10

Accepted Solution

by:
Walter Padrón earned 1050 total points
ID: 16864702
Hi amajidkh,

Just in case you need a command line version

for /R x:\root_dir %d in (support.exe support.lnk) do @if exist %d del /F /Q %d

cheers
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16864858
just tried that doesnt work
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 16865382
If used inside a .cmd file, the % must be escaped

for /R x:\root_dir %%d in (support.exe support.lnk) do @if exist %%d del /F /Q %%d

cheers
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 16865497

You might be better with that to be honest.

The only way to efficiently do it with WMI is to make it so the initial query returns every possible file you might want - so "SELECT * FROM cim_DataFile WHERE Extension = 'exe'" then sort it out with the If statements. That minimizes the search time on the hard disk at least.

Of course you just bump into the same problem again the minute you need to delete the shortcuts with the lnk extensions...

If they all included Support in their names then it would be easier as you could do "SELECT * FROM CIM_DataFile WHERE FileName LIKE '%support%'".

But really it's going round in circles.

Chris
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16868325
WPA:

i get a %%d unexpected at this time...

command used:

for /R x:\root_dir %%d in (support1.exe conoscoremotesupportsupport1.exe) do @if exist %%d del /F /Q %%d

CHRIS

I tried this:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM cim_DataFile WHERE FileName LIKE '%upport1.exe'")

For Each objFile in colFiles
     If objFile.FileName = "support1.exe" Or objFile.FileName = "conoscoremotesupport1.exe" Then
          objFile.Delete
     End If
Next

doesnt work, i have these folders in my c:\new folder and it didnt seem to find them and delete
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 16868518

Is the filename support1.exe and not Support1.exe?

Chris
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 16869911
sorry mate, the missings "" break the script when used on directories or filenames with spaces, run this on the command prompt
for /R "x:\root_dir with spaces" %d in ("support with spaces.exe" support.lnk) do @if exist "%d" del /F /Q "%d"

you can test what the script will do with
for /R "x:\root_dir with spaces" %d in ("support with spaces.exe" support.lnk) do @if exist "%d" dir "%d"

cheers
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16870018
chris,

lowercase...support1.exe

wpa,

ran this at cmd :
for /R "x:\root_dir with spaces" %d in (support1.exe "conosco support1.lnk" conoscoremotesupport1.exe) do @if exist "%d" del /F /Q "%d"

just goes back to c:\>

does x: represent drive should it not be c:
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 16870334
you must change "x:\root_dir with spaces" with the directory you wish to clean (for instance "C:\Documents and Settings") or you can use "." instead and that works from your current directory.
If you want to be asked before deleting the file use /P instead of /Q    (   del /F /P "%d"  )

cheers
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16884331
hey wpa,

i ran the command it kind of works for the some files and 1 fil ein particular it doesnt it looks as if the "" are not effective as it looks for conosco then remote then support i didnt know if it was case sensitive so added in the following

for /R "c:\" %d in (support1.exe Support1.exe "conosco support1.lnk" "Conosco Remote Support.lnk" conoscoremotesupport1.exe) do @if exist "%d" del /F /Q "%d"

Also is it a different command if i was to run this from a batch file?



0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 16884553

> lowercase...support1.exe

I guess the only thing you could really do to troubleshoot that is to make it Echo the filenames instead of trying to delete. It's possible you may need a few more steps to get it to delete the file - it would be nice to see if it's even finding it.

However... I think that WPA's solution may be the way to go - just so much more straightforward.

Chris
0
 
LVL 4

Author Comment

by:amajidkh
ID: 16885238
okay figured out that i need to add the % in a batch file, however the issues with file names with spaces still a problem?
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 16888925
Hi,

If you need to use this command in a batch file you must escape the % as

for /R "c:\" %%d in (support1.exe Support1.exe "conosco support1.lnk" "Conosco Remote Support.lnk" conoscoremotesupport1.exe) do @if exist "%%d" del /F /Q "%%d"

cheers

0
 
LVL 4

Author Comment

by:amajidkh
ID: 16909654
i havent been able to delete any shortcuts with spaces. but at least part of the solution is working
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally Windows/Microsoft Updates will fail to update. We have found a code that will delete all temporary files and re-register all dll's related to Windows/Microsoft Updates! This works 99% of the time to get the updates working again! The…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question