topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday March 19, 2024, 2:28 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: DONE: Batch-create shortcuts from a CSV file  (Read 10699 times)

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Points: 45
  • Posts: 3,411
    • View Profile
    • Donate to Member
DONE: Batch-create shortcuts from a CSV file
« on: September 12, 2012, 07:26 PM »
Hello all,

Here is what I want to achieve. I need to create around 80-90 shortcuts. I have a CSV file that is formatted as such:

Code: Text [Select]
  1. Shortcut Title, Command

Is there a tool that can take this csv file and create shortscuts titled "Shortcut Title" and points to the "Command" executable or path?
« Last Edit: September 12, 2012, 10:25 PM by Josh »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: IDEA: Batch-create shortcuts from a CSV file
« Reply #1 on: September 12, 2012, 08:14 PM »
Hello all,

Here is what I want to achieve. I need to create around 80-90 shortcuts. I have a CSV file that is formatted as such:

Code: Text [Select]
  1. Shortcut Title, Command

Is there a tool that can take this csv file and create shortscuts titled "Shortcut Title" and points to the "Command" executable or path?

I haven't tested this, but try this AHK script...

#singleinstance,force

fileselectfile, src, ,*.csv
if errorlevel
  exitapp

loop, read, %src%
{
  stringsplit,tmp_,a_loopreadline,`,
  filecreateshortcut, %tmp_2%, %tmp_1%
}

exitapp

shortcuts should be created in the same directory as the script, though if you want to create them in a specific location that could be catered for as well

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: IDEA: Batch-create shortcuts from a CSV file
« Reply #2 on: September 12, 2012, 08:34 PM »
If "Command" is not wrapped in double quotes when containing spaces, such as a path under Program Files, I could see problems. This function might help

;returns string, wrapped in quotes when containing spaces
;updated to test if string already wrapped in double quotes
_WrapQuotes(str)
{
  wqLeftChar := ""
  wqRightChar := ""
  if (!str) || (!InStr(str," "))
    return str
  StringLeft,wqLeftChar,str,1
  StringRight,wqRightChar,str,1
  if (wqLeftChar = """") && (wqRightChar = """")
    return str
  str := """" . str . """"
  return str
}

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Points: 45
  • Posts: 3,411
    • View Profile
    • Donate to Member
Re: IDEA: Batch-create shortcuts from a CSV file
« Reply #3 on: September 12, 2012, 09:26 PM »
I appreciate the responses thus far! I must say that I am fairly proud that I just made this applet in powershell.

The intent behind this was to have a list of known control panel applets, and generate shortcuts to them. This is to aid mouser with a planned update to FARR and the way it calls the control panel applets.

I created a file named applets.txt, formatted it as such:
Code: Text [Select]
  1. Shortcut (LNK) Title, Path to control.exe, and the argument to call to execute the desired applet

Here is the code for powershell tested on Windows 8 and verified to work:
Code: Text [Select]
  1. # Create shortcuts to a known list of control panel applets from XP-Win8
  2. # File format for CSV is Shortcut Name, Path to control.exe (including control.exe in the path), Argument to call this control panel applet
  3. # Sample: Action Center, c:\windows\system32\control.exe, /name Microsoft.ActionCenter
  4.  
  5. $filename = import-csv "c:\temp\applets.txt"
  6. foreach ($i in $filename)
  7. {
  8.         $ShortcutName = $i.Name
  9.         $Target = $i.Path
  10.         $Argument = $i.Argument
  11.  
  12.         # Set the path to the shortcut LNK file
  13.         $FullSPath = "$home\Desktop\Shortcuts\" + $ShortcutName + ".lnk"
  14.  
  15.         ### Debug code to display the shortcut file name, target file name (control.exe path) and the executing argument.
  16.         # write-host "Shortcut File Name: " $FullSPath " Target File: " $Target "  Argument: " $Argument
  17.         # pause
  18.         ### End debug code
  19.  
  20.         # Establish a wscript shell variable
  21.         $wshshell = New-Object -ComObject WScript.Shell
  22.  
  23.         # Set the path to the target shortcut file (LNK)
  24.         $lnk = $wshshell.CreateShortcut($FullSPath)
  25.  
  26.         # Set the Target path to the executable file itself (control.exe)
  27.         $lnk.TargetPath = $Target
  28.        
  29.         # Set the argument (Control panel applet call)
  30.         $lnk.Arguments = $i.Argument
  31.  
  32.         # Write the shortcut
  33.         $lnk.save()
  34. }

Source file attached for shortcuts
« Last Edit: September 12, 2012, 09:35 PM by Josh »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: IDEA: Batch-create shortcuts from a CSV file
« Reply #4 on: September 12, 2012, 10:11 PM »
Glad you got it to work. :)

Is this for W8 only? Or is it meant to be portable across Windows versions? I'm not sure about PowerShell as I could only force myself to read about it for 5 minutes. Something about the syntax turned me off. But there's probably stuff you can do with it that would be more difficult with other script types.
« Last Edit: September 12, 2012, 10:18 PM by MilesAhead »

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Points: 45
  • Posts: 3,411
    • View Profile
    • Donate to Member
Re: IDEA: Batch-create shortcuts from a CSV file
« Reply #5 on: September 12, 2012, 10:24 PM »
Powershell is completely portable. You can run a PS1 script created on Windows 8 on a Windows XP system as long as all methods you call are not OS-dependent. This one is a simple WScript call, so it should work fine.

I am going to mess with yours and Target's tomorrow to see how they work. I figured I would take this as a learning opportunity but just in case I got stuck, I figured it wouldn't hurt to ask on DoCo either.

Either way, Powershell is a tool I already use and it is as powerful, if not more-so, as languages like Perl on Linux. You can access any part of the OS through the same commands. This makes automation in Windows quite easy. I use it primarily for SysAdmin level stuff, didn't think about using it for this until after I had made my initial post.

Thanks again for the quick replies!

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: DONE: Batch-create shortcuts from a CSV file
« Reply #6 on: September 12, 2012, 11:29 PM »
Thanks for the insight. I did see some stuff of interest when looking through the PS stuff a while back. I've seen many scripts use a shell object to do stuff. Only thing is I noticed they tend to be too slow for polling. They work pretty well if you have an active window and want to know who owns the window or what folder the window represents, but if you try to use the functions in a monitor that checks every window as it becomes active it tends to bog. Still, it's probably the easiest way to do desktop object stuff like shell special folders etc..

I just wish they came up with a simpler syntax. Yet another learning curve. :)