VO LINK
VO LINK

Reputation: 21

Changing multiple folder icons by dropping folder as input to a batch script

I have a batch script which is used to change a folder's icon.

If [%1] == [] goto :eof
ECHO [.ShellClassInfo] >%1\desktop.in
ECHO IconResource=Example.ico,0 >>%1\desktop.in
move %1\desktop.in %1\desktop.ini
attrib +S +H %1\desktop.ini
attrib +R %1

The problem is that currently the batch file only accepts one folder dropped onto it.

Is there a way for it to accept multiple dropped folders?

Upvotes: 2

Views: 1086

Answers (2)

Hackoo
Hackoo

Reputation: 18837

You can try something like that :

@echo off
Color 0A & Mode 75,3
set "ScriptName=%~nx0"
Title Drag and Drop a folder or multi folders over "%ScriptName%"
if "%~1"=="" goto error
:loop
set "$Folder=" & pushd "%~1" 2>nul && ( popd  & set "$Folder=%~1" 
) || ( 
    set "$Folder=" && echo "%~1" is not a folder & pause
)
If Defined $Folder Call :WriteDesktopIni %$Folder%
shift
if not "%~1"=="" goto loop
echo(
echo  End of the script "%ScriptName%"
Timeout /T 3 /nobreak>nul & Exit
::***************************************************************************
:WriteDesktopIni [Folder]
if exist "%~1\desktop.ini" ( attrib -h -s -a "%~1\desktop.ini" >nul 2>&1 )
(
    ECHO [.ShellClassInfo] 
    ECHO IconResource=%systemroot%\system32\shell32.dll,47
)>"%~1\desktop.ini"
attrib +S +H +A "%~1\desktop.ini"
attrib +R "%~1"
goto :eof
::***************************************************************************
:Error
echo(
echo   You should drag and drop a folder or multi folders over "%ScriptName%"
Timeout /T 3 /nobreak>nul & exit
::***************************************************************************

Edit : CustomIconFolder.bat

In this script, you can select a folder or multi folders and your custom icon by drag and drop over the script

@echo off & Setlocal EnableDelayedExpansion
Color 0A & Mode 78,5
set "ScriptName=%~nx0"
set /a "count=0"
Title Drag and Drop a folder or multi folders over "%ScriptName%"
if "%~1"=="" goto error
for %%a in (%*) do ( 
    set /a "count+=1"
    set "$Folder=" & pushd "%%~a" 2>nul && ( popd  & set "$Folder[!count!]=%%~a"
        ) || ( 
        set "$Folder="
        Setlocal DisableDelayedExpansion
        color 0C & echo(
        echo    "%%~a"
        echo     ====^> is not a folder !
        echo     Exiting the script . . .
        endlocal
        Timeout /T 3 /nobreak>nul & exit
    )
)
Rem Dispaly selected folders
Mode 75,10
Setlocal EnableDelayedExpansion
for /L %%i in (1,1,%count%) do (
    If [%count%] EQU [1] (
        echo You have chosen this folder : 
        echo [%%i] - "!$Folder[%%i]!"
    ) else (
        echo [%%i] - "!$Folder[%%i]!"
    )
)
Timeout /T 2 /nobreak>nul
Mode 78,8 & Cls & echo(
echo Please drag and drop your custom icon to be set to your folder over here
echo and press enter...
echo(
echo Or just write the whole path and press enter ...
Set /p "Icon="
If [!Icon!] EQU [] (
    cls & echo(
    echo  The selected icon is : "%systemroot%\system32\shell32.dll,47"
    Timeout /T 3 /nobreak>nul
        for /L %%i in (1,1,%count%) do (
            echo !$Folder[%%i]!
            Call :WriteDesktopIni !$Folder[%%i]! "%systemroot%\system32\shell32.dll,47"
        )
) Else (
        for %%a in (!Icon!) do ( set "Icon_Name=%%~nxa" & set "Ext=%%~xa" )
        If /I [!Ext!] EQU [.ICO] (
            cls & echo(
            echo The selected icon is : "!Icon_Name!"
            echo From this path : !Icon!
            Timeout /T 3 /nobreak>nul
            for /L %%i in (1,1,%count%) do (
                echo "!$Folder[%%i]!"
                Copy /y !Icon! "!$Folder[%%i]!\!Icon_Name!">nul 2>&1
                Attrib +H "!$Folder[%%i]!\!Icon_Name!">nul 2>&1
                Call :WriteDesktopIni !$Folder[%%i]! "!Icon_Name!"
            )
        ) else (
            Cls & Color 0C & echo( 
            echo The extension : [*!Ext!] is not allowed
            Timeout /T 3 /nobreak>nul
        )
)
cls
echo(
echo  End of the script "%ScriptName%"
Timeout /T 2 /nobreak>nul & Exit
::***************************************************************************
:WriteDesktopIni [Folder] [Icon]
if exist "%~1\desktop.ini" ( attrib -h -s -a "%~1\desktop.ini" >nul 2>&1 )
(
    ECHO [.ShellClassInfo] 
    ECHO IconResource=%~2
)>"%~1\desktop.ini"
attrib +S +H +A "%~1\desktop.ini">nul 2>&1
attrib +R %~1>nul 2>&1
goto :eof
::***************************************************************************
:Error
Mode 86,10 & color 0B
echo( & echo(
echo   You should drag and drop a folder or multi folders over "%ScriptName%"
echo(
echo   Or Usage in command line like this syntax : 
echo(
echo   %~nx0 "FolderPath1" "FolderPath2" "FolderPath3" "FolderPath4"
Timeout /T 10 /nobreak>nul & exit
::***************************************************************************

Upvotes: 2

Stephan
Stephan

Reputation: 56180

if you drop more than one folder, they are received. %1 is just the first of them. Next would be %2 etc. There is a shift command, which shifts the parameters to the left (%1 is discarded, %2 becomes the new %1 etc.):

@echo off
:loop
  if "%~1"==""  pause & goto :eof
  echo %~1
  shift
goto :loop

Notes: use doublequoutes instead of [ and ] to correctly process folders with spaces (and avoid syntax errors with the if command.
use %~1 to remove any surrounding quotes (will be added automatically, if the folder name contains space(s).
There is a line length limitation of about 8200 chars. If you drop too many folders (exceeding the character limit), it will be cut off.

Upvotes: 3

Related Questions