Page 1 of 2 12>
Topic Options
#8517 - 2001-05-08 12:51 AM INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Hi!

I'm getting gray hairs here, and i'm only 26!

I'm trying to search *.lnk files for the appearance of a defined program, and if there is a match, delete the *.lnk. I want to do this because I sometimes don't know what the shortcut has been named.

code:

BREAK ON
$Dir = "D:\temp\test"
$Links = "$Dir" + "\*.lnk"
$Program = "vncviewer.exe"

$FileName = DIR("$Links")
WHILE $FileName <> "" AND @ERROR = 0
IF OPEN(1, "$Dir" + "\$Filename") = 0
$Content = READLINE(1)
WHILE @ERROR = 0 AND $Found <> 1
IF INSTR("$Content", "$Program") <> 0
$Found = 1 ; $Program appears more than once in the $Links,
?"$Program were found in i: $Filename" ; this is to speed things up.
ENDIF
$Content = READLINE(1)
LOOP
CLOSE(1)
IF $Found = 1
DEL "$Dir" + "\$Filename"
IF @ERROR <> 0
?" Didn't manage to dele the link [$Filename]"
ENDIF
ENDIF
$Found = 0
ENDIF
DIR() ; retrieve next file
LOOP


I only found this thread, unfortunately not containing any solution.

This script works 'sometimes' :/ This is part of a script to update/reinstall VNC.. (will post the full version when i'm done).

[This message has been edited by masken (edited 08 May 2001).]

_________________________
The tart is out there

Top
#8518 - 2001-05-08 02:58 AM Re: INSTR() problems - searching *.lnk files
cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
*.LNK files are, unfortunately, binary. KiX doesn't really like binary files as it strips CHR(13), CHR(10) and CHR(26) with the READLINE function. I have already suggested some Binary File IO functions, but you have two options in the meantime:

1. WSH/OLE. You can use the FileSystemObject with OLE/COM calls to read bytes or lines and seek to certain positions in a file. More on this in a bit...

2. DEBUG. You can create a text file full of DEBUG commands like D for Dump and then SHELL to DEBUG and < the commands file. You then > the output of debug and use the standard READLINE to read in the text.

The DEBUG output of binary is always in this format:

code:

1091:0000 4C 00 00 00 01 14 02 00-00 00 00 00 C0 00 00 00 L...............
1091:0010 00 00 00 46 0F 00 00 00-20 00 00 00 A0 76 E4 58 ...F.... ....v.X
1091:0020 AD C3 C0 01 00 30 14 DD-58 C3 C0 01 00 F0 8E 64 .....0..X......d
1091:0030 06 54 C0 01 3F C0 15 00-00 00 00 00 01 00 00 00 .T..?...........
1091:0040 00 00 00 00 00 00 00 00-00 00 00 00 8C 00 14 00 ................
1091:0050 1F 0F E0 4F D0 20 EA 3A-69 10 A2 D8 08 00 2B 30 ...O. .:i.....+0


so you can read a line, strip the first 11 and the last 19 chars then you have the hex values separated by space and -


Ok, back to the WSH/OLE/COM idea.


This will read an entire file into a variable:

code:

$sFilename="yourfilenamehere"


; open file for reading
$fText=val("&"+olecallfunc(olecreateobject("Scripting.FileSystemObject"), "OpenTextFile", "s", $sFilename))


; read ALL of file
$sText = olecallfunc($fText, "ReadAll")


; close file
$=olecallfunc($fText, "Close")


$sText now contains the entire file, so you can INSTR or SUBSTR to your hearts content.

cj

------------------
For more scripts goto cj's home page


chrismat@ozemail.com.au

Top
#8519 - 2001-05-08 03:12 AM Re: INSTR() problems - searching *.lnk files
kholm Offline
Korg Regular
*****

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

If you try to solve the Drive\Path\Program to a .lnk file only using KixTart, you should try this:

code:

BREAK ON
$Dir = "D:\temp\test"
$Links = "$Dir" + "\*.lnk"
$Program = "vncviewer.exe" ; Include drive and path in searchstring if you can
;$Program = %WINDIR% + "vncviewer.exe" ; Include drive and path for program in searchstring, if you know it

$Found = 0
$FileName = DIR("$Links")
WHILE $FileName <> "" And @ERROR = 0 And $Found = 0
IF OPEN(1, "$Dir" + "\$Filename") = 0
$Content = ""
$Line = READLINE(1)
WHILE $Line And @ERROR = 0
$Content = $Content + $Line
$Line = READLINE(1)
LOOP
$Err = CLOSE(1)
IF InStr($Content, $Program)
DEL $Dir + "\" + $FileName
IF @ERROR <> 0
?" Didn't manage to dele the link [" + $Dir + "\" + $FileName + "]"
ENDIF
$Found = -1
ENDIF
ENDIF
DIR() ; retrieve next file
LOOP



Erik

If for some reason the EOF character should appears in the midle of a .lnk file, you have to use cj's suggestions, it
could be the reason for the failing of your own script!

I have no gray hairs, they fall of before they get gray

[This message has been edited by kholm (edited 08 May 2001).]

Top
#8520 - 2001-05-08 11:19 AM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Hi again!

Thanks for replying cj & kholm!

The sad part is that I didn't get any of the suggestions to work , perhaps I did some serious BDU error?! I tried this:

code:

$sFilename = "D:\temp\test\Acrobat Assistant.lnk"

; open file for reading
$fText = VAL("&" + OLECALLFUNC(OLECREATEOBJECT("Scripting.FileSystemObject"), "OpenTextFile", "s", "$sFilename"))

; read ALL of file
$sText = OLECALLFUNC($fText, "ReadAll")

; close file
$ = OLECALLFUNC($fText, "Close")

?" the text: $sText."

IF INSTR("$sText", "AcroTray.exe") <> 0
?"AcroTray.exe were found in [$sFilename]"
ENDIF



code:

BREAK ON
$Dir = "D:\temp\test"
$Links = "$Dir" + "\*.lnk"
$Program = "AcroTray.exe" ; Include drive and path in searchstring if you can
;$Program = %WINDIR% + "vncviewer.exe" ; Include drive and path for program in searchstring, if you know it
$Found = 0

$FileName = DIR("$Links")
WHILE $FileName <> "" AND @ERROR = 0 AND $Found = 0
IF OPEN(1, "$Dir" + "\$Filename") = 0
$Content = ""
$Line = READLINE(1)
WHILE $Line AND @ERROR = 0
$Content = $Content + $Line
$Line = READLINE(1)
LOOP
$Err = CLOSE(1)
IF INSTR($Content, $Program)
?" String found in link [" + $Dir + "\" + $FileName + "]"
; DEL $Dir + "\" + $FileName
;IF @ERROR <> 0
; ?" Didn't manage to dele the link [" + $Dir + "\" + $FileName + "]"
;ENDIF
$Found = -1
ENDIF
ENDIF
DIR() ; retrieve next file
LOOP



Damn those shortcuts! cj, the output of your OLE thingie were a simple "L"! ? Wild gues, could it have something to do with me running a localized version of Win2000 (Swedish)?

[This message has been edited by masken (edited 08 May 2001).]

_________________________
The tart is out there

Top
#8521 - 2001-05-09 05:34 PM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Any of you people a programmer?!!

I found this source code, which will do EXACTLY what we want, but it isn't compiled, and i'm no programmer . There were also some improvements by other programmers on the source, all can be found at: http://www.codeguru.com/shell/gettingshortcuts.shtml

The original code looks like this:

code:

CString GetShortcutTarget(const CString LinkFileName)
{
HRESULT hres;

CString Link, Temp = LinkFileName;
Temp.MakeLower();
if (Temp.Find(".lnk")==-1) //Check if the name ends with .lnk
Link = LinkFileName + ".lnk"; //if not, append it
else
Link = LinkFileName;

CString Info;
Info.Empty();

IShellLink* psl;

//Create the ShellLink object
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*) &psl);

if (SUCCEEDED(hres))
{
IPersistFile* ppf;
//Bind the ShellLink object to the Persistent File
hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
if (SUCCEEDED(hres))
{
WORD wsz[MAX_PATH];
//Get a UNICODE wide string wsz from the Link path
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Link, -1, wsz, MAX_PATH);

//Read the link into the persistent file
hres = ppf->Load(wsz, 0);

if (SUCCEEDED(hres))
{
//Read the target information from the link object
//UNC paths are supported (SLGP_UNCPRIORITY)
psl->GetPath(Temp.GetBuffer(1024), 1024, NULL, SLGP_UNCPRIORITY);
Temp.ReleaseBuffer();
Info = Temp;

//Read the arguments from the link object
psl->GetArguments(Temp.GetBuffer(1024), 1024);
Temp.ReleaseBuffer();
Info += " " + Temp;
}
}
}
psl->Release();
//Return the Target and the Argument as a CString
return Info;
}



Cool if it could be compiled (and of course work )
_________________________
The tart is out there

Top
#8522 - 2001-05-17 03:02 PM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
hmm.. is there really no solution for this? I'm sure it must have been done before?

please please help!

_________________________
The tart is out there

Top
#8523 - 2001-05-17 03:07 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
masken...

I'll compile this into a command line utility later today or tomorrow - cool ?

Shawn.

Top
#8524 - 2001-05-17 03:33 PM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Who da man? Shawn da man!
Thanks Shawn, I was afraid this thread had gone dead, but apparently not. Thanks a bunch (no need to stress for little me)!
_________________________
The tart is out there

Top
#8525 - 2001-05-17 06:06 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
masken,

how would you like this utility to work - what would you like to call it - how would you like to call it (eg, command line params, etc) ?

Shawn.

Top
#8526 - 2001-05-18 10:51 AM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Wow, thanks Shawn, you rule.

Well, I was thinking of a few basic things;

Usage: application.exe [ShortcutToCheck.lnk] [Field to check] [string to check for] [returntype]

Where:
[ShortCutToCheck.lnk]
-the filename of the shortcut to check, if it contains whitespace, enclosed in "".
[Field to check]
-Target
-Working Directory (not very important, but would be useful)
[String to check for]
freetext string to search for, case insensitive.
[returntype]
-default (meaning just an errorlevel)
-dump contents of "Field to check"
-dump all the readable contents of the *.lnk (the two fields, or whatever useful info that could be substracted).

The Errorlevels:
0 = shortcut were found, the string specified were found in the field specified.
1 = shortcut were found, the string specified were not found in the field specified.
2 = shortcut not found.
(3 = error, @error, @serror?)

_________________________
The tart is out there

Top
#8527 - 2001-05-18 05:37 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611

MASKEN - WHATS YOUR EMAIL ADDRESS ?
SEND ME AN EMAIL AND I'LL SEND YOU THE EXE

okee-dokee masken, here you go...

LINK.EXE

usage: link filename.lnk [index string]

indexes:

0 target
1 arguments
2 workingdirectory
3 comment
4 iconpath

I gave this utilitiy the following features...

1) To do a string compare against an internalize link string (like target), you run it like this...

link notepad.lnk 0 "c:\winnt\system32\notepad.exe"

KiX's @ERROR macro will be set to the following values:

-1 string < link string
0 string = link string
1 string > link string

The various indexes are listed above.

The other feature I added was the ability to dump the shortcut settings to the console, if you run it like this...

C:\> link notepad.lnk

You'll get this dumped to your console (stdout)...

[notepad.lnk]
Comment=
Target=%windir%\System32\notepad.exe
Arguments=
WorkingDirectory=%windir%
Hotkey=0
ShowCmd=1
IconPath=
IconIndex=0

The way you could use this is to redirect link's output to a temporary file ...

shell "%comspec% /c link notepad.lnk >link.ini"

Then use KiX's readprofilestring function to read any value you wanted back into your script...

Shawn.


I can't figure out a way to pass environment variables (like %WINDIR%) throught the command line to link.exe - alot of links use these in their targets and the compare doesn't work as expected - does anyone know how to pass these things ? I've already tried double quotes !

link notepad.lnk 0 "%windir%"

???

Shawn.

[This message has been edited by Shawn (edited 18 May 2001).]

Top
#8528 - 2001-05-18 09:29 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hello masken - are you there - it's friday and i' leaving early today... you do want your shiny new link.exe ?
Top
#8529 - 2001-05-21 04:45 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ping -t -a masken


Top
#8530 - 2001-05-21 05:19 PM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Sending signals to mats.bjur@wettergrens.se with cool data:
response from masken: [user seems to be dead] time [too long]
response from masken: [user is twitching] time [a while ago]
response from masken: [user screams ARGHH, being swamped in work] time [constant]

Hi Shawn! Sorry for not replying earlier, but i've been swamped, got 12 stores round the city where I live to support, and when something goes wrong, well, you know, its at ALL the places at the same time! :P

There you have my e-mail adress, I will sit here making my eyes square until it hits my mailbox!

Thanks again, you have been most, most kind!

[This message has been edited by masken (edited 21 May 2001).]

_________________________
The tart is out there

Top
#8531 - 2001-05-28 05:54 PM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
I've now tested Shawn's util on Win95a, WinNT, and Win2000 (Swedish editions, except NT4 (server)), and all is Ok! Thank you very, very much Shawn!
_________________________
The tart is out there

Top
#8532 - 2001-05-29 08:06 AM Re: INSTR() problems - searching *.lnk files
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear Shawn,

Send a copy of your LINK program to us. We will check your problem.
Question: which program language you are using?
Greetings.

_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
#8533 - 2001-05-29 03:18 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
MCA,

Right on - the sourcode and exe are on the way (written in C based on above example code). The problem is when you create a shortcut (eg, to notepad) that has a target something like this:

%windir%\system32\notepad.exe

I can't figure out a way to specify %windir% on the command line without the shell translating the environment variable into c:\winnt (before being passed to the exe) ...

I've tried double-quotes and double-percents and all that obvious stuff...

Shawn.

Top
#8534 - 2001-05-29 03:48 PM Re: INSTR() problems - searching *.lnk files
Jack Lothian Offline
MM club member
*****

Registered: 1999-10-22
Posts: 1169
Loc: Ottawa,Ontario, Canada
Wow, this is one of the most interesting boards on the net. The stuff you guys come up with is amazing.

When you guys finish tuning this utility why not post it on your sites.

Did you notice that masken's url to the codeguru leads to a second utility from the same programmer that creates shortcuts? Seems to me that is a topic that has come up more than once on this board.

[This message has been edited by JackLothian (edited 29 May 2001).]

_________________________
Jack

Top
#8535 - 2001-05-29 06:09 PM Re: INSTR() problems - searching *.lnk files
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Shawn,
how about the GETENV() function?

MSDN GETENV() Function, #2
GETENV( ) Function Example, #2
getenv, _wgetenv

maybe i've gone fishing in the wrong pond here? But as I said, im no programmer I think that perhaps the last link can be interesting?

hth, Masken

[This message has been edited by masken (edited 29 May 2001).]

_________________________
The tart is out there

Top
#8536 - 2001-05-29 07:22 PM Re: INSTR() problems - searching *.lnk files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Mats:

yeah - not only are you fishing in the right pond - me thinks you caught a big'n. I imagine what should be done is to let the command interpreter translate the environment variable (cause I can't see anyway to disable it) and then have the program take the target string from the shortcut and expand that as well (like the shell did) - then compare those two values ! yup - that'll do 'er !

Jack:

Great idea - yeah - we see enough shortcut create/query questions on da-board here to justify that. Maybe MCA would like to create an all-knowing, all-doing link.exe utility that can create,modify,dump,compare shortcut files. I myself feel that the reskit shortcut.exe is a little "lacking" in certain regards - haven't checked-out any others on the web though. But at least we can call this one "our own" and can enhance it as required.

MCA:

You interested in doing that ?

-Shawn

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 799 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.054 seconds in which 0.029 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