Batch file to attach icons to subfolders

早过忘川 提交于 2020-04-20 17:09:48

问题


DESCRIPTION

00) I have a folder with subfolders with subfolders.

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.


回答1:


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 "delims=" %%A in ('dir /AD /B * 2^>nul') do (
    if not exist "%%A\%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%\%%A"
        %SystemRoot%\System32\attrib.exe -s "%%A"
        for /F "delims=" %%B in ('dir /A-D /B /S "%%A\desktop.ini" "%%A\%IconFile%"') do (
            %SystemRoot%\System32\attrib.exe -h -r -s "%%B"
            del "%%B"
        )
        for /F "delims=" %%B in ('dir /AD /B /S "%%A\*"') do %SystemRoot%\System32\attrib.exe -s "%%B"

    ) 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%\%%A"
        if not exist "%%A\desktop.ini" (
            echo [.ShellClassInfo]
            echo ConfirmFileOp=0
            echo NoSharing=1
            echo IconFile=%IconFile%
            echo IconIndex=0
            echo InfoTip=
        ) >"%%A\desktop.ini"
        %SystemRoot%\System32\attrib.exe +s "%%A"
        %SystemRoot%\System32\attrib.exe -a +h -r +s "%%A\desktop.ini"
        %SystemRoot%\System32\attrib.exe -a +h -r +s "%%A\%IconFile%"
        for /F "delims=" %%B in ('dir /AD /B /S "%%A\*"') do (
            %SystemRoot%\System32\xcopy.exe "%%A\%IconFile%" "%%B\" /C /H /Q /Y >nul
            if not exist "%%B\desktop.ini" (
                echo [.ShellClassInfo]
                echo ConfirmFileOp=0
                echo NoSharing=1
                echo IconFile=%IconFile%
                echo IconIndex=0
                echo InfoTip=
            ) >"%%B\desktop.ini"
            %SystemRoot%\System32\attrib.exe +s "%%B"
            %SystemRoot%\System32\attrib.exe -a +h -r +s "%%B\desktop.ini"
            %SystemRoot%\System32\attrib.exe -a +h -r +s "%%B\%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.



来源:https://stackoverflow.com/questions/41660866/batch-file-to-attach-icons-to-subfolders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!