Need to parse a folder of shortcuts into a text file

Monopoly money comes in many flavors: 7, Vista, XP, 2K, ME, 98, etc.

Moderators: Flying Fox, Ryu Connor

Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 10:42 am

I have a large folder consisting of nothing but hundreds of shortcuts that I need to convert to a text file listing their "Target:" and "Start in:" paths. If only one of those fields can be generated, the "Start in:" path is the more important one. Does anyone know a program, script or simple hack that can generate such a list?

Thanks,

-bd
bdwilcox
Graphmaster Gerbil
 
Posts: 1261
Joined: Mon Apr 21, 2003 12:21 pm

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 12:06 pm

You can run the following PowerShell script:

Code: Select all
$links = get-childitem * -include *.lnk
$shell = New-Object -COM WScript.Shell
foreach ($lnk in $links){
    $shortcut = $shell.CreateShortcut($lnk.FullName);
    $description = "Link: " + $lnk.FullName + "    Target: " + $shortcut.TargetPath + "    Start in: " + $shortcut.WorkingDirectory
    Add-Content $"output.txt" $description
}


You can adjust how I format $description variable to get whatever output you need

Edit: do not forget to navigate to the folder with links before pasting the code into PowerShell. Start PowerShell, run "cd C:\whatever\folder_with_links" and then paste the code.
i5 2500K@4.5GHz | Asus P8P67 PRO | 4x4GB DDR3 1600 | Radeon HD 7950 | Samsung 830 Pro 256GB| Antec Solo Black | CORSAIR 550VX | Dell 2407WFP-HC + Auria EQ276W
yuriylsh
Gerbil
 
Posts: 87
Joined: Tue May 26, 2009 3:52 pm
Location: KS, USA

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 1:23 pm

Here's a program, dunno if it does what you want.
http://www.karenware.com/powertools/ptdirprn.asp
l33t-g4m3r
Gerbil Jedi
Silver subscriber
 
 
Posts: 1971
Joined: Mon Dec 29, 2003 2:54 am

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 1:43 pm

yuriylsh,

Thank you very much for taking the time to write that script. I do have three questions, though. First, the name of the output file is always .txt without a filename. Not a big deal as I can rename it, but was curious as to why that was happening. Second, after the script runs, the input caret looks like >> and I have to hit Enter twice to get back to the normal PS caret; is that normal? And third, is there any way to list the Target: and Start in: paths on separate lines? The ideal output for a folder full of links would look something like either:

Target: C:\apps\app.exe
Start in: C:\apps
Target: H:\userfiles\working.doc
Start in: H:\userfiles
Target: J:\shares\bdwilcox\resume.doc
Start in: J:\shares\bdwilcox\

-or-

Target: C:\apps\app.exe
Target: H:\userfiles\working.doc
Target: J:\shares\bdwilcox\resume.doc
Start in: C:\apps
Start in: H:\userfiles
Start in: J:\shares\bdwilcox\

I tried stumbling around in the script and just ended up making a mess of things. Also, I tried googling different ways to modify the script and quickly became lost. Do you know of a good PowerShell primer for a non-programmer to start with? Programmers around me here use it a lot, but it's not something I've become familiar with and would like to start learning.
Last edited by bdwilcox on Wed Jul 06, 2011 1:50 pm, edited 1 time in total.
bdwilcox
Graphmaster Gerbil
 
Posts: 1261
Joined: Mon Apr 21, 2003 12:21 pm

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 1:49 pm

l33t-g4m3r wrote:Here's a program, dunno if it does what you want.
http://www.karenware.com/powertools/ptdirprn.asp

Thanks for the suggestion. Unfortunately, her program lists the directories, contents and file metadata but not the Target: and Start in: strings I was looking for. The file metadata listing is very interesting, though, and I've noted the program for future needs.
bdwilcox
Graphmaster Gerbil
 
Posts: 1261
Joined: Mon Apr 21, 2003 12:21 pm

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 2:05 pm

I found a program that does what I'm looking to do here, but it's only offered as Delphi source code and I don't have access to a Delphi compiler. :(
bdwilcox
Graphmaster Gerbil
 
Posts: 1261
Joined: Mon Apr 21, 2003 12:21 pm

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 2:19 pm

Glad I can help.
I'm not quite sure why the output file does not have a name. The "output.txt" in the 'Add-Content $"output.txt" $description' statement actually specifies the file name. You can try to specify it explicitly by adding -path instruction, so becomes 'Add-Content -path $"output.txt" $description'.
Hitting Enter twice is normal.
I'm changing the script so that it has formatting you want (the first option, it's easier):
Code: Select all
$links = get-childitem * -include *.lnk
$shell = New-Object -COM WScript.Shell
foreach ($lnk in $links){
    $shortcut = $shell.CreateShortcut($lnk.FullName);
    $description = "Target: " + $shortcut.TargetPath + "`r`nStart in: " + $shortcut.WorkingDirectory
    Add-Content -path $"output.txt" $description
}


Edit: As for learing PowerShell, Technet's documentation is surprisingly good: http://technet.microsoft.com/en-us/library/bb978525.aspx. "Getting Started" and "User Guide" are good to get familiar with the shell.
Edit2: Oh, and I almost forgot, of course check this free ebook: http://powershell.com/cs/blogs/ebook/. It was written for v1 (current is versions 2) of PowerShell, but is still very good.
i5 2500K@4.5GHz | Asus P8P67 PRO | 4x4GB DDR3 1600 | Radeon HD 7950 | Samsung 830 Pro 256GB| Antec Solo Black | CORSAIR 550VX | Dell 2407WFP-HC + Auria EQ276W
yuriylsh
Gerbil
 
Posts: 87
Joined: Tue May 26, 2009 3:52 pm
Location: KS, USA

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 2:34 pm

yuriylsh,

Thank you for the script, it works great. :D

-bdwilcox
bdwilcox
Graphmaster Gerbil
 
Posts: 1261
Joined: Mon Apr 21, 2003 12:21 pm

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 2:42 pm

Cool :D
Code: Select all
Add-Content -path output.txt $description

This should correct the output file name problem :)
i5 2500K@4.5GHz | Asus P8P67 PRO | 4x4GB DDR3 1600 | Radeon HD 7950 | Samsung 830 Pro 256GB| Antec Solo Black | CORSAIR 550VX | Dell 2407WFP-HC + Auria EQ276W
yuriylsh
Gerbil
 
Posts: 87
Joined: Tue May 26, 2009 3:52 pm
Location: KS, USA

Re: Need to parse a folder of shortcuts into a text file

Postposted on Wed Jul 06, 2011 2:57 pm

yuriylsh wrote:Cool :D
Code: Select all
Add-Content -path output.txt $description

This should correct the output file name problem :)

Yup, works like a charm, now. 8)
bdwilcox
Graphmaster Gerbil
 
Posts: 1261
Joined: Mon Apr 21, 2003 12:21 pm


Return to Windows

Who is online

Users browsing this forum: No registered users and 4 guests