1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Creating Shortcuts with Batch File

Discussion in 'Programming & Webmastering' started by Flava0ne, Aug 31, 2014.

  1. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    I've created a Batch file which calls on the following VB Script to be able to create a shortcut to my desktop:

    VB Script (named createLink.vbs):
    Code:
    set objWSHShell = CreateObject("WScript.Shell")
    set objFso = CreateObject("Scripting.FileSystemObject")
    
    ' command line arguments
    ' TODO: error checking
    sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
    sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)
    
    set objSC = objWSHShell.CreateShortcut(sShortcut)
    
    objSC.TargetPath = sTargetPath
    objSC.WorkingDirectory = sWorkingDirectory
    
    objSC.Save
    
    Batch File Example (for Windows Notepad):
    Code:
    @echo off
    cscript createLink.vbs "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe"
    
    But after creating the shortcut using this method, I'm not able to assign a custom icon for the shortcut. I get the following error message when trying to do so:

    [​IMG]

    Any help and ideas on what I need to put into the VB Script and/or Batch File to be able to do this would be greatly appreciated! :D

    Also, I know that I can simply do this by going to Start Menu\All Programs\Accessories\ right-clicking on Notepad and selecting Send to\Desktop (create shortcut). Easy enough for creating a single shortcut. But I need to create several multiple shortcuts and using this Batch/VBS method would really save me a lot of time in doing so.
     
    Last edited: Aug 31, 2014
  2. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    Working Directory needs to match the directory of target which would be "C:\Windows\System32"

    You set the icon via: objShortcut.SetIconLocation "C:\Windows\System32\SHELL32.dll", 13

    Path is obvious, the number is the index of the icon inside the DLL, EXE, or ICO. You should be able to use Greenfish Icon Editor Pro to see the indices in an ICO file. You can use Resource Hacker to view the indicies in a DLL or EXE.
     
    Crunching for Team TPU
  3. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    How would the VB script look like with that line you mentioned added into it? I didn't create this VB Script and I'm not very familiar with using it. I'm more familiar with using Batch Command Files. I've created my very own custom icons for practically every program on my system. I usually use Restorator 2007 to edit the icons themselves in the EXE files for the programs but there are some program's that apparently won't work if doing this. So for the one's that won't work with that method, I place the icon(s) in the Pictures folder and then make it hidden after assigning it to the shortcut.
     
  4. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    Code:
    set objWSHShell = CreateObject("WScript.Shell")
    set objFso = CreateObject("Scripting.FileSystemObject")
    
    sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
    sWorkingDirectory = objFso.GetAbsolutePathName(sTargetPath)
    
    set objSC = objWSHShell.CreateShortcut(sShortcut)
    objSC.TargetPath = sTargetPath
    objSC.WorkingDirectory = sWorkingDirectory
    objShortcut.SetIconLocation(sTargetPath, 9)
    objSC.Save
    Or if you want to control the icon index from batch:
    Code:
    set objWSHShell = CreateObject("WScript.Shell")
    set objFso = CreateObject("Scripting.FileSystemObject")
    
    sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
    sWorkingDirectory = objFso.GetAbsolutePathName(sTargetPath)
    
    set objSC = objWSHShell.CreateShortcut(sShortcut)
    objSC.TargetPath = sTargetPath
    objSC.WorkingDirectory = sWorkingDirectory
    objShortcut.SetIconLocation(sTargetPath, WScript.Arguments.Item(2))
    objSC.Save
    Batch:
    Code:
    @echo off
    cscript createLink.vbs "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe" 9
     
    Last edited: Sep 1, 2014
    Crunching for Team TPU
  5. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    Tried both of the VB Scripts that you provided and adding the 9 switch to the batch file but neither was able to create a shortcut to my desktop.
     
  6. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    What was the error? Maybe it is better to move to a program to do this instead of VBS.
     
    Crunching for Team TPU
  7. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    No error, when I run the BAT file the CMD prompt shows and looks like it did stuff. But no shortcut on my Desktop.
     
  8. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    Threw a program together quick for you. It is attached with source in the other. Use it in place of your VBScript.

    It supports:
    CreateShortcut [save shortcut] [path to run]
    CreateShortcut [save shortcut] [path to run] [icon index]
    CreateShortcut [save shortcut] [path to run] [icon file]
    CreateShortcut [save shortcut] [path to run] [icon file] [icon index]

    If "icon index" is missing, it assumes 0. If "icon file" is missing it assumes "path to run." It doesn't presently support arguments to be added to the shortcut nor modifying the working directory (always assumes same directory as "path to run").

    Requires .NET Framework 4.0 to run. Will run natively on 32-bit and 64-bit versions of Windows.
     

    Attached Files:

    Last edited: Sep 1, 2014
    Wastedslayer and Flava0ne say thanks.
    Crunching for Team TPU
  9. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    So basically my Batch File will now look like this:

    Code:
    @echo off
    CreateShortcut.exe "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe"
    
    Thanks man! :D This does exactly what I want done and I'm able to customize the icon for the shortcut without problems. But I'm not quite sure how to use the [icon file] portion you mentioned that it supports. I tried the following Batch File:

    Code:
    @echo off
    CreateShortcut.exe "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe" "C:\Users\Administrator\Pictures\Notepad.ico"
    
    It created the shortcut but didn't change the icon.
     
  10. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    Are you sure Notepad.ico exists in that location? If it does, maybe you should zip it up and attach it so I can try myself and see if I can catch an error.
     
    Crunching for Team TPU
  11. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    Yes, the icon is in that location. Anyways, I zipped it up and attached it. It's just the icon itself.
     

    Attached Files:

  12. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    Hmm, I just tried it and it worked fine. Maybe try running the batch as administrator? Also, right click on the desktop after the shortcut is created and click Refresh.

    If the file in the third parameter doesn't exist, it assumes the icon is in the application.
     
    Flava0ne says thanks.
    Crunching for Team TPU
  13. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    Yeah, refreshing did it, well not exactly, more like updated it lol. Thanks man you really saved me a lot of time with this, much appreciated! :D
     
  14. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    How would I assign specific icon that's inside an EXE within the program itself?
     
    Last edited: Sep 11, 2014
  15. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    That's what the icon index is for. This one:
    CreateShortcut [save shortcut] [path to run] [icon index]

    You can use Resource Hacker to view the indices. Or you can just guess and check.
     
    Crunching for Team TPU
  16. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    So for the icon index, should I just set the path to the EXE followed by "\[x]" (x being the index of the specific icon)?
     
  17. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    No, example: CreateShortcut "C:\shortcut.lnk" "C:\program.exe" 6

    That would set icon index 6 in program.exe to the icon in shortcut.lnk. No slash and no quotes, just a number.
     
    Flava0ne says thanks.
    Crunching for Team TPU
  18. Flava0ne

    Flava0ne

    Joined:
    Mar 22, 2008
    Messages:
    493 (0.19/day)
    Thanks Received:
    13
    Location:
    192.168.0.1
    Okay gotcha! Thanks again man!
     
  19. Wastedslayer

    Wastedslayer

    Joined:
    Dec 7, 2005
    Messages:
    871 (0.25/day)
    Thanks Received:
    46
    This little program just saved me a whole heap of time with pushing out some new software in a school. Until our AD is cleaned up/organized pushing something like this out via GPO would have been a nightmare... while I still have to have my guys touch every machine, using this program allows them to just run a simple script and wait for the okay. Without this I would still be trying to make a VB script work.

    Thanks so much!
     
  20. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,918 (6.15/day)
    Thanks Received:
    4,574
    Location:
    IA, USA
    FYI, here is batch code to make Vista and newer demand admin access:
    Code:
    @echo OFF
    :: BatchGotAdmin
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        set params = %*:"=""
        echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
        exit /B
    
    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    If you need to use it, place all of the administrative batch lines after this and they'll run with admin rights. Basically, it elevates the batch to administrator access and then passes those administrator credentials to all subsequent applications.
     
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page