49

I'd like to know what's the way to actually set the icon of a .bat file to an arbitrary icon. How would I go about doing that programmatically, independently of the language I may be using.

Cœur
  • 37,241
  • 25
  • 195
  • 267
SaM
  • 2,410
  • 1
  • 19
  • 19

11 Answers11

49

Assuming you're referring to MS-DOS batch files: as it is simply a text file with a special extension, a .bat file doesn't store an icon of its own.

You can, however, create a shortcut in the .lnk format that stores an icon.

Matt
  • 74,352
  • 26
  • 153
  • 180
Sören Kuklau
  • 19,454
  • 7
  • 52
  • 86
17

You can just create a shortcut and then right click on it -> properties -> change icon, and just browse for your desired icon. Hope this help.

To set an icon of a shortcut programmatically, see this article using SetIconLocation:

How Can I Change the Icon for an Existing Shortcut?:

https://devblogs.microsoft.com/scripting/how-can-i-change-the-icon-for-an-existing-shortcut/

Const DESKTOP = &H10&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(DESKTOP)
Set objFolderItem = objFolder.ParseName("Test Shortcut.lnk")
Set objShortcut = objFolderItem.GetLink
objShortcut.SetIconLocation "C:\Windows\System32\SHELL32.dll", 13
objShortcut.Save
XP1
  • 6,910
  • 8
  • 54
  • 61
bli
  • 179
  • 1
  • 2
12

You could use a Bat to Exe converter from here:

https://web.archive.org/web/20190304134631/http://www.f2ko.de/en/b2e.php

This will convert your batch file to an executable, then you can set the icon for the converted file.

VFDan
  • 831
  • 10
  • 26
d4rkcell
  • 137
  • 1
  • 2
  • 2
    It also allows to suppress cmd window with "Invisible application" option. – Artem P May 04 '16 at 18:46
  • That link directs me to a site where I have to allow a coinhive miner to download something. – Akerus Jan 23 '19 at 06:29
  • 2
    @Akerus try this - https://web.archive.org/web/20190304134631/http://www.f2ko.de/en/b2e.php – Gabriel H Feb 07 '20 at 08:35
  • @GabrielH Genius for using wayback machine to download a file, it works perfectly! – Ares9323 Mar 25 '20 at 04:27
  • The wayback download link seems to be dead now. Here's the download link to the source repo: https://github.com/tokyoneon/B2E/raw/master/Bat_To_Exe_Converter.zip – Prasun Biswas Dec 11 '21 at 00:05
  • A word of caution though: I just used it with a bat script that runs a simple `vlc ` command (lofi girl live stream from youtube) and the output executable was flagged as malicious by [17 antivirus programs](https://www.virustotal.com/gui/file/65fea192775f452a075fcab9b2feadf9e62faeda997d4ccb0ca3d37d901a881d/detection). I'd like to give it the benefit of doubt that they are false positives, but 17 sounds like a lot. – Prasun Biswas Dec 11 '21 at 00:37
  • Unfortunately while BAT to EXE converters would fix these issues, ANYTHING created by them will basically be flagged my most AV programs now (though a false positive) making them pretty much useless. – Enverex Sep 03 '22 at 22:09
9

The icon displayed by the Shell (Explorer) for batch files is determined by the registry key

HKCR\batfile\DefaultIcon

which, on my computer is

%SystemRoot%\System32\imageres.dll,-68

You can set this to any icon you like.

This will however change the icons of all batch files (unless they have the extension .cmd).

Joey
  • 344,408
  • 85
  • 689
  • 683
5

One of the way you can achieve this is:

  1. Create an executable Jar file
  2. Create a batch file to run the above jar and launch the desktop java application.
  3. Use Batch2Exe converter and covert to batch file to Exe.
  4. During above conversion, you can change the icon to that of your choice.(must of valid .ico file)
  5. Place the short cut for the above exe on desktop.

Now your java program can be opened in a fancy way just like any other MSWindows apps.! :)

Patrick
  • 3,490
  • 1
  • 37
  • 64
Naveen
  • 51
  • 1
  • 1
5

If you want an icon for a batch file, first create a link for the batch file as follows

Right click in window folder where you want the link select New -> Shortcut, then specify where the .bat file is.

This creates the .lnk file you wanted. Then you can specify an icon for the link, on its properties page.

Some nice icons are available here:

%SystemRoot%\System32\SHELL32.dll

Note For me on Windows 10: %SystemRoot% == C:\Windows\

More Icons are here: C:\Windows\System32\imageres.dll

Also you might want to have the first line in the batch file to be "cd .." if you stash your batch files in a bat subdirectory one level below where your shortcuts, are supposed to execute.

jmkuss
  • 136
  • 1
  • 2
2

I'll assume you are talking about Windows, right? I don't believe you can change the icon of a batch file directly. Icons are embedded in .EXE and .DLL files, or pointed to by .LNK files.

You could try to change the file association, but that approach may vary based on the version of Windows you are using. This is down with the registry in XP, but I'm not sure about Vista.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
2

Try BatToExe converter. It will convert your batch file to an executable, and allow you to set an icon for it.

Makaveli84
  • 453
  • 6
  • 16
1

try with shortcutjs.bat to create a shortcut:

call shortcutjs.bat -linkfile mybat3.lnk -target "%cd%\Ascii2All.bat" -iconlocation "%SystemRoot%\System32\SHELL32.dll,77"

you can use the -iconlocation switch to point to a icon .

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

You may use a program like BAT to EXE converter for example that one: link

enter image description here

This program permits you to add your custom icon.

Lev K.
  • 344
  • 4
  • 4
-2

i recommand to use BAT to EXE converter for your desires

  • 6
    Please have a look at [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This answer is much too short, doesn’t provide motivation for your suggestion and doesn’t show any code or resources to actually get at a solution. – Dario Mar 18 '18 at 11:39