Page 1 of 2 12>
Topic Options
#75708 - 2003-07-08 07:08 AM Shortcuts - getting the target of an existing shortcut
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
So aside from using WSH, how could one read the target of a shortcut knowing the full path of the link? It would appear that Application.Shell and WMI could both do the task but I am having little success. I can easily use
code:
GetObject("WinMgmts:\root\cimv2").ExecQuery("Select * From Win32_ShortcutFile")

to enumerate all shortcuts within a computer, but I am having problems getting one specific shortcut. I have tried
code:
GetObject("WinMgmts:\root\cimv2").ExecQuery("Select * From Win32_ShortcutFile Where Name='C:\Documents and Settings\All Users\Start Menu\Programs\Startup\test.lnk'")

but it returns nothing instead of the shortcut. I understand that Application.Shell may also be able to do this so my ears are open to suggestion.

Top
#75709 - 2003-07-08 09:34 AM Re: Shortcuts - getting the target of an existing shortcut
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
why don't you use wscript object from kix ?
the createshortcut method can open an existing file.


global $objShell, $strDesktop

$objShell = CreateObject("WScript.Shell")
$strDesktop = $objShell.SpecialFolders("Desktop")

$strStartMenu = $objShell.SpecialFolders("StartMenu")
$strPath = $strStartMenu + "\Programmes"

$=ShowShortcutInfo( "Internet Explorer.lnk", $strPath )

?
?"== ended =="
flushkb
gets $x
exit 0

function ShowShortcutInfo( $filename, $path )
Dim $objShortCut
Dim $strFilename

$strFilename = "$Path\$filename"
if not exist($strFileName)
? "File "+$strFilename+" does not exist"
else
$objShortCut = $objShell.CreateShortCut( $strFilename )

? " TargetPath : " +$objShortCut.TargetPath
? " Arguments : " +$objShortCut.Arguments
? " WindowStyle : " +$objShortCut.WindowStyle
? " Hotkey : " +$objShortCut.Hotkey
? " IconLocation : " +$objShortCut.IconLocation
? " Description : " +$objShortCut.Description
? " WorkingDir : " +$objShortCut.WorkingDirectory
?

$objShortCut = nothing
endif
endfunction


if you want to modify the shortcut, properties are read/write and you can save the info with the save method :


$objShortCut = $objShell.CreateShortCut( $strFilename )

$objShortCut.TargetPath = "notepad.exe"
$objShortCut.Arguments = "my file.txt"
$objShortCut.WindowStyle = 1
$objShortCut.Hotkey = ""
$objShortCut.IconLocation = "notepad.exe,0"
$objShortCut.Description = "this is my own description"
$objShortCut.WorkingDirectory = "%temp%"

$objShortCut = nothing
_________________________
Christophe

Top
#75710 - 2003-07-08 09:48 AM Re: Shortcuts - getting the target of an existing shortcut
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
quick search at our udf-library gives at least these 2 perfectly fitting examples:
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000256
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000143

the first is kinda based on:
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000254

and the latter has quite many dependencies...
if you want to search yourself, browse to:
http://www.gwspikval.com/jooel/UDF
_________________________
!

download KiXnet

Top
#75711 - 2003-07-08 03:14 PM Re: Shortcuts - getting the target of an existing shortcut
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The second one is more of a link synchronization tool that replicates a link directory from a central repository.
_________________________
There are two types of vessels, submarines and targets.

Top
#75712 - 2003-07-08 10:16 PM Re: Shortcuts - getting the target of an existing shortcut
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
It is against policy to READ the first message before posting a reply? I cannot use WSH which was in the FIRST 5 words of the post. I went on to ask if anybody knew how to access link properties using either WMI or an Explorer object. If you are a smart person and you can help me with this, please post. If your advise is, "why don't you use WSH" or, "why don't you use UDF X which uses WSH or is completely irrelivent" then I would highly suggest you go back to first grade and LEARN TO READ!

Sorry about getting so pissed off but I really wish people would read a post before responding.

Top
#75713 - 2003-07-08 10:31 PM Re: Shortcuts - getting the target of an existing shortcut
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
quote:
So aside from using WSH
does not say that you CANNOT use WSH. It rather implies that you are looking for alternative ways, e.g. using WMI, as you then go on with a WMI code fragment.

Secondly, there is not reason to get *** off as people try to help you. However, you might want to consider to first search the BBS for solutions to your questions. Then you might have discovered the referenced UDF that uses WSH to manipulate .LNK files and you could have said, "yeah, found that UDF, but I'd rather use WMI for this".

Also, WMI requires you to be very dilligent with regards to querying it. So, I have no idea whether you do proper error checking or use proper permissions to execute the query.

Oh, and I just used Google to look up some scripts doing what you're asking for by using just two keywords. The code used is very straight-forward. I'm pretty sure you know how to use a search engine and I'm also sure you are aware that the Microsoft website has extensive WMI documentation.

Oh, and we also prefer to not have such long lines inside quote tags as they make such threads hard to read.

[ 08. July 2003, 22:33: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#75714 - 2003-07-08 10:59 PM Re: Shortcuts - getting the target of an existing shortcut
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
Seal, you are one of those people who needs to go back to first grade and not only learn to read,
but understand what you are reading. I wouldn't have said 'aside from using wsh' if I
could use WSH. Additionally, I did search the site, I did quiet a bit of searching. What
did I find? I found that everything out there either uses wsh or doesn't help me one
bit. Maybe rather then thinking you know everything you should assume you don't and go
from there. With regards to WMI, I found a class that nobody on the KiXtart site appears to
have used, that being Win32_ShortcutFile. I made some attempts to use it, as I explained in
the post you still haven't read, but ran into a problem. I posted more then sufficient
information for a WMI wiz out there to give me a little help. What I didn't count on was a
bojo making the post look answered when it isn't. Now before you look like an even bigger
idiot, would you please READ my post with OPEN eyes before responding again? Beyond that,
if you are not posting code that uses WMI to read the target of a shortcut, you are not
helping me or the site. I am NOT interesting in a mud fight with other bulletin board
members. All I want is some working code delivered though clear, logical, and relivent (on
point) posts. If I had wanted a WSH v. WMI debate, I would have asked for that.


As for the long lines, I would have though the site engine would be smart enough to wrap non-
code lines. I will add -br-'s in the future.

Top
#75715 - 2003-07-08 11:06 PM Re: Shortcuts - getting the target of an existing shortcut
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Damn Gaven... You have such a unique method of asking for help [Roll Eyes]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#75716 - 2003-07-08 11:25 PM Re: Shortcuts - getting the target of an existing shortcut
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I hate it when this kinda stuff happens ... Gaven, I dont think your justified in jumping all over one of our more respected and helpfull members (Jen's vote count speaks volumes). Lets try to keep things loose and friendly eh ? You can choose to take advise or ignore it altogether, Jen's certainly took the time out of his busy day to be helpfull ...
Top
#75717 - 2003-07-08 11:47 PM Re: Shortcuts - getting the target of an existing shortcut
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
I hear your point Shawn but I slightly disagree. While Jens may be a moderator, he does not walk
on water. To be more on point, he, and others, quiet clearly posted responses after
reading the subject and NOT the post. This bulletin board will no longer be helpful if it
becomes a forum for people to hear themselves. I appreciate the fact that Jens read
my post, but I am angry that he would half-ass it to the point of not reading the post.
This is supposed to be a forum of intelligent programmers thus I submit that an intelligent
person would read a post and respond accordingly.

[ 08. July 2003, 23:52: Message edited by: Gaven ]

Top
#75718 - 2003-07-08 11:54 PM Re: Shortcuts - getting the target of an existing shortcut
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Gaven,
I clearly have to vote against you. Reviewing the thread, Jens' first entry simply says:
quote:
The second one is more of a link synchronization tool that replicates a link directory from a central repository.
I certainly don't see anything that warrants the attack you unleashed!
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#75719 - 2003-07-08 11:54 PM Re: Shortcuts - getting the target of an existing shortcut
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Gaven,

You still must remember one thing. We are all volunteers. If there was a mis-interpretation of the original question, then maybe clarification is needed. You will go further with patience rather than jumping all over people.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#75720 - 2003-07-09 01:01 AM Re: Shortcuts - getting the target of an existing shortcut
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Gaven,

Don't hit the ceiling.

Give credits to all that is trying to help you.

However, I tried running these to scripts:

Script 1:
code:
$colItems = GetObject("winmgmts:\root\cimv2").ExecQuery("Select * from Win32_ShortcutFile")
For each $objItem in $colItems
"Name: " + $objItem.Name ?
?
Next

This returns the names of all shortcuts on my PC

One of these shorcuts is:
c:\programs\msn\msn explorer.lnk

So I would expect this code to return only that shorcut:
Script 2:
code:
$colItems = GetObject("winmgmts:\root\cimv2").ExecQuery("Select * from Win32_ShortcutFile where name='c:\programs\msn\msn explorer.lnk'")
For each $objItem in $colItems
"Name: " + $objItem.Name ?
?
Next

Script 2 returns nothing, quite mysterious ??

Anyway, I don't think you should envestigate this way of checking shortcuts further, because it seems that winmgmts:\Win32_ShortcutFile
expands and checks all propertys of all shortcuts on the computer. On a slow line this takes loong.

Gaven,

I'm just as mystified as you.

BUT:
Yoy could have added further explaining post instead of attacking people who are trying to help you

-Erik

Top
#75721 - 2003-07-09 02:32 AM Re: Shortcuts - getting the target of an existing shortcut
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
Erik, thank you.
To everybody: for what it's worth I didn't jump down Jens throat. In fact, I would be more
inclined to say Jens jumped down my throat with the first direct insult. I would encourage
people to not get caught up on my hyperbole. I don't actually think anybody
should go back to first grade, nor am I directly insulting the intelligence of anybody.
We all do stupid things, but people need to be able to simply admit to the mistake and move
on. What I think should have happened here is Jens second post should have not existed -
there was simply no reason for it. He, as stated, didn't offer useless information. He
did, however, throw gas on the fire which means this little "flame up" is his fault more
then anyone else's. Now before anybody comments, read all above responses in their
entirety. Don't skim over and think you know what going on.

Top
#75722 - 2003-07-09 03:22 AM Re: Shortcuts - getting the target of an existing shortcut
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Gaven, you mentioned shell.application as a possible option ? Heres something cobbled together from bits and pieces of this and that ...

btw, unless i read you wrong and your not looking for shell.application, go easy on me ? [Wink]


break on

$FOLDER = "F:\"
$SHORTCUT = "NOTEPAD.LNK"

$objShell = CreateObject("Shell.Application")

$objFolder2 = $objShell.NameSpace($FOLDER)

if ($objFolder2)

$objFolderItem = $objFolder2.ParseName($SHORTCUT)

if ($objFolderItem)

$objLink = $objFolderItem.GetLink

if ($objLink)
?"Shortcut: $SHORTCUT"?
?"Arguments=" $objLink.Arguments
?"Description=" $objLink.Description
?"Path=" $objLink.Path
?"ShowCommand=" $objLink.ShowCommand
?"Target=" $objLink.Target
?"WorkingDirectory=" $objLink.WorkingDirectory

endif

endif

endif


-Shawn

[ 09. July 2003, 03:23: Message edited by: Shawn ]

Top
#75723 - 2003-07-09 03:24 AM Re: Shortcuts - getting the target of an existing shortcut
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
quote:
If you are a smart person and you can help me with this, please post. If your advise is, "why don't you use WSH" or, "why don't you use UDF X which uses WSH or is completely irrelivent" then I would highly suggest you go back to first grade and LEARN TO READ!

quote:
What I didn't count on was a
bojo making the post look answered when it isn't. Now before you look like an even bigger
idiot, would you please READ my post with OPEN eyes before responding again?

quote:
I appreciate the fact that Jens read
my post, but I am angry that he would half-ass it to the point of not reading the post.
This is supposed to be a forum of intelligent programmers thus I submit that an intelligent
person would read a post and respond accordingly.

quote:
He
did, however, throw gas on the fire which means this little "flame up" is his fault more
then anyone else's. Now before anybody comments, read all above responses in their
entirety. Don't skim over and think you know what going on.

Consider yourself Plonked noob
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#75724 - 2003-07-09 03:29 AM Re: Shortcuts - getting the target of an existing shortcut
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Hi Gaven,

If I may. Perhaps you could look at this from another point of view. I know that it is often hard to accept or feel fault and I'm not trying to place fault on you, rather I'd like to make a little bit of an analogy.

Let's say that you join the Army and after training for 4 weeks as an Infantryman, they now ship you off to some hostile place where the Army is at War. They have you join a crew of Rangers that has been fighting together for 2 years now. They have established quite a bond with each other. Then when you show up, you have a new idea that you present to them, but the presentation didn't go as well as you planned. Somehow, accidentally it now appears as though you are attacking one of their long time buddies. Now all of the guys have started to form a disliking to you. At this point (in my humble opinion) you could do a couple things.

1. Apologize and hope that you could blend in with the guys in the near future (even if you felt you had done nothing wrong).

2. Ignore them and go about your merry way, realizing that in the future when your life might depend upon one of them, that they would not come to your rescue. (I know that programming is not the same life threatening circumstance here, but I’m trying to get a point across)

3. Continue to protest and acert your rights and feelings on the matter. (which in most cases would still garner ill feeling from the other guys).

What I'm trying to say is, as a new guy I personally think it would be easier and perhaps more prudent to suck it up. Yes you can continue to disagree or fight it, but bottom line is that as a new comer to this board it is difficult to "blow-up" and expect anyone to continue to help you. This has now become more a matter of social graces rather then "who is right".

[ 09. July 2003, 03:30: Message edited by: NTDOC ]

Top
#75725 - 2003-07-09 04:28 AM Re: Shortcuts - getting the target of an existing shortcut
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
Shawn, YES YES YES!!! After all this headache I'm glad that I was still able to get an answer to my question. Thank you, your code is my answer.
Top
#75726 - 2003-07-09 05:12 AM Re: Shortcuts - getting the target of an existing shortcut
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Gaven: I hope this will satisfy you and comply with your restriction of using a non-WSH solution
code:
Break On
dim $irc

$irC=setoption('Explicit','on')
$iRC=setoption('NoVarsInStrings','on')
$iRC=setoption('WrapAtEOL','on')

dim $objWBEM, $colShortcuts, $objShortcut, $sWQL, $sShortcut

$sShortcut='C:\Documents and Settings\All Users\Start Menu\Programs\Internet Explorer\Internet Explorer.lnk'

$sWQL="Select * from Win32_ShortcutFile where name='"+join(split($sShortcut,'\'),'\\')+"'"

$objWBEM = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colShortcuts=$objWBEM.ExecQuery($sWQL)

? 'Number shortcuts = '+$colShortcuts.count
if $colShortcuts.count
For each $objShortcut in $colShortcuts
? 'Name : ' +$objShortcut.Name
? 'Description : ' +$objShortCut.description
? ''
Next
else
? 'No matching shortcuts found.'
endif

exit 0

However, I would recommed to check for the existance of the file via EXIST first as the WMI call will need to enumerate all shortcuts in order to filter them. The filecheck would speed up things if the shortcut does not exist.
_________________________
There are two types of vessels, submarines and targets.

Top
#75727 - 2003-07-09 05:30 AM Re: Shortcuts - getting the target of an existing shortcut
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Double slashes ! Double slashes !! That was the issue ? Good call!
Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 980 anonymous users online.
Newest Members
rrosell, PatrickPinto, Raoul, Timothy, Jojo67
17877 Registered Users

Generated in 0.077 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org