Reputation: 11
May I need your help now, I have been searching all over in google but exactly I didn't get really answer so I am balance to put my question here.
How to get an icon for .exe
file and save to hard drive using Batch code?
Thanks
Upvotes: 1
Views: 1720
Reputation: 24466
Expanding on wOxxOm suggested above, you can do this with a PowerShell command within a batch script like this:
@echo off
setlocal
set "exe_in=%~1"
set "ico_out=%~2"
set "psCommand="[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing');^
[Drawing.Icon]::ExtractAssociatedIcon(\"%exe_in%\").ToBitmap().Save(\"%ico_out%\")""
powershell -noprofile -noninteractive %psCommand%
Example usage:
script.bat c:\portaputty\putty.exe putty.ico
... will save putty.ico to the current working directory.
Or if you'd like to have the script save to basename.ico
without your having to specify argument 2, you can embellish a bit like this:
@echo off
setlocal
set "exe_in=%~1"
set "out_dir=."
if not "%~2"=="" 2>NUL pushd "%~2" && ( call set "out_dir=%%CD%%" & popd )
set "ico_out=%out_dir%\%~n1.ico"
set "psCommand="[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing');^
[Drawing.Icon]::ExtractAssociatedIcon(\"%exe_in%\").ToBitmap().Save(\"%ico_out%\")""
powershell -noprofile -noninteractive %psCommand%
Example usage:
script.bat c:\portaputty\putty.exe
... will save putty.ico to the current working directory.
script.bat c:\portaputty\putty.exe "%temp%"
... will save %temp%\putty.ico.
Upvotes: 3