Search code examples
batch-fileshortcut

How to create an icon for shortcut file when the icon is next to the shortcut


I want choose an icon for a shortcut file.

The file must be run in each computer, but icon file path, may be different on each.

I tried this code to change the icon properties:

"%windir%\system32\cmd.exe /c echo "%CD%\1.ico""

But it did not work!

My Screenshot Image


Solution

  • So basically, what you want to do is create a shortcut with a custom icon and path without user interaction, correct?

    We can use a VBS code to perform this task. I believe that in Windows 8 / 8.1 / 10 it is slightly different, if anything breaks just let me know, but, as I remember VBS in Windows 7:

    Set WshShell = CreateObject ("Wscript.Shell")
    strDesktop = WshShell.SpecialFolders("Desktop") \\ You can delete the code and just put like 'strDekstop = "X:\User\Desktop"' if you want.
    Set Shortcut = WshShell.CreateShortcut(strDesktop + "\ShortcutName.lnk")
    Shortcut.WindowStyle = "3" 
    Shortcut.IconLocation = "X:\YourIcon.ico"
    Shortcut.TargetPath = "X:\YourPath\OrFolder\yourarchive.exe"
    ShortCut.Hotkey = "ALT+CTRL+F" // You can just delete this line if you don't want a hotkey.
    ShortCut.Save
    

    If you want something more automated, we can turn it into a bat script with no problems!

    Shortcut maker.bat

    @echo off
    @chcp 1252 >nul
    :: By Katorn Hypno
    :: Never use quotation marks.
    :loop
    :: Configs and info above.
    
    :: Command section bellow
    
    :: Asking for directions.
    :: Don't forget the executable name and the .exe.
    set /p targetpath=Target Path+Executable: 
    :: Don't forget the .ico at the end with the icon name.
    :: You can put lnklocation and the iconlocation on top of ":loop" for a single ask.
    set /p iconlocation=Icon Location: 
    set /p lnklocation=Shortcut Location: 
    set /p lnkname=Shortcut Name: 
    
    :: Writing VBS file and creating Shortcut.
    echo Trying to write...
    set katorn=%random%
    echo >>%katorn%.vbs Set WshShell = CreateObject ("Wscript.Shell")
    echo >>%katorn%.vbs strDesktop = "%lnklocation%"
    echo >>%katorn%.vbs Set Shortcut = WshShell.CreateShortcut(strDesktop + "\%lnkname%.lnk")
    echo >>%katorn%.vbs Shortcut.WindowStyle = "3" 
    echo >>%katorn%.vbs Shortcut.IconLocation = "%iconlocation%"
    echo >>%katorn%.vbs Shortcut.TargetPath = "%targetpath%"
    echo >>%katorn%.vbs ShortCut.Save
    :: You can comment the next line if you don't want to execute the VBS files.
    cscript %katorn%.vbs
    :: You can uncommment the next line if you want to delete after executing.
    :: del /q /f %katorn%.vbs
    echo Done.
    goto loop
    

    Shortcut Reader.bat

    @echo off
    @chcp 1252 >nul
    set /p folder=VBS archives folder:
    :: Uncomment the code bellow to try every possibility.
    :: set /l %%w in (0,1,99999) do (
    :: cscript %%w.vbs
    :: )
    :: echo All trys got executed. 99999.
    for /d %%w in (%folder%\*) do (
    cscript %%w.vbs
     )
    echo Done.
    pause;
    

    Hope this helps,
    K.