Batch file to list start menu shortcuts + their targets

Posted on 2007-04-06
Medium Priority
Last Modified: 2013-12-28
Hi Experts
I've been trying to thin out and streamline a problem Win98 (1st edition) system using a remote connection by VNC.  It's hard going, because the problem PC is on a standard dialup modem connection.  I'm on broadband, but the screen refresh time lag makes it really difficult navigating the start menu, and frequently causes me to click the wrong shortcut and launch app's that freeze the system.

There's a lot of redundant start menu folders and shortcuts, and I was hoping to try and get a listing of the shortcuts and the targets they point to by running a batch file rather than having to check out the property pages of each shortcut one at a time.

I can get the target of a given shortcut into a text file using the command:
type "file name.lnk" | find /i "c:\" > linknames.txt
but I can't figure a way that would traverse through the start menu listing the *.LNK files and then acting on each one to give a list something like this:

C:\Windows\Start Menu\Programs\Prog1 Folder\Shortcut1.lnk
C:\Program Files\Program1\Target1.exe
C:\Windows\Start Menu\Programs\Prog2 Folder\Shortcut2.lnk
C:\Program Files\Program2\Target2.exe

I'm aware of the CHKLNKS.EXE program in the Win98 Resource Kit, but I would rather get a listing and verify at my end while offline that the start menu folders and shortcuts were no longer needed before then deleting them using a batch file created from the listing.

Any ideas?  Maybe using the FOR command or something?
A small freeware utility program that could create such a listing would be a good alternative.

Question by:BillDL
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
LVL 67

Expert Comment

ID: 18868502
Hey Bill - I don't know for sure if the 98 version of for lists /r as a parameter?
If so, try this:

for /r c:\ %a in (*.lnk) do echo %a
(from a command prompt).
I'm thinking that maybe it doesn't - but worth a shot...
LVL 67

Expert Comment

ID: 18868506
Another method that might work...
tree /f /a |find /i ".lnk" >> c:\lnklist.txt
LVL 38

Author Comment

ID: 18868532
Thanks for the suggestions, sirbounty.

I would have to use the old DOS 6.22 TREE program, use Setver to set add it to the version table, and load setver at startup.  No great problem, it lists using 8.3 names, but I can use them in a batch file just the same.

Without trying either option though, it doesn't look at first glance as though they will list both the *.lnk file with a fully qualified path AND the target it points to.

I'll test them out though.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 67

Expert Comment

ID: 18869425
Nope you're right..I read that and skipped right over that requirement...sorry about that.
Well, as I'm not convinced myself that either will work for you - I'll wait for your response - if it works, it shouldn't be too much trouble to add that bit in - I was more interested in if the /r was supported..
LVL 38

Author Comment

ID: 18869991
Thanks sirbounty.  I went and forgot to test that when I was connected to the user's PC by VNC the last time.  Definitely tonight :-)

I suggested a long-winded method to gather different information (but in a similarly sorted manner) in another E-E question, and I can try that if there isn't an alternative batch only method:


Transposed to this scenario, it may work by adding your FOR command suggestion with the /R switch if it works in Win98:

@echo off
:: Create list of qualified paths to Start Menu Links
cd \
cd "C:\Windows\Start Menu\Programs"
dir /on /b /s "*.lnk" > %TEMP%\TmpList.txt
:: Add line numbers to all lines in new temp file
type %TEMP%\TmpList.txt | find /i /n "C:" > %TEMP%\tmp_01.txt
:: Walk through start menu and sequentially
:: get details of the target for each link in a
:: numbered list below the directory list in same file.
:: /R assumes current directory if not qualified
cd \
cd windows\startm~1
for /r %%a in (*.lnk) do type %%a | find /i /n "C:" >> %TEMP%\tmp_01.txt
:: Sort the list so all the 1's, 2's, etc are grouped
cd \
cd %TEMP%
sort /+2 tmp_01.txt > LinkList.txt
:: Cleanup
del %TEMP%\TmpList.txt > nul
del %TEMP%\tmp_01.txt > nul

Use Wordpad Find and replace to add commas, save as a *.csv file, load in Excel, and delete column 1 which contains the numbers.

I'll try this and let you know, unless you can see any glaring errors in my FOR command line.  I don't use the FOR command too often you see :-)

LVL 67

Expert Comment

ID: 18870002
:)  Looks good.
If that doesn't work - I'd imagine I could come up with a vbscript for this...if that's an option...
LVL 38

Author Comment

ID: 18870020
Hey, that was quick.  Are you never off E-E?
Yeah, anything's an option.  As I say, the connection is so slow that it's really aggravating manually deleting redundant start menu folders from the viewing interface.  Getting the details of the shortcuts will also give me a good idea of the leftover folders from uninstallations that are cluttering up the root and program files directories, and I can use the deltree command on them from another batch file.
LVL 67

Expert Comment

ID: 18870035
How could I be 'off' EE?  Haha
Actually I'm headed to the store, but will work on a test script for you when I get back.
Hopefully one of these will work.  With the vbs, I can output the file in any format you want...even drop it into excel, if you'd like...
So, would you like a csv/excel file for this?
Let me know the output you're looking for.
LVL 38

Author Comment

ID: 18870224
Either format would do, sirbounty, but don't spend too long creating a VBScript though.  No sense in you ruining a Saturday drinking that beer you're heading off to the store to buy.

Actually, I discovered something I hadn't been aware of.  The Win98 Dead Link Checker (chklnks.exe) displays checkboxes of dead links found, but by name only.  I hadn't realised it shows a properties dialog immediately on a right-click, and these show greyed-out File, Location, Resolves To, and Error Code fields.  I've messed with the resources of the *.exe (Resource hacker) and made the fields accessible to copy the paths from.  Still not automated, but usable at a push.
LVL 67

Accepted Solution

sirbounty earned 2000 total points
ID: 18870523
Nope - no beer this weekend. Haha

Here's the output from the following script.  I'm not sure if it'll work under 98, or if it'll have a prereq of the scripting host runtime....(been too long for me and 98, although I still have a few clients that run it).

C:\Documents and Settings\Administrator\Start Menu\Programs\Remote Assistance.lnk,C:\WINDOWS\system32\rcimlby.exe
C:\Documents and Settings\Administrator\Start Menu\Programs\Windows Media Player.lnk,C:\Program Files\Windows Media Player\wmplayer.exe
C:\Documents and Settings\Administrator\Start Menu\Programs\Accessories\Command Prompt.lnk,C:\WINDOWS\system32\cmd.exe

Note the above is 'full path to the shortcut", a comma, and the target for the shortcut....hope I didn't misunderstand the requirements.

Here's the script - it's recursive...in my test it was against C:\Documents and Settings - presumably yours would be changed to C:\Windows ?
Let me know. :^)


'' /// Setup environment declarations and object references ///
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell = CreateObject("Wscript.Shell")
Dim objFolder:Set objFolder = objFSO.GetFolder("C:\Documents and Settings\")
Dim objOutput:Set objOutput = objFSO.CreateTextFile("C:\Shortcuts.txt")

' /// Run the main routine, ProcessFolder, a recursive search through the files/folders found in the objFolder reference ///
  ProcessFolder (objFolder)

' /// Close file and destroy object references, finalizing the script ///
Set objOutput=Nothing
Set objFolder=Nothing
Set objShell=Nothing
Set objFSO=Nothing

' /// Main routine to scan current folder and recursively call against sub folders ///
Sub ProcessFolder(fld)
  Set subFld = objFSO.GetFolder(fld) ' Set object reference against current folder
  ProcessFile (subFld) 'Call Processfile routine against current folder
  For Each fld In subFld.SubFolders 'loop through all subfolders, recalling the same routine
    ProcessFolder (fld)
End Sub

' /// Routine to process all container files within the passed folder reference
Sub ProcessFile(fld)
  Set objFld = objFSO.GetFolder(fld) 'create object reference to the current folder
  For Each file In objFld.Files 'loop through all files found
  If objFSO.GetExtensionName(file) = "lnk" Then 'if the extension is 'lnk', it's a shortcut - only proceed if this returns TRUE
    Set objFile = objShell.CreateShortcut(file) 'create a shell reference to the file
    objOutput.WriteLine file & "," & objFile.TargetPath  'Write the file path and target path values to the output file
    Set objFile = Nothing 'destory object reference
  End If
End Sub
LVL 38

Author Comment

ID: 18871542
Thank you for that one sirbounty.
The VBS Script ran perfectly with changes to reference the Win98 Start Menu.  So much easier than messing with batch files.  I keep saying to myself that I should learn some VB, but never seem to get around to it.  That should have taught me a lesson.
LVL 67

Expert Comment

ID: 18872148
No need to learn it Bill - just post your needs here! :^)
Glad I could help you.
LVL 38

Author Comment

ID: 18872650
Actally, sirbounty, you commented your script well enough that I could probably use it as a basis for a few other routines without having to ask.  It's a lot easier to follow than the 16-bit C Programming I had to cover in its basics as part of a college course, in that the functions use plain English.

There's no good excuse for me not having learned useful programming  languages, because I have the legit install CD's and reference CD's for for Visual Studio 6 Enterprise, Visual Studio Express 2005, Visual J++ 6.0, Borland C++ 5 Builder, Delphi 6, and several others.  I know some people in third- world countries would give their left arm for them, and I've just been plain lazy.

I suppose part of the difficulty is knowing where to start and not having a structured learning path to follow as you would have if learning through a college course.  Until then I'll ask and know I'll get great answers like this.
LVL 67

Expert Comment

ID: 18872745
Well, you're welcome to nudge me on my email if you need help getting started.
Best of luck to you!
LVL 38

Author Comment

ID: 18873554
Thank you immensely for that offer, sirbounty.  There's obviously a fine line between what should properly be asked here and what can be asked off-site, so I would be conscious of that, but may well "nudge" you at some point for some initial advice.

In fact, watch for a question fairly soon in the VB TA.

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

771 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