Page 1 of 1

assing an ico file to a shortcut

Posted: 24 Jan 2013 06:33
by jvuz
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

Re: assing an ico file to a shortcut

Posted: 24 Jan 2013 09:02
by DigitalSnow
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" ""

Re: assing an ico file to a shortcut

Posted: 25 Jan 2013 02:36
by abc0502
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

Re: assing an ico file to a shortcut

Posted: 25 Jan 2013 02:53
by jvuz
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.

Re: assing an ico file to a shortcut

Posted: 25 Jan 2013 03:05
by abc0502
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.