assing an ico file to a shortcut

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

assing an ico file to a shortcut

#1 Post by jvuz » 24 Jan 2013 06:33

Hi,

I have this code

Code: Select all

@echo off
setLocal EnableDelayedExpansion
set dir=c:\users

if exist "%dir%\." (goto 7) else (goto XP)

:7
pushd "C:\Users\%username%\Desktop"
type nul > %computername%
exit

:XP
pushd "C:\Documents and settings\%username%\Desktop"
type nul > %computername%
exit


It shows the shortcut, but it's without a proper icon. is there a way to assign the ico file (one I created)?

Jvuz

DigitalSnow
Posts: 20
Joined: 21 Dec 2012 13:36
Location: United States

Re: assing an ico file to a shortcut

#2 Post by DigitalSnow » 24 Jan 2013 09:02

You might want to use the %UserProfile% environment variable for simplicity.

Also, I do not know of any way to set shortcut icons using just batch.

Here is a script of mine that I use to create Shortcuts using batch and WScript. It could probably use some updates, but it works well enough for me :D

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Shortcut <xReturn> <xLink> <xTarget> [xArguments] [xDirectory] [xDescription] [xStyle] [xIcon] [xHotKey]
:: Create a Windows shortcut to the target.
:: Return true on shortcut creation, false on failure.
setlocal
 
:: Setup
set "xVBS=%Temp%\CreateShortcut.vbs"
set "xResult=false"
set "xArguments=%~4"
set "xDescription=%~n3"
set "xDirectory=%~dp3"
set "xHotKey="
set "xIcon=%~3"
set "xStyle=%~7"
 
:: Validate Required
if "%~2"=="" goto EndShortcut
if "%~3"=="" goto EndShortcut
if exist "%~2" goto EndShortcut
if not exist "%~3" goto EndShortcut
 
:: Directory Shortcut
if exist %3\* (
   set "xDirectory=%~dpf3"
   set "xIcon="
)
 
:: Set Valid Values
:: Style [Normal = 1, Maximized = 3, Minimized = 7]
:: HotKey TODO
if not "%~5"=="" if exist "%~5" set "xDirectory=%~5"
if not "%~6"=="" set "xDescription=%~6"
if not "%~7"=="1" if not "%~7"=="3" if not "%~7"=="7" set "xStyle=1"
if not "%~8"=="" if exist "%~8" set "xIcon=%~8"
if not "%~9"=="" set "xHotKey=%~9"
 
:: Create VBS
echo set oWshShell = WScript.CreateObject("WScript.Shell" ) > "%xVBS%"
echo set oShortcut = oWshShell.CreateShortcut("%~2.lnk") >> "%xVBS%"
echo oShortcut.TargetPath = "%~3" >> "%xVBS%"
if not "%xArguments%"=="" echo oShortcut.Arguments = "%xArguments%" >> "%xVBS%"
echo oShortcut.Description = "%xDescription%" >> "%xVBS%"
echo oShortcut.WorkingDirectory = "%xDirectory%" >> "%xVBS%"
echo oShortcut.WindowStyle = %xStyle% >> "%xVBS%"
if not "%xIcon%"=="" echo oShortcut.IconLocation = "%xIcon%" >> "%xVBS%"
if not "%xHotKey%"=="" echo oShortcut.HotKey = "%xHotKey%" >> "%xVBS%"
echo oShortcut.Save >> "%xVBS%"
 
:: Run VBS
if exist "%xVBS%" (
   "%xVBS%"
   if exist "%~2.lnk" set "xResult=true"
   rem pause
   del "%xVBS%"
)
 
:EndShortcut
endlocal & if not "%~1"=="" set "%~1=%xResult%"
goto :eof
:: by David Ruhmann


Here is an example call for the routine.

Code: Select all

call :Shortcut xResult "C:\User\Desktop\Shortcut" "C:\Filepath\File.exe" "-arguments" "C:\Filepath" "Description" "7" "C:\Pathtoicon\icon.ico" ""

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: assing an ico file to a shortcut

#3 Post by abc0502 » 25 Jan 2013 02:36

I remember using vbscript before to change the icon, i searched again for it and found this, Link & Link
This is a function based on a VBscript (in he first link above) to just add an Icon to an existing shortcut ".lnk file".

Code: Select all

@Echo OFF
call :icon "%userprofile%\desktop\shortcut.lnk" "%userprofile%\desktop\3D-Eagle.ico"

Pause
Exit /B

:icon <shortcut_full_path> <icon_full_path>
:: Must provide Full Path to shortcut and icon files
(
Echo.set WshShell = WScript.CreateObject^("WScript.Shell"^)
Echo.set oShellLink = WshShell.CreateShortcut^("%~1"^)
Echo.oShellLink.IconLocation = "%~2"
Echo.oShellLink.Save
)>"%temp%\ico.vbs"
"%temp%\ico.vbs"
This will add the specified icon file to the .lnk file

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: assing an ico file to a shortcut

#4 Post by jvuz » 25 Jan 2013 02:53

Thank you both for your posts. The only problem is that the shortcut doesn't contain an extension. So I'm not sure howto to do this.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: assing an ico file to a shortcut

#5 Post by abc0502 » 25 Jan 2013 03:05

It doesn't matter, the VBScript require an extension, so just when you call the function, provide a .lnk to the name of the shortcut even if it didn't have one and it will work fine.

Post Reply