Reputation: 135
I'm trying to create a batch file that will toggle a desktop shortcut icon from icon0 to icon1 and back again on second execute.
The Desktop Shortcut points to the batch file in Desktop/toggleicon.BAT, (Batch files is in same directory) but I'm having trouble at the first stage of changing the icon on first execute.
This is my code in the batch file:
Const DESKTOP = &H10&
Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.NameSpace(DESKTOP)
Set objFolderItem = objFolder.ParseName(“Shortcut.lnk”)
Set objShortcut = objFolderItem.GetLink
objShortcut.SetIconLocation “C:\Windows\System32\SHELL32.dll”, 13
objShortcut.Save
I imagine I'm simply googling the wrong thing.
If you know the whole code, awesome. If you can help with the current code to get the icon changing then that's great too, of course I'll mark as solution etc.
Upvotes: 5
Views: 11887
Reputation: 2951
Another, simpler way to do the same If your open to using Powershell. Save the following powershell script as MakeShortcut.ps1
Change the filepaths, extension, shortcut name, .ico filepath and name, etc to match your needs.
# Create a Shortcut with Windows PowerShell
$SourceFileLocation = "$env:userprofile\FILEPATHTOTARGETPROGRAM.EXTENSION"
$ShortcutLocation = "$env:userprofile\Desktop\SHORTCUTNAME.lnk"
#New-Object : Creates an instance of a Microsoft .NET Framework or COM object.
#-ComObject WScript.Shell: This creates an instance of the COM object that represents the WScript.Shell for invoke CreateShortCut
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $SourceFileLocation
$Shortcut.IconLocation = "$env:userprofile\FILEPATHTO.ico"
#Save the Shortcut to the TargetPath
$Shortcut.Save()
In your .bat program, call a function (testshortcut in this instance) to check if the shortcut has been made, and make it if not. Choose a location and name to store the confirmation File as / to.
Use Conditional checks within TestShortcut to Control what the icon is changed to (keep track of the state by storing a value in the ARBITRARYFILEPATH\NAME.txt file that you can check and modify within the function for your conditional checks).
:TestShortcut
IF EXIST "ARBITRARYFILEPATH\NAME.txt" GOTO :EOF
Powershell.exe -NoProfile -executionpolicy -Bypass -File FILEPATHTO\CreateShortcut.ps1
ECHO ShortcutInstalled >ARBITRARYFILEPATH\NAME.txt
GOTO :EOF
Upvotes: 3
Reputation: 16672
As mentioned in the comments, the code you have there is not a Batch File so creating a toggleicon.BAT
file will do nothing as those commands will not be interpreted and probably just error.
The code is actually VBScript which can be executed using either of the two Windows Scripting Host programs
cscript.exe
- For executing code using the Windows Console (command prompt)wscript.exe
- For executing code using the Windows GUIFollow these steps to test the code above
Rename the file with a .vbs
extension, based on your question I'd suggest toggleicon.vbs
.
Then run either of the two commands (depending on the requirement define above)
From a Console Window (cmd.exe
) or via a batch file;
cscript.exe "toggleicon.vbs"
From the Run command (❖ + R) or via a batch file;
wscript.exe "toggleicon.vbs"
Note: If not in the correct path make sure to pass the full path not just the filename.
I just don't understand why you think that is the answer, the code you provided was straight up VBScript NOT a batch file and placing that in a file with a .BAT
extension will just give an error?
This is just nonsense!
Comment by @ace-thanks
I'm making my first batch files today, and have learnt the difference between .bat (batch file) .vbs (visual basic script) and Powershell. Future readers might benefit from knowing this difference. Luckily that script is pretty straight forward enough for me to understand, and although I would mornally agree with Lankymart, Hackoo has provided a more thorough answer to my whole problem, which was outlined in the question. So Vote +1.
If you think you have learnt anything, you are sadly mistaken.
Let's just analyse the original question...
This is my code in the batch file:
Const DESKTOP = &H10 Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.NameSpace(DESKTOP) Set objFolderItem = objFolder.ParseName(“Shortcut.lnk”) Set objShortcut = objFolderItem.GetLink objShortcut.SetIconLocation “C:\Windows\System32\SHELL32.dll”, 13 objShortcut.Save
If that is your batch file then running that would give you the following error;
'objShortcut.Save' is not recognized as an internal or external command, operable program or batch file.
This is because objShortcut.Save
is VBScript syntax not Batch Command syntax.
There is a really simple way to get this example to work, just rename the file with a .VBS
extension as already mentioned and if you really need to execute from a batch write one like this;
Batch file called toggleicon.bat
@ECHO OFF
ECHO.
ECHO "Create Shortcut Example"
ECHO.
cscript.exe /nologo "toggleicon.vbs"
Just make sure both the batch file and the VBScript file are in the same directory when executed.
Upvotes: -2
Reputation: 18827
This a batch code that generate a vbscript to create a shortcut on your desktop with icon that can be changed any time when you call this sub like this way :
Call:CreateShortcut "%windir%\system32\calc.exe" "Calculatrice" "Winver.exe,0"
or to :
Call:CreateShortcut "%windir%\system32\calc.exe" "Calculatrice" "%Windir%\System32\moricons.dll,6"
The Whole Batch script to test :
@echo off
Title Create a shortcut on your Desktop with icon that can be changed any time by Hackoo
mode con cols=75 lines=3 & color 9B
Call:CreateShortcut "%windir%\system32\calc.exe" "Calculatrice" "Winver.exe,0"
echo(
echo Hit any key to change the icon shortcut....
pause>nul
Call:CreateShortcut "%windir%\system32\calc.exe" "Calculatrice" "%Windir%\System32\moricons.dll,6"
cls & color 9E
echo(
echo The icon shortcut has been changed...
echo Hit any key to change the icon shortcut....
pause>nul
Call:CreateShortcut "%windir%\system32\calc.exe" "Calculatrice" "%Windir%\System32\SHELL32.dll,13"
cls & color 9D
echo(
echo The icon shortcut has been changed...
echo Hit any key to change the icon shortcut....
pause>nul
Call:CreateShortcut "%windir%\system32\calc.exe" "Calculatrice" "%Windir%\System32\SHELL32.dll,14"
cls & color 9F
echo(
echo The icon shortcut has been changed...
pause>nul
Exit /b
::****************************************************************************************************
:CreateShortcut <ApplicationPath> <ShortcutName> <Icon>
(
echo Call Shortcut("%~1","%~2","%~3"^)
echo ^'**********************************************************************************************^)
echo Sub Shortcut(ApplicationPath,Name,Icon^)
echo Dim objShell,DesktopPath,objShortCut,MyTab
echo Set objShell = CreateObject("WScript.Shell"^)
echo MyTab = Split^(ApplicationPath,"\"^)
echo If Name = "" Then
echo Name = MyTab(UBound^(MyTab^)^)
echo End if
echo DesktopPath = objShell.SpecialFolders("Desktop"^)
echo Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& Name ^& ".lnk"^)
echo objShortCut.TargetPath = Dblquote^(ApplicationPath^)
echo ObjShortCut.IconLocation = Icon
echo objShortCut.Save
echo End Sub
echo ^'**********************************************************************************************
echo ^'Fonction pour ajouter les doubles quotes dans une variable
echo Function DblQuote(Str^)
echo DblQuote = Chr(34^) ^& Str ^& Chr^(34^)
echo End Function
echo ^'**********************************************************************************************
)> Shortcutme.vbs
Start /wait Shortcutme.vbs
Del Shortcutme.vbs
Exit /b
::****************************************************************************************************
Upvotes: 5