#8517 - 2001-05-08 12:51 AM
INSTR() problems - searching *.lnk files
|
masken
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
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
|
|
|
|
#8520 - 2001-05-08 11:19 AM
Re: INSTR() problems - searching *.lnk files
|
masken
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
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
|
|
|
|
#8526 - 2001-05-18 10:51 AM
Re: INSTR() problems - searching *.lnk files
|
masken
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
   
Registered: 1999-08-13
Posts: 8611
|
MASKEN - WHATS YOUR EMAIL ADDRESS ? SEND ME AN EMAIL AND I'LL SEND YOU THE EXEokee-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
|
|
|
|
#8529 - 2001-05-21 04:45 PM
Re: INSTR() problems - searching *.lnk files
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
|
|
Top
|
|
|
|
#8530 - 2001-05-21 05:19 PM
Re: INSTR() problems - searching *.lnk files
|
masken
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
|
|
|
|
#8536 - 2001-05-29 07:22 PM
Re: INSTR() problems - searching *.lnk files
|
Shawn
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 700 anonymous users online.
|
|
|