Changing file icon via cmd or python script

  • Python
  • Thread starter Arman777
  • Start date
  • Tags
    File Python
In summary: How do you specify the icon that goes with a specific program?In summary, it is possible to change the icon of a file in Windows, but the process varies depending on the type of file. For executable files, the icon is embedded in the file and can be modified by editing the file itself. However, this may cause issues with integrity checks and anti-virus software. For other files, the default icon is configured in the registry and can be changed, but it is not recommended to do so through a Python script. Creating a new file extension and icon is possible, but editing the registry is not recommended as it can cause problems. Changing the registry should only be done as a last resort.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
Is it possible to change the icon of a file via python or cmd in windows ?

In my previous post I hava manage to change folder icon but I am not sure how to do that for files. Any ideas ?
 
Technology news on Phys.org
  • #2
For executable files Windows displays icons embedded in the file so you would need to modify the file. This may or may not upset integrity checks and/or anti-virus software. For other files Windows uses default icons configured in the registry so you would need to change the registry: you probably don't want to do that from a Python script.
 
  • Like
Likes Arman777
  • #3
pbuk said:
For other files Windows uses default icons configured in the registry so you would need to change the registry: you probably don't want to do that from a Python script.
I see.. so thata not possible...Editiing registery would a problem for me...
 
  • #4
Arman777 said:
I see.. so thata not possible...Editiing registery would a problem for me...
The registry can be a monster, with one item referenced several places in a ton of code. I would not try to automate any modifications of it unless you really want to open a can of worms.
 
  • Like
Likes Arman777 and pbuk
  • #5
FactChecker said:
I would not try to automate any modifications of it unless you really want to open a can of worms.
And once those worms sense either Light or Air, they trigger unlimited growth of 'side effects.' :oldruck: :cry:
 
  • Haha
Likes FactChecker
  • #6
FactChecker said:
The registry can be a monster, with one item referenced several places in a ton of code. I would not try to automate any modifications of it unless you really want to open a can of worms.
pbuk said:
For executable files Windows displays icons embedded in the file so you would need to modify the file. This may or may not upset integrity checks and/or anti-virus software. For other files Windows uses default icons configured in the registry so you would need to change the registry: you probably don't want to do that from a Python script.
What about creating a new extension such as, .xyz, and creating an icon for that special file extension. Is it possbile to that via python ? I have found this code on stack but I am sure its safe or useful

https://stackoverflow.com/questions/2331690/how-to-set-a-icon-file-while-creating-file

Code:
from _winreg import *

xyzKey = CreateKey(HKEY_CLASSES_ROOT, ".xyz")
SetValue(xyzKey, None, REG_SZ, "MyTest.xyz")
CloseKey(xyzKey)

myTestKey = CreateKey(HKEY_CLASSES_ROOT, "MyTest.xyz")
iconKey= CreateKey(myTestKey, "DefaultIcon")
CloseKey(myTestKey)

SetValue(iconKey, None, REG_SZ, "D:\\Python25\\python.exe")
CloseKey(iconKey)
but most importantly, If I create a new file extension and new icon like this, and turn this into an .exe. Is it become a problem for other users that running the program ?
 
  • #7
  1. You don't want to be messing with the registry on your live machine, you will break it.
  2. Please use code=python for your code blocks so they are readable like this:
    Python:
    from _winreg import *
  3. Having been warned about messing with the registry in this thread (and I believe warned about the danger of import * before), using from _winreg import * is insane.
    Python:
    # either use
    from _winreg import CreateKey, CloseKey, HKEY_CLASSES_ROOT, REG_SZ
    # or use
    import _winreg as reg
    # and then
    xyzKey = reg.CreateKey(reg.HKEY_CLASSES_ROOT, ".xyz")
    # etc.
  4. If you create an icon association for a file type of .xyz then it will only show for files with the extension .xyz. If you change the extension of a file to .exe then it will either show the icon that is correctly embedded in the file or the default icon for .exe files.
  5. Users can only run .exe (or .com) files, you can't run a file with the extension .xyz. Well I suppose you could associate that extension with command.exe but that would be stupid (and may not work any more in any case - I may be thinking back to Windows 3.1).
  6. If you want a custom icon for an .exe file then you simply embed it correctly in the file. Most build systems targeting Windows help you with this.
 
Last edited:
  • Like
Likes Arman777 and Vanadium 50
  • #8
pbuk said:
You don't want to be messing with the registry on your live machine, you will break it.
I see so its always a bad idea...
pbuk said:
Please use code=python for your code blocks so they are readable like this
Next time, I'll be more careful
pbuk said:
Having been warned about messing with the registry in this thread (and I believe warned about the danger of import * before), using from _winreg import * is insane.
thats reasonable..
pbuk said:
  1. If you create an icon association for a file type of .xyz then it will only show for files with the extension .xyz. If you change the extension of a file to .exe then it will either show the icon that is correctly embedded in the file or the default icon for .exe files.
  2. Users can only run .exe (or .com) files, you can't run a file with the extension .xyz. Well I suppose you could associate that extension with command.exe but that would be stupid (and may not work any more in any case - I may be thinking back to Windows 3.1).
You got me wrong. I meant something different. I have written a program called 'run_program.exe.' This program creates a new file extension called .xyz. Now my purpose was to create an icon for this .xyz program. There's no problem in creating .xyz via 'run_program.exe.' My main question was about changing 'run_program.exe' so that it generates .xyz files but also assigns it an icon.
 
  • #9
But I understand that changing the registry is a terrible idea, so you guys don't have to answer anymore..since I am not going to mess with it...
 
  • #10
Arman777 said:
I see so its always a bad idea...
Changing the registry is something you might have to do occasionally if something is not working and you have no choice. But it is very big and the same programs may have several entries scattered through the registry. IMO, it is the last resort. I am not familiar with what the registry has to do with desktop icons. I was actually surprised that they are related.
 
  • #13
FactChecker said:
IMO, it is the last resort. I am not familiar with what the registry has to do with desktop icons. I was actually surprised that they are related.
me too actually.
FactChecker said:
Changing the registry is something you might have to do occasionally if something is not working and you have no choice. But it is very big and the same programs may have several entries scattered through the registry.
Well, it's not necessary. I mean, I don't need to change the icon of the new extension...it was just for visualization...my program works fine without it. I just thought adding an icon will look nicer than a blank white icon
 
  • Like
Likes FactChecker
  • #14
Arman777 said:
That seems helpful, but I don't know how to code that.
You seem stuck on the idea that you want the EXE file to do it. I suggest that you bend to Microsoft's preference and do it the way Microsoft says.
 
  • #15
anorlunda said:
You seem stuck on the idea that you want the EXE file to do it. I suggest that you bend to Microsoft's preference and do it the way Microsoft says.
That's true. Because if someone else wants to run 'run_program.exe', they should also see the new icon. (That's also kind of problematic. In that case, their registry will also change when they run .exe)

Suppose I can somehow created a new registry for the .xyz extension via python; that's okay. Then I can convert .py to .exe via pyinstaller.

There's probably a way to do it via winreg https://docs.python.org/3/library/winreg.html, but as previously mentioned, it's best not to mess with the registry. Since I don't know how it will turn around :)
 
  • #16
The Windows registry hierarchically associates an .ico file not first with the extension, but with the application that opens files associated with that extension; however, an application can have different icons for different file extensions with which it's associated. If the application is assoiciated with multiple extensions, each of them can be associated with a different subkey, which can reference a different icon for that extension.

Most Windows applications don't do that; most of them use a single icon for the application, and that is most often in either the application's .exe file or a DLL file, rather than associated in the registry. There are numerous open source freeware programs available that can change the such a linked-in icon for an application.
 
  • Informative
Likes FactChecker
  • #17
sysprog said:
The Windows registry hierarchically associates an .ico file not first with the extension, but with the application that opens files associated with that extension; however, an application can have different icons for different file extensions with which it's associated. If the application is assoiciated with multiple extensions, each of them can be associated with a different subkey, which can reference a different icon for that extension.

Most Windows applications don't do that; most of them use a single icon for the application, and that is most often in either the application's .exe file or a DLL file, rather than associated in the registry. There are numerous open source freeware programs available that can change the such a linked-in icon for an application.
If you are talking about the icon of the .exe file, I have already done that. Pyinstaller has a built-in function that can perform such operations.

Consider a test.txt file. My program turns this into test.txt.xyz. I want to change the icon of the test.txt.xyz file after the operation, not the icon of the program that does this operation.
 

FAQ: Changing file icon via cmd or python script

How can I change a file's icon using the command prompt?

To change a file's icon using the command prompt, you can use the attrib command followed by the +s parameter and the path to the file. This will make the file a system file, allowing you to change its icon. Then, you can use the icacls command followed by the path to the file and the /seticon parameter to change the icon. For example: attrib +s "C:\Users\Username\Desktop\file.txt" and icacls "C:\Users\Username\Desktop\file.txt" /seticon "C:\path\to\icon.ico"

Can I change a file's icon using a Python script?

Yes, you can change a file's icon using a Python script. You can use the win32api and win32con modules to access the Windows API and change the file's attributes. Then, you can use the win32api.ShellExecute() function to change the file's icon. For example:

import win32api, win32con
win32api.SetFileAttributes("C:\\Users\\Username\\Desktop\\file.txt", win32con.FILE_ATTRIBUTE_SYSTEM)
win32api.ShellExecute(0, "seticon", "C:\\Users\\Username\\Desktop\\file.txt", "C:\\path\\to\\icon.ico", "", 1)

Can I change the icon of a file to a custom image?

Yes, you can change the icon of a file to a custom image. You will need to have an image file in .ico format to use as the icon. You can use an online converter or an image editing software to create an .ico file from your custom image. Then, you can follow the steps in question 1 or 2 to change the file's icon.

Will changing a file's icon affect its functionality?

No, changing a file's icon will not affect its functionality. It is purely a visual change and will not alter the file's contents or how it functions. However, if you use a Python script to change the icon, you may need to run the script with administrative privileges in order for it to work.

Can I revert a file's icon back to its original icon?

Yes, you can revert a file's icon back to its original icon. You can use the same commands mentioned in question 1 or 2, but instead of specifying a new icon file, you can use the file's original icon file path. For example: attrib +s "C:\Users\Username\Desktop\file.txt" and icacls "C:\Users\Username\Desktop\file.txt" /seticon "C:\Windows\System32\shell32.dll,5" (this is the default icon for a text file in Windows).

Similar threads

Back
Top