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
- Parent2
- Child2
- Grandchild 1
- Grandchild 2
- desktop.ini
- Icon.ico
- Icon.ico
- Parent3
- Child3
- desktop.ini
- Icon.ico
- UpdateIcons.bat
The task description for UpdateIcons.bat
in folder C:\Temp\Test
.
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
.
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.
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
- Parent2
- Child2
- Grandchild 1
- Grandchild 2
- desktop.ini
- Icon.ico
- desktop.ini
- Icon.ico
- Parent3
- Child3
- 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.