Delete Shortcut with Code

  • Thread starter Thread starter zSplash
  • Start date Start date
Z

zSplash

I am trying to delete all shortcuts that have "log" in them on the desktop
(Win2k). I have unsuccessfully tried the following code:

Kill "C:\Documents and Settings\username\Desktop\*log*.lnk"

I know these shortcuts exist, and my code simply won't delete them. Using
this syntax, even if I put the exact name of the known-to-exist shortcut
(i.e., "Mylog.lnk"), the code does not delete it. Any suggestions, guys?

TIA
 
I am not surprised that the *log* didn't work, but the full form worked for
me. I had to change username to my name, and I used PINS2.lnk, and it went
fine.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks, Bob.

So, are you thinking that wildcards won't work? Any other way to "wildcard"
the name (in case it's close, but not exact)?

st.
 
Indeed I am.

IU did think of uysing FileSearch which can handle wildcards, but this
seemed to return the shortcut target rather than the shortcut. So I
developed this bit of code to do it

Dim oFSO As Object
Dim oFolder As Object
Dim oSubfolder As Object
Dim oFile As Object

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Documents and Settings\Bob\Desktop\")
For Each oFile In oFolder.Files
If oFile.Type = "Shortcut" Then
kill ofile.path
End If
Next
End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Bob! You are wonderful!! I used your code, adding the following to
"wildcard it". It works great. Many, many thanks for your kindness. I'm
smiling again, thanks to you...

... If UCase(InStr(1, oFile.Name, "LOG")) <> 0 Then
Kill oFile.Path
End If ...

st.
 
Great, and thanks for the feedback.

I forgot about the log bit in the filename in the exchanges, sorry about
that, but you obviously had no problem with it.

Keep smiling!

Bob
 
Rats, Bob. I smiled too soon. I just assumed that the code:
UCase(InStr(1, oFile.Name, "LOG"))
would return with a positional value if the shortcut existed, but even
shortcuts
I know to exist (i.e., "MYLOG") are coming back with a 0 value; hence, they
are not being killed.

st.
 
HOLD EVERYTHING, Bob!! You are really right! I'm a numbskull!!

Ucase has to be around oFile.Name, not around the function that returns a
position/integer.
UCase(InStr(1, UCase(oFile.Name), "LOG")
You really deserve even more gratitude, for my stupidity. Thanks ever so
much! (the smile's covering the whole face, now...)

st.
 
Sorry, in your last post I read what you said but only superficially looked
at your code, otherwise I would have seen the flaw.

Change

If UCase(InStr(1, oFile.Name, "LOG")) <> 0 Then

to

If InStr(1, UCase(oFile.Name), "LOG") <> 0 Then

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
If InStr(1, oFile.Name, "LOG", vbTextCompare) <> 0 Then

would probably be better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top