使用VBScript或批处理来更改快捷方式图标

时间:2021-08-22 02:07:56

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.

我正在尝试创建一个批处理文件,它将桌面快捷方式图标从icon0切换到icon1,然后在第二次执行时再切换回来。

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.

桌面快捷方式指向Desktop / toggleicon.BAT中的批处理文件,(批处理文件位于同一目录中)但我在第一次执行时更改图标的第一阶段遇到了问题。

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.

如果你知道整个代码,真棒。如果您可以帮助使用当前代码来更改图标,那么这也很棒,当然我会将其标记为解决方案等。

2 个解决方案

#1


0  

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 :

这是一个生成vbscript的批处理代码,用于在桌面上创建一个带有图标的快捷方式,当您以这种方式调用此子时,该图标可以随时更改:

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 :

要测试的Whole Batch脚本:

@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
::****************************************************************************************************

#2


-1  

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.

正如评论中所提到的,那里的代码不是批处理文件,因此创建一个toggleicon.BAT文件将不会执行任何操作,因为这些命令不会被解释,可能只是错误。

The code is actually VBScript which can be executed using either of the two Windows Scripting Host programs

代码实际上是VBScript,可以使用两个Windows Scripting Host程序中的任何一个执行

  1. cscript.exe - For executing code using the Windows Console (command prompt)
  2. cscript.exe - 使用Windows控制台执行代码(命令提示符)

  3. wscript.exe - For executing code using the Windows GUI
  4. wscript.exe - 用于使用Windows GUI执行代码

Follow these steps to test the code above

请按照以下步骤测试上面的代码

  1. Rename the file with a .vbs extension, based on your question I'd suggest toggleicon.vbs.

    根据您建议的toggleicon.vbs问题,使用.vbs扩展名重命名该文件。

  2. Then run either of the two commands (depending on the requirement define above)

    然后运行两个命令中的任何一个(取决于上面定义的要求)

    From a Console Window (cmd.exe) or via a batch file;

    从控制台窗口(cmd.exe)或批处理文件;

    cscript.exe "toggleicon.vbs"
    

    From the Run command ( + R) or via a batch file;

    从运行命令(❖+ R)或批处理文件;

    wscript.exe "toggleicon.vbs"
    

Note: If not in the correct path make sure to pass the full path not just the filename.

注意:如果不在正确的路径中,请确保传递完整路径而不仅仅是文件名。


Update

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?

我只是不明白为什么你认为这是答案,你提供的代码是直接的VBScript而不是批处理文件,并将其放在一个扩展名为.BAT的文件中只会出错?

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.

评论@ ace-thanks我今天正在制作我的第一批文件,并且已经了解了.bat(批处理文件).vbs(可视化基本脚本)和Powershell之间的区别。未来的读者可能从了解这种差异中受益。幸运的是,这个剧本非常直接让我理解,虽然我很同意Lankymart,但是Hackoo为我的整个问题提供了一个更彻底的答案,问题中概述了这个问题。所以投票+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.

'objShortcut.Save'未被识别为内部或外部命令,可操作程序或批处理文件。

This is because objShortcut.Save is VBScript syntax not Batch Command syntax.

这是因为objShortcut.Save是VBScript语法而不是Batch Command语法。

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;

有一个非常简单的方法可以让这个例子工作,只需重新命名一个带有.VBS扩展名的文件,如上所述,如果你真的需要从批处理执行这样的写操作;

Batch file called toggleicon.bat

批处理文件名为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.

只需确保批处理文件和VBScript文件在执行时都在同一目录中。

#1


0  

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 :

这是一个生成vbscript的批处理代码,用于在桌面上创建一个带有图标的快捷方式,当您以这种方式调用此子时,该图标可以随时更改:

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 :

要测试的Whole Batch脚本:

@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
::****************************************************************************************************

#2


-1  

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.

正如评论中所提到的,那里的代码不是批处理文件,因此创建一个toggleicon.BAT文件将不会执行任何操作,因为这些命令不会被解释,可能只是错误。

The code is actually VBScript which can be executed using either of the two Windows Scripting Host programs

代码实际上是VBScript,可以使用两个Windows Scripting Host程序中的任何一个执行

  1. cscript.exe - For executing code using the Windows Console (command prompt)
  2. cscript.exe - 使用Windows控制台执行代码(命令提示符)

  3. wscript.exe - For executing code using the Windows GUI
  4. wscript.exe - 用于使用Windows GUI执行代码

Follow these steps to test the code above

请按照以下步骤测试上面的代码

  1. Rename the file with a .vbs extension, based on your question I'd suggest toggleicon.vbs.

    根据您建议的toggleicon.vbs问题,使用.vbs扩展名重命名该文件。

  2. Then run either of the two commands (depending on the requirement define above)

    然后运行两个命令中的任何一个(取决于上面定义的要求)

    From a Console Window (cmd.exe) or via a batch file;

    从控制台窗口(cmd.exe)或批处理文件;

    cscript.exe "toggleicon.vbs"
    

    From the Run command ( + R) or via a batch file;

    从运行命令(❖+ R)或批处理文件;

    wscript.exe "toggleicon.vbs"
    

Note: If not in the correct path make sure to pass the full path not just the filename.

注意:如果不在正确的路径中,请确保传递完整路径而不仅仅是文件名。


Update

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?

我只是不明白为什么你认为这是答案,你提供的代码是直接的VBScript而不是批处理文件,并将其放在一个扩展名为.BAT的文件中只会出错?

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.

评论@ ace-thanks我今天正在制作我的第一批文件,并且已经了解了.bat(批处理文件).vbs(可视化基本脚本)和Powershell之间的区别。未来的读者可能从了解这种差异中受益。幸运的是,这个剧本非常直接让我理解,虽然我很同意Lankymart,但是Hackoo为我的整个问题提供了一个更彻底的答案,问题中概述了这个问题。所以投票+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.

'objShortcut.Save'未被识别为内部或外部命令,可操作程序或批处理文件。

This is because objShortcut.Save is VBScript syntax not Batch Command syntax.

这是因为objShortcut.Save是VBScript语法而不是Batch Command语法。

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;

有一个非常简单的方法可以让这个例子工作,只需重新命名一个带有.VBS扩展名的文件,如上所述,如果你真的需要从批处理执行这样的写操作;

Batch file called toggleicon.bat

批处理文件名为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.

只需确保批处理文件和VBScript文件在执行时都在同一目录中。