0

DESCRIPTION

00) I have a folder with subfolders with subfolders.

enter image description here

01) Every parent and child have an .ico file.

02) All .ico files have exactly the same name.

WHAT I AM TRYING TO DO

I am trying to create a batch file that would attach each .ico to its folder

MY STEPS

In the beginning it sounded easy and I created a desktop.ini file with the following content :

[.ShellClassInfo] 
ConfirmFileOp=0 
NoSharing=1 
IconFile=Icon.ico 
IconIndex=0 
InfoTip= 

Then I created a script that does the job :

REM cd the current directory
CD "%cd%"
set startdir=%cd% 
set MYDIR=%cd%
REM it returns the last branch from the path (/../../last). This is the PARENT NAME FOLDER
set MYDIR1=%MYDIR:~0% 
for %%f in (%MYDIR1%) do set myfolder=%%~nxf
REM ECHO %myfolder% > ccc.txt
REM we need a for loop here, after taking all the folder name (not path) from subfolders
set child=CHILD4
ATTRIB +s "%child%"
CD "%child%"
set ICONCOPYDIR=%cd%\OriginalIcon.ico
REM two lines above are useless. If I remove them, the icon does not get attached
ECHO %ICONCOPYDIR% > cet.txt
ECHO %startdir% > cet.txt
COPY /Y %ICONCOPYDIR%  ".\Icon.ico"
ECHO [.ShellClassInfo] >> desktop.txt
ECHO ConfirmFileOp=0 >> desktop.txt
ECHO NoSharing=1 >> desktop.txt
ECHO IconFile=Icon.ico >> desktop.txt
ECHO IconIndex=0 >> desktop.txt
ECHO InfoTip= >> desktop.txt
CHCP 1252 >NUL
CMD.EXE /D /A /C (SET/P=ÿþ)<NUL > desktop.ini 2>NUL
CMD.EXE /D /U /C TYPE desktop.txt >> desktop.ini
DEL /F /Q desktop.txt
REM two lines above are useless. If I remove them, the icon does not get attached
ECHO %ICONCOPYDIR% > cet.txt
DEL cet.txt
ATTRIB +S +H desktop.ini Icon.ico

Most of the times, it works. If I move the folder in another directory I can see the icon attached.

WHAT IS LEFT

01) Get the subfolder names. I managed to get a list with the paths using this script :

set subfolders=DIR /s /b /o:n /ad
%subfolders%

But I only want to get the folder names and not the full path. Please note that some subfolders might have a name like this "04 - Folder Name"

02) Use the list of the subfolders (parents) and make this part of the main script dynamic :

Before --> set child=PARENT1 After --> set child=%parentname%

03) The previous step requires a for loop for each subfolder name.

So, considering that as a first step, I want to change the icons of the parents, I need a for loop and after getting all the parent-names, my script should work.

Datacrawler
  • 2,780
  • 8
  • 46
  • 100

1 Answers1

3

The purpose of batch file in question is mystic for me.

The second line CD "%cd%" is completely useless. Change current directory to current directory? What should be the purpose of this command?

There are three ECHO command lines with output redirected to file cet.txt overwriting this file in case of already existing before cet.txt is finally deleted without being used at all. So all four lines with cet.txt are also completely useless.

After removing all four lines with cet.txt also the third line with set startdir=%cd% is of no use anymore as the environment variable startdir is not used anymore.

Then there are the two command lines:

set MYDIR=%cd%
set MYDIR1=%MYDIR:~0%

Running those two commands in a command prompt window and next the command set MYDIR should make it clear that the second command line is the same as set MYDIR1=MYDIR which is most likely not really wanted here.

For that reason the command line

for %%f in (%MYDIR1%) do set myfolder=%%~nxf

is the same as

for %%f in (%CD%) do set myfolder=%%~nxf

whereby the environment variable myfolder would be just used to write its value into file ccc.txt which would be not further used if that line would not be commented out completely with remark command REM.

The remaining command lines also do not make much sense.

So I stop here describing what's wrong on provided code and suggest something completely different on an example.

The folder C:\Temp\Test contains following folders and files:

  • Parent1
    • Child1
      • Icon.ico
    • desktop.ini
  • Parent2
    • Child2
      • Grandchild 1
      • Grandchild 2
        • desktop.ini
        • Icon.ico
      • desktop.ini
      • Icon.ico
    • Icon.ico
  • Parent3
    • Child3
      • desktop.ini
      • Icon.ico
    • desktop.ini
    • Icon.ico
  • UpdateIcons.bat

The task description for UpdateIcons.bat in folder C:\Temp\Test.

  1. Check in each subdirectory of current directory if the subdirectory contains the file Icon.ico which can be also a hidden file.
    This means on this example checking for Icon.ico in the directories Parent1, Parent2 and Parent3.

  2. If the subdirectory of current directory does not contain Icon.ico then
    a) delete all Icon.ico from all subdirectories of this subdirectory,
    b) delete desktop.ini from this subdirectory and all its subdirectories,
    c) remove system attribute from this subdirectory and all its subdirectories.

  3. But if there is Icon.ico in subdirectory of current directory then
    a) copy Icon.ico of this subdirectory to all its subdirectories,
    b) create desktop.ini in this subdirectory and all its subdirectories if not already existing,
    c) set system attribute on this subdirectory and all its subdirectories.

The expected result after running C:\Temp\Test\UpdateIcons.bat with C:\Temp\Test being the current directory:

  • Parent1
    • Child1
  • Parent2
    • Child2
      • Grandchild 1
        • desktop.ini
        • Icon.ico
      • Grandchild 2
        • desktop.ini
        • Icon.ico
      • desktop.ini
      • Icon.ico
    • desktop.ini
    • Icon.ico
  • Parent3
    • Child3
      • desktop.ini
      • Icon.ico
    • desktop.ini
    • Icon.ico
  • UpdateIcons.bat

All desktop.ini and Icon.ico should have hidden and system attribute set. All directories of C:\Temp\Test with exception of Parent1 and Child1 should have system attribute set. And Parent1 and Child1 should have not set anymore the system attribute. All Icon.ico in Parent2 and its subdirectories should have some binary data. And all Icon.ico in Parent3 and Child3 should be also identical after running the batch file.

For that task UpdateIcons.bat could contain following command lines:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "IconFile=Icon.ico"

rem Environment variable CD holds path to current directory not ending with
rem a backslash except the current directory is the root of a drive. Get the
rem current directory path as base folder path always without a trailing
rem backslash. The variable BaseFolder is used only for printing the
rem currently processed folder on execution of most outer loop.

if "%CD:~-1%" == "\" ( set "BaseFolder=%CD:~0,-1%" ) else ( set "BaseFolder=%CD%" )

rem Run the commands on each subfolder in current folder which can
rem be also hidden folders. Therefore command DIR is used instead
rem of FOR /D which would ignore hidden folders.

for /F "eol=| delims=" %%I in ('dir /AD /B * 2^>nul') do (
    if not exist "%%I\%IconFile%" (

        rem Delete all desktop.ini and folder icon files from this
        rem folder and all subfolders and remove system attribute
        rem from this folder and all subfolders.

        echo No %IconFile% in folder:    "%BaseFolder%\%%I"
        %SystemRoot%\System32\attrib.exe -s "%%I"
        for /F "eol=| delims=" %%J in ('dir /A-D /B /S "%%I\desktop.ini" "%%I\%IconFile%"') do (
            %SystemRoot%\System32\attrib.exe -h -r -s "%%J"
            del "%%J"
        )
        for /F "delims=" %%J in ('dir /AD /B /S "%%I\*"') do %SystemRoot%\System32\attrib.exe -s "%%J"

    ) else (

        rem Copying this icon to all subfolders and create in this folder
        rem and all subfolders the file desktop.ini. Additionally set the
        rem system attribute on this folder and all subfolders.

        echo %IconFile% found in folder: "%BaseFolder%\%%I"
        if not exist "%%I\desktop.ini" (
            echo [.ShellClassInfo]
            echo ConfirmFileOp=0
            echo NoSharing=1
            echo IconFile=%IconFile%
            echo IconIndex=0
            echo InfoTip=
        ) >"%%I\desktop.ini"
        %SystemRoot%\System32\attrib.exe +s "%%I"
        %SystemRoot%\System32\attrib.exe -a +h -r +s "%%I\desktop.ini"
        %SystemRoot%\System32\attrib.exe -a +h -r +s "%%I\%IconFile%"
        for /F "delims=" %%J in ('dir /AD /B /S "%%I\*"') do (
            %SystemRoot%\System32\xcopy.exe "%%I\%IconFile%" "%%J\" /C /H /Q /Y >nul
            if not exist "%%J\desktop.ini" (
                echo [.ShellClassInfo]
                echo ConfirmFileOp=0
                echo NoSharing=1
                echo IconFile=%IconFile%
                echo IconIndex=0
                echo InfoTip=
            ) >"%%J\desktop.ini"
            %SystemRoot%\System32\attrib.exe +s "%%J"
            %SystemRoot%\System32\attrib.exe -a +h -r +s "%%J\desktop.ini"
            %SystemRoot%\System32\attrib.exe -a +h -r +s "%%J\%IconFile%"
        )
    )
) 2>nul
endlocal

The output of this batch file for the example is:

No Icon.ico in folder:    "C:\Temp\Test\Parent1"
Icon.ico found in folder: "C:\Temp\Test\Parent2"
Icon.ico found in folder: "C:\Temp\Test\Parent3"

The command FOR ignores folders and files with hidden attribute set. Therefore it is necessary to use command DIR with /A-D (just files with any attribute) or with /AD (just directories with any attribute).

All error messages which could occur on searching for a folder or file, or on copying the icon file, or on creating desktop.ini, or on changing the attributes of the folders and files (denied access) are suppressed by redirecting handle STDERR to device NUL for entire most outer loop.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • attrib /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • xcopy /?

See also the Microsoft article about Using command redirection operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • I wrote in the past also a smarter batch file solution for folder icon updates as answer on: [How to get file name of an icon file in a folder for creating/updating desktop.ini for folder(s)?](http://stackoverflow.com/a/27745109/3074564) – Mofi Jan 15 '17 at 16:17
  • Using my script or even FolderMarker software in Win10 (in the past I had no problems), sometimes the attachment works and sometimes it does not. Sometimes, if I wait like 2-3 minutes, I can see the icon. In other cases, I had to delete the thumbs.db file and it worked. In other cases, I just copied a random thumbs.db file in the folder and then I saw the icon. It is quite strange (windows value is low for me). I have not used the batch file having a network share. I did not run the file as an admin. Maybe a batch file is not the best solution. I could not think of anything else. – Datacrawler Jan 16 '17 at 18:32
  • Well, the folder, file and symbol caches of Windows Explorer are not controlled by batch file. It is up to Windows Explorer when it updates its caches resulting in an update of folder display. – Mofi Jan 17 '17 at 06:13
  • Do you reckon that a batch file is not the solution then? I will try to create more scripts in order to (for example) move some files like images into specific folders depending on their name. For example, image "1990 - France - Ferrari - Alain Prost" will move into the subfolder "France" within the "1990" folder. – Datacrawler Jan 17 '17 at 16:20
  • Windows Explorer recognizes changes in the directory not as good as other file managers like Total Commander in my experience. But the user has the possibility to press key F5 to reread the active directory and refresh the display. But file renames, movements, deletions, ... (standard file/folder operations) are usually well detected by Explorer. It is not the fault of the batch file that Windows Explorer for folder display does not recognized the changes on `desktop.ini`, `icon.ico` and system attributes. I don't think usage of any other scripting language makes a difference on this issue. – Mofi Jan 18 '17 at 05:53
  • In the beginning, I thought I just had to create the `desktop.ini` file and paste the icon in the folder. I clicked F5, I refreshed the Explorer from the task manager, I restarted the PC and the icon was not attached. I thought it needed a trigger (registry based??). – Datacrawler Jan 18 '17 at 11:38