批处理文件以将图标附加到子文件夹 [英] Batch file to attach icons to subfolders
问题描述
说明
00)我有一个带子文件夹的文件夹.
00) I have a folder with subfolders with subfolders.
01):每个父母和孩子都有一个.ico
文件.
01) Every parent and child have an .ico
file.
02).所有.ico
文件都具有完全相同的名称.
02) All .ico
files have exactly the same name.
我要做什么
我正在尝试创建一个批处理文件,将每个.ico
附加到其文件夹中
I am trying to create a batch file that would attach each .ico
to its folder
我的步骤
一开始听起来很简单,我创建了一个带有以下内容的desktop.ini文件:
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.
剩下的是
01)获取子文件夹名称.我设法使用此脚本获取包含路径的列表:
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%
但是我只想获取文件夹名称而不是完整路径.请注意,某些子文件夹的名称可能类似于"04-文件夹名称"
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)使用子文件夹(父文件夹)的列表,并使主脚本的这一部分动态化:
02) Use the list of the subfolders (parents) and make this part of the main script dynamic :
之前-> set child=PARENT1
在-> set child=%parentname%
Before --> set child=PARENT1
After --> set child=%parentname%
03)
上一步需要为每个子文件夹名称提供一个for循环.
03)
The previous step requires a for loop for each subfolder name.
因此,考虑到第一步,我想更改父级的图标,我需要一个for循环,并在获取所有父级名称后,我的脚本应该可以工作.
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.
推荐答案
有问题的批处理文件的目的对我来说是神秘的.
The purpose of batch file in question is mystic for me.
第二行CD "%cd%"
完全没有用.将当前目录更改为当前目录?该命令的目的是什么?
The second line CD "%cd%"
is completely useless. Change current directory to current directory? What should be the purpose of this command?
有3条 ECHO 命令行,其输出重定向到文件cet.txt
,如果在cet.txt
最终被删除之前根本不存在,则覆盖该文件.因此,所有带有cet.txt
的四行也完全没有用.
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.
用cet.txt
删除所有四行后,由于不再使用环境变量startdir
,也不再使用set startdir=%cd%
的第三行.
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.
然后有两个命令行:
set MYDIR=%cd%
set MYDIR1=%MYDIR:~0%
在命令提示符窗口中运行这两个命令,然后下一个命令set MYDIR
应该使第二个命令行与set MYDIR1=MYDIR
相同,这很可能在这里并不需要.
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 %%f in (%MYDIR1%) do set myfolder=%%~nxf
与
for %%f in (%CD%) do set myfolder=%%~nxf
其中,环境变量myfolder
将仅用于将其值写入文件ccc.txt
中,如果注释命令 REM 无法完全注释掉该行,则将不再使用该变量.
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.
文件夹C:\Temp\Test
包含以下文件夹和文件:
The folder C:\Temp\Test
contains following folders and files:
- 父母1
- Child1
- Icon.ico
- Parent1
- Child1
- Icon.ico
- Child2
- 孙子1
- 孙子2
- desktop.ini
- Icon.ico
- Child2
- Grandchild 1
- Grandchild 2
- desktop.ini
- Icon.ico
- Child3
- desktop.ini
- Icon.ico
文件夹
C:\Temp\Test
中UpdateIcons.bat
的任务描述.-
检查当前目录的每个子目录中是否包含文件
Icon.ico
,该文件也可以是隐藏文件.
这意味着在此示例中,检查目录Parent1
,Parent2
和Parent3
中的Icon.ico
.
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 forIcon.ico
in the directoriesParent1
,Parent2
andParent3
.
如果当前目录的子目录不不包含
Icon.ico
,则
a)从该子目录的所有子目录中删除所有Icon.ico
,
b)从该子目录及其所有子目录中删除desktop.ini
,
c)从该子目录及其所有子目录中删除系统属性.If the subdirectory of current directory does not contain
Icon.ico
then
a) delete allIcon.ico
from all subdirectories of this subdirectory,
b) deletedesktop.ini
from this subdirectory and all its subdirectories,
c) remove system attribute from this subdirectory and all its subdirectories.但是如果当前目录的子目录中有
Icon.ico
,则
a)将此子目录的Icon.ico
复制到其所有子目录,
b)(如果尚不存在,则在此子目录及其所有子目录中创建desktop.ini
,
c)在此子目录及其所有子目录上设置系统属性.But if there is
Icon.ico
in subdirectory of current directory then
a) copyIcon.ico
of this subdirectory to all its subdirectories,
b) createdesktop.ini
in this subdirectory and all its subdirectories if not already existing,
c) set system attribute on this subdirectory and all its subdirectories.以
C:\Temp\Test
作为当前目录运行C:\Temp\Test\UpdateIcons.bat
后的预期结果:The expected result after running
C:\Temp\Test\UpdateIcons.bat
withC:\Temp\Test
being the current directory:- 父母1
- Child1
- Parent1
- Child1
- Child2
- 孙子1
- desktop.ini
- Icon.ico
- Child2
- Grandchild 1
- desktop.ini
- Icon.ico
- desktop.ini
- Icon.ico
- Child3
- desktop.ini
- Icon.ico
所有
desktop.ini
和Icon.ico
应该都已隐藏并且设置了系统属性.除Parent1
和Child1
之外的所有C:\Temp\Test
目录均应设置系统属性.并且Parent1
和Child1
应该不再设置系统属性.Parent2
中的所有Icon.ico
及其子目录都应具有一些二进制数据.运行批处理文件后,Parent3
和Child3
中的所有Icon.ico
也应该相同.All
desktop.ini
andIcon.ico
should have hidden and system attribute set. All directories ofC:\Temp\Test
with exception ofParent1
andChild1
should have system attribute set. AndParent1
andChild1
should have not set anymore the system attribute. AllIcon.ico
inParent2
and its subdirectories should have some binary data. And allIcon.ico
inParent3
andChild3
should be also identical after running the batch file.对于该任务,
UpdateIcons.bat
可以包含以下命令行: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"
命令 FOR 忽略具有隐藏属性集的文件夹和文件.因此,必须将 DIR 命令与
/A-D
(仅具有任何属性的文件)或/AD
(仅具有任何属性的目录)一起使用.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).通过重定向句柄可以抑制在搜索文件夹或文件,复制图标文件或创建
desktop.ini
或更改文件夹和文件的属性(拒绝访问)时可能发生的所有错误消息. STDERR 到设备 NUL 进行整个外部循环.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 /?
attrib /?
del /?
dir /?
echo /?
endlocal /?
for /?
if /?
rem /?
set /?
setlocal /?
xcopy /?
另请参阅有关 查看全文
- Grandchild 1
- 孙子1
- Child1
- Child1