Mittwoch, 21. März 2007

folderico - Setzen von Verzeichnis Icons via Batch Script

Mein erstes Blog, mein erster Blog-Eintrag. Und damit ist die erste Hürde genommen, vielleicht demnächst mal ein wenig mehr zu schreiben.

Grund für dieses erste Posting ist eigentlich die Bitte eines Kollegen das folgende Batch Skript verfügbar zu machen. Das Skript habe ich geschrieben, um per Desktop.ini einen Ordner unter Windows XP so anzupassen, dass das Icon als CD Cover der CD erscheint, deren MP3s sich in dem entsprechenden Ordner befinden.

Gewiss, es gibt den Weg über die Eigenschaften des Ordners. Dieser ist aber gerade bei vielen Alben sehr mühselig. Außerdem bin ich ein Freund von der Shell und von Skripten. Ich bin kein Experte im Schreiben von Batch-Skripten, aber ich fand eine tolle Hilfe bei Rob van der Woude's Scripting Pages.

@echo off
:REM <folderico> by Mark Michaelis aka Thragor
:REM See Reference [1] for the following trick:
verify OTHER 2>nul
setlocal ENABLEEXTENSIONS
if errorlevel 1 (
echo Unable to enable extensions.
exit /b 1
)
:REM If DELAYEDEXPANSION is enabled this script would have
:REM problems handling image files containing exclamation
:REM marks. Thus this command shall ensure that it is disabled.
setlocal DISABLEDELAYEDEXPANSION
set IMG=%1
set IMG=%IMG:"=%
if "%IMG%"=="" goto HELP
if "%IMG%"=="-h" goto HELP
if "%IMG%"=="/h" goto HELP
if "%IMG%"=="-?" goto HELP
if "%IMG%"=="-help" goto HELP
if "%IMG%"=="/help" goto HELP
:REM Windows will take any file named Folder.jpg as default
:REM file e. g. in thumbnail view
set NAME=Folder
set FOLDERIMG=%NAME%.jpg
set FOLDERICO=%NAME%.ico
set ICONSIZES=16 24 32 48 64 128
set ICONFILES=%ICONSIZES: =.png %.png
set FOLDERINI=Desktop.ini
:REM First ensure that Folder.jpg either does not exist or
:REM is writable.
if exist %FOLDERIMG% attrib -s -h %FOLDERIMG%
echo Creating %FOLDERIMG% for Thumbnail view...
convert "%IMG%" %FOLDERIMG%
if errorlevel 1 (
echo Error: Conversion of %IMG% failed.
endlocal
exit /b 1
)
echo Hiding Folder.jpg...
attrib +s +h %FOLDERIMG%
:REM Now create the different icons as png
echo Creating icons at different sizes...
for %%S in (%ICONSIZES%) do (
convert -geometry "%%S!x%%S!" "%IMG%" %%S.png
)
echo Merging PNG files to windows icon file %FOLDERICO%...
png2ico %FOLDERICO% %ICONFILES%
echo Hiding %FOLDERICO%...
attrib +s +h %FOLDERICO%
echo Deleting temporary icon files (png version)...
del %ICONFILES%
echo Make this folder a system folder...
attrib +s "%CD%"
echo Writing %FOLDERINI%...
if exist %FOLDERINI% attrib -s -h %FOLDERINI%
echo [.ShellClassInfo] > %FOLDERINI%
echo IconFile=%FOLDERICO% >> %FOLDERINI%
echo IconIndex=0 >> %FOLDERINI%
if not "%2"=="" (
echo FolderType=%2 >> %FOLDERINI%
)
attrib -a +s +h %FOLDERINI%
echo Done.
goto END
:HELP
echo.
echo Syntax: folderico ^<image file^> [^<folder type^>]
echo.
echo.
echo ^<folderico^> by Mark Michaelis aka Thragor
echo.
echo This small batch file was mainly created to set CD covers
echo as icons (and as thumbnails) for the folders their
echo mp3s reside in. Of course it might be used for
echo generally setting icons for folders.
echo.
echo In case you want to use it for CD covers you might also
echo want to set the type of the folder using the second
echo (optional) argument and set it to MusicAlbum. For
echo others see [2].
echo.
echo Requirements:
echo PNG to icon converter (Linux,GNU,Windows,Unix)
echo ImageMagick (convert) to convert and resize images
echo.
echo References:
echo Batch File Tips (Really great tips!)
echo Desktop.ini Documentation
goto END
:END
endlocal