如何获取文件夹中图标文件的文件名以创建/更新文件夹的 desktop.ini? [英] How to get file name of an icon file in a folder for creating/updating desktop.ini for folder(s)?

查看:25
本文介绍了如何获取文件夹中图标文件的文件名以创建/更新文件夹的 desktop.ini?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助返回位于文件夹中的文件的文件名.

我已经阅读了几次被问到的其他问题,答案似乎是:

for/d %F in (*.*) do echo %~nxF

虽然这似乎对其他人有效,但当我在批处理文件中运行它时,它会出现异常并指出此时不需要'~nxF'.

我要做的是创建一个批处理文件,该文件将读取图标文件名,然后将特定信息输入 desktop.ini,最后创建具有相应权限或属性的文件.

@echo off设置 NAME=%~dp0对于 (.) 中的 %%*,请设置 NAME=%%~n*设置文件夹=%NAME%设置图标=16 24 32 48 64 128 256设置 FOLDERINI=Desktop.ini属性 +s "%CD%"如果存在 %FOLDERINI% 属性 -s -h %FOLDERINI%回声 [.ShellClassInfo] >%文件夹%echo IconResource=[视频][高清电影]\%FOLDERICO%Icon\%FOLDERICO%.ico,0 >>%文件夹%如果不是 "%2"=="" (echo FolderType=%2 >>%文件夹%)属性 -a +s +h %FOLDERINI%

我认为可以以某种方式改进代码以从根目录而不是特定文件夹运行它.

更新了我的文件,现在看起来像这样:

@ECHO 关闭属性 +s "%CD%"设置 ICODIR=%CD%Icon对于 ("%ICODIR%"*.ico) 中的 %%F,请设置 ICO=%%~nxF回声%ICO%设置 ICOINI=Desktop.ini如果存在 %ICOINI% 属性 -s -h %ICOINI%回声 [.ShellClassInfo] >%ICOINI%echo IconResource=%ICODIR:~2%%ICO%,0 >>%ICOINI%如果不是 "%2"=="" (echo FolderType=%2 >>%ICOINI%)属性 -a +s +h %ICOINI%暂停

我需要放入一个 for 循环扫描根目录的每个子目录.

解决方案

这是一个完整注释的批处理代码,用于创建或更新文件夹图标的 desktop.ini 以及可选的文件夹类型当前驱动器根目录中的指定文件夹或所有文件夹(或删除/注释单行的当前目录).

创建 desktop.ini 非常简单,通过查看代码即可看出.仅使用并非为此类任务设计的 Windows 命令处理器 cmd.exe 的内部命令,更新现有的 INI 文件以替换行或在正确的部分添加行(如有必要)要困难得多.

@echo offrem CreateDesktopIni.bat [文件夹名 |文件夹类型] [文件夹类型]rem 这个批处理文件可以在没有任何参数的情况下启动rem 更新当前驱动器根目录中所有子文件夹的 desktop.ini 或rem 当前工作目录,删除或注释其中的一行下方的 rem 代码,请参阅标签 AllFolders 下方的注释.rem 但是批处理文件也可以用文件夹名启动rem 仅创建或更新此文件夹的文件 desktop.ini.rem 可以选择仅将文件夹类型指定为参数rem 或附加为文件夹名称文件夹类型之后的第二个参数.rem 文件夹类型可以是以下字符串之一(未验证):rem CommonDocuments, Contacts, Documents, Music, MusicAlbum, MusicArtist,rem MusicIcons、MyDocuments、MyMusic、MyPictures、MyVideos、PhotoAlbum、rem 图片、UseLegacyHTT、VideoAlbum、视频setlocal EnableExtensions DisableDelayedExpansion设置文件夹类型=%~2"rem 定义包含文件夹图标的子文件夹.设置图标文件夹=图标"rem 调用的批处理文件是否至少带有一个参数?如果 "%~1" == "" 转到所有文件夹雷姆 是的!它可以是文件夹名称或文件夹类型.如果不存在 "%~1" 设置 "FolderType=%~1" &转到所有文件夹rem 第一个参数指定一个文件夹(很可能未验证).设置文件夹=%~1"rem 删除尾部反斜杠(如果有的话).rem 不应仅使用  作为文件夹路径来调用批处理文件.如果 "%Folder:~-1%" == "" 设置 "Folder=%Folder:~0,-1%"rem 调用子程序来创建或更新此文件夹的桌面文件.调用 :DesktopINI "%Folder%"转到 EndBatch:所有文件夹rem 将工作目录更改为当前驱动器的根目录.删除或评论rem 下一行来处理当前目录中的所有子文件夹.光盘
em 调用子程序为每个子文件夹创建/更新桌面文件.对于/F "eol=| delims=" %%I in ('dir/AD/B 2^>nul') 调用 :DesktopINI "%%I"转到 EndBatchrem 子程序,用于创建或更新文件夹的桌面文件.rem 这个子程序首先搜索图标文件,什么也不做rem 如果在定义的子文件夹中找不到图标文件,因为rem 子文件夹根本不存在或没有 *.ico 文件.rem 确定图标文件(首先找到的 *.ico 文件)后有完整路径rem 包括驱动器号(删除字符 d 以获得相对路径rem 驱动器号符合 %%~dpnxI),该子程序接下来检查rem 当前文件夹中存在文件 desktop.ini(不区分大小写).如果这个文件有两三行的 rem desktop.inirem 不存在且批处理文件的用户有权限rem 在当前文件夹中创建此文件.rem 对于已经存在的desktop.ini,更新它的必要过程rem 要复杂得多.节外的所有行 [.ShellClassInfo]rem 必须保留,因此只是复制到一个临时文件中,除了rem 命令 FOR 忽略的空行.也是该部分中的所有行rem [.ShellClassInfo] 不以字符串 IconFile= 开头或可选rem FolderType= (均不区分大小写)也必须通过复制简单地保存将它们复制到临时文件中.rem [.ShellClassInfo] 部分中以 IconFile= 开头的现有行rem 不会复制到临时文件,而是将这一行写入rem 具有已确定图标文件名和路径的临时文件.rem [.ShellClassInfo] 部分中以 FolderType= 开头的现有行rem 也不会复制到临时文件,而是写入此行rem 到具有在开始批处理时指定的文件夹类型的临时文件.rem 如果找到节 [.ShellClassInfo] 并且新节的开头是检测到 rem 是因为有一行以左方括号开头,并且rem 没有找到带有 IconFile= 的行和/或带有 FolderType= 的行rem 在处理现有的 desktop.ini 期间,此部分中的行rem 写在临时文件旁边以在继续之前插入它们rem 与下一节.rem 最后可能会发生 [.ShellClassInfo] 部分丢失的情况rem 现有的 desktop.ini,因此必须添加到文件中.而且它rem 可能是该部分存在于 desktop.ini 的末尾,但要么rem 带有 IconFile= 或 FolderType= 的行或两者都缺失并且因此,必须将这些行附加到文件中.rem 接下来将临时文件复制到现有的 desktop.ini 和rem 然后删除,因为不再需要.最后系统和隐藏rem 属性在文件 desktop.ini 上设置,系统属性为rem 在当前文件夹上设置,否则 desktop.ini 将被忽略.:桌面INI设置文件夹=%~1"对于 ("%Folder%\%IconFolder%*.ico") 中的 %%I 执行 (设置图标文件=%%~dpnxI"转到图标发现)转到:EOF:图标发现设置桌面文件=%Folder%desktop.ini"如果不存在 "%DesktopFile%" (echo [.ShellClassInfo]>"%DesktopFile%"如果不存在 "%DesktopFile%" 转到 :EOFecho Iconfile=%IconFile%>>"%DesktopFile%"如果定义了 FolderType echo FolderType=%FolderType%>>"%DesktopFile%"%SystemRoot%System32attrib.exe +h +s "%DesktopFile%"%SystemRoot%System32attrib.exe +s "%Folder%"转到:EOF)设置图标线="设置ShellClassInfo="设置更新完成="设置TempFile=%TEMP%Desktop.tmp"如果存在 "%TempFile%" del/F "%TempFile%"如果未定义 FolderType (设置 "TypeLine=1") 否则设置 "TypeLine="%SystemRoot%System32attrib.exe -h -s "%DesktopFile%"(对于/F "usebackq delims=" %%L in ("%DesktopFile%") 做(设置线路输出="如果定义了 ShellClassInfo (for/F "delims==" %%V in ("%%L") 做 (如果/I "%%V" == "IconFile" (回显图标文件=%图标文件%设置图标线=1"设置线路输出=1") else if/I "%%V" == "FolderType" (如果定义了 FolderType (echo FolderType=%FolderType%设置TypeLine = 1"设置线路输出=1")) 别的 (设置NewSection=1"对于/F "eol=[" %%G in ("%%V") 设置 "NewSection="如果定义了 NewSection (如果未定义 IconLine echo Iconfile=%IconFile%如果未定义 TypeLine echo FolderType=%FolderType%设置ShellClassInfo="设置更新完成=1")))) else if/I "%%L" == "[.ShellClassInfo]" (回声 [.ShellClassInfo]设置ShellClassInfo=1"设置线路输出=1")如果未定义 LineOutput echo(%%L)) >"%TempFile%"如果未定义则 UpdateComplete 如果未定义 ShellClassInfo (echo [.ShellClassInfo]>>"%TempFile%"设置ShellClassInfo=1")如果定义了 ShellClassInfo (如果未定义 IconLine echo Iconfile=%IconFile%如果未定义 TypeLine echo FolderType=%FolderType%)>>"%TempFile%"move/Y "%TempFile%" "%DesktopFile%" >nul如果存在 "%TempFile%" del "%TempFile%"%SystemRoot%System32attrib.exe +h +s "%DesktopFile%"%SystemRoot%System32attrib.exe +s "%Folder%"转到:EOF:EndBatch本地端

建议删除以命令 rem 开头的所有注释,以便更快地处理批处理文件.

如果批处理文件在启动批处理文件时应该处理当前文件夹的所有子文件夹而不是当前驱动器的子文件夹,则必须删除或注释掉命令行 cd .

DIR 选项/S 可以附加到dir/AD/B 以处理当前文件夹或当前驱动器的整个目录树在这个批处理文件的开始.

环境变量 IconFolder 可以只用 . 而不是 Icon 来定义,而是在文件夹本身中搜索第一个 *.ico 文件子文件夹的Icon.

要了解使用的命令及其工作原理,请打开 命令提示符 窗口,在此处执行以下命令,并仔细阅读每个命令显示的所有帮助页面.

  • 属性/?
  • 调用/?
  • cd/?
  • del/?
  • 目录/?
  • echo/?
  • endlocal/?
  • for/?
  • 转到/?
  • 如果/?
  • 移动/?
  • rem/?
  • 设置/?
  • setlocal/?

阅读有关 使用命令重定向操作符>>>2>nul.重定向运算符 > 必须在 FOR 命令行上使用插入字符 ^ 转义,并使用 2^>nul当 Windows 命令解释器在执行命令 FOR 之前处理此命令行时被解释为文字字符,该命令在后台启动的单独命令进程中执行嵌入的 dir 命令行.p>

I need help returning the file name of a file located in a folder.

I have read other questions asked a few times and the answer seems to be:

for /d %F in (*.*) do echo %~nxF

Although this seems to work for everyone else, when I run this within a batch file it has an exception and states that '~nxF' is not expected at this time.

What I am trying to do is create a batch file that will read the icon file name, then enter the specific information into desktop.ini and finally create that file with the respective rights or attributes.

@echo off

set NAME=%~dp0
for %%* in (.) do set NAME=%%~n*

set FOLDERICO=%NAME%
set ICONSIZES=16 24 32 48 64 128 256
set FOLDERINI=Desktop.ini

attrib +s "%CD%"

if exist %FOLDERINI% attrib -s -h %FOLDERINI%

echo [.ShellClassInfo] > %FOLDERINI%
echo IconResource=[Video][HD Films]\%FOLDERICO%Icon\%FOLDERICO%.ico,0 >> %FOLDERINI%

if not "%2"=="" (
    echo FolderType=%2 >> %FOLDERINI%
)
attrib -a +s +h %FOLDERINI%

I think the code could be improved somehow to run it from a root directory as opposed to the specific folder.

EDIT: Updated my file so it now looks like this:

@ECHO OFF

attrib +s "%CD%"
set ICODIR=%CD%Icon

for %%F in ("%ICODIR%"*.ico) do set ICO=%%~nxF
echo %ICO%

set ICOINI=Desktop.ini
if exist %ICOINI% attrib -s -h %ICOINI%

echo [.ShellClassInfo] > %ICOINI%
echo IconResource=%ICODIR:~2%%ICO%,0 >> %ICOINI%

if not "%2"=="" (
    echo FolderType=%2 >> %ICOINI%
)

attrib -a +s +h %ICOINI%

Pause

Which I need to put into a for loop scanning each subdirectory of the root.

解决方案

Here is a fully commented batch code for creating or updating desktop.ini for a folder icon and optionally also for folder type for either a specified folder or all folders in root of current drive (or current directory with deleting/commenting a single line).

Creating a desktop.ini is quite simple as it can be seen by looking on code. Updating an existing INI file to replace lines or add them if necessary in correct section is much more difficult on using only internal commands of Windows command processor cmd.exe which is not designed for such tasks.

@echo off

rem CreateDesktopIni.bat [FolderName | FolderType] [FolderType]

rem This batch file can be started without any parameter to create or
rem update desktop.ini for all subfolders in root of current drive or
rem current working directory with removing or commenting one line in
rem code below, see comment below label AllFolders.

rem But the batch file can be also started with a folder name
rem to create or update file desktop.ini of this folder only.

rem Optionally it is possible to specify as only parameter a folder type
rem or append as second parameter after folder name the folder type.

rem The folder type can be one of the following strings (not verified):

rem CommonDocuments, Contacts, Documents, Music, MusicAlbum, MusicArtist,
rem MusicIcons, MyDocuments, MyMusic, MyPictures, MyVideos, PhotoAlbum,
rem Pictures, UseLegacyHTT, VideoAlbum, Videos

setlocal EnableExtensions DisableDelayedExpansion
set "FolderType=%~2"

rem Define the subfolder containing the icon for the folder.
set "IconFolder=Icon"

rem Is the batch file called with at least one parameter?
if "%~1" == "" goto AllFolders

rem Yes! It could be a folder name or the folder type.
if not exist "%~1" set "FolderType=%~1" & goto AllFolders

rem First parameter specifies a folder (most likely as not verified).
set "Folder=%~1"
rem Remove trailing backslash if there is one.
rem The batch file should not be called with just  as folder path.
if "%Folder:~-1%" == "" set "Folder=%Folder:~0,-1%"

rem Call subroutine to create or update the desktop file for this folder.
call :DesktopINI "%Folder%"
goto EndBatch

:AllFolders
rem Change working directory to root of current drive. Remove or comment
rem the next line to process instead all subfolders in current directory.
cd 
rem Call subroutine to create/update the desktop file for each subfolder.
for /F "eol=| delims=" %%I in ('dir /AD /B 2^>nul') do call :DesktopINI "%%I"
goto EndBatch


rem Subroutine to create or update the desktop file for a folder.

rem This subroutine first searches for the icon file and does nothing
rem if no icon file could be found in the defined subfolder because
rem the subfolder does not exist at all or there is no *.ico file.

rem After determining the icon file (first found *.ico file) with full path
rem including drive letter (remove character d for relative path without
rem drive letter in line with %%~dpnxI), this subroutine checks next for
rem existence of file desktop.ini (case-insensitive) in current folder.

rem desktop.ini with the two or three lines is simply created if this file
rem does not already exist and the user of the batch file has permissions
rem to create this file in the current folder.

rem For an already existing desktop.ini the necessary process to update it
rem is much more complex. All lines outside the section [.ShellClassInfo]
rem must be kept and are therefore just copied to a temporary file, except
rem empty lines ignored by command FOR. Also all lines within the section
rem [.ShellClassInfo] not starting with the string IconFile= or optionally
rem FolderType= (both case-insensitive) must be also simply kept by copying
rem them to the temporary file.

rem An existing line starting with IconFile= in section [.ShellClassInfo]
rem is not copied to temporary file, but instead this line is written to
rem the temporary file with determined icon file name with path.

rem An existing line starting with FolderType= in section [.ShellClassInfo]
rem is also not copied to temporary file, but instead this line is written
rem to the temporary file with folder type as specified on starting batch.

rem If section [.ShellClassInfo] was found and beginning of a new section is
rem detected because of a line starting with an opening square bracket and
rem the line with IconFile= and/or the line with FolderType= was not found
rem in this section during processing the existing desktop.ini, the lines
rem are written next to temporary file to insert them before continuing
rem with the next section.

rem Finally it could happen that section [.ShellClassInfo] is missing in
rem existing desktop.ini and must be therefore added to the file. And it
rem could be that this section exists at end of desktop.ini, but either
rem the line with IconFile= or with FolderType= or both are missing and
rem those lines must be therefore appended to the file.

rem The temporary file is next copied over the existing desktop.ini and
rem then deleted as not further needed. Finally the system and hidden
rem attributes are set on file desktop.ini and the system attribute is
rem set on the current folder as otherwise desktop.ini would be ignored.

:DesktopINI
set "Folder=%~1"

for %%I in ("%Folder%\%IconFolder%*.ico") do (
    set "IconFile=%%~dpnxI"
    goto IconFound
)
goto :EOF

:IconFound
set "DesktopFile=%Folder%desktop.ini"

if not exist "%DesktopFile%" (
    echo [.ShellClassInfo]>"%DesktopFile%"
    if not exist "%DesktopFile%" goto :EOF
    echo Iconfile=%IconFile%>>"%DesktopFile%"
    if defined FolderType echo FolderType=%FolderType%>>"%DesktopFile%"
    %SystemRoot%System32attrib.exe +h +s "%DesktopFile%"
    %SystemRoot%System32attrib.exe +s "%Folder%"
    goto :EOF
)

set "IconLine="
set "ShellClassInfo="
set "UpdateComplete="
set "TempFile=%TEMP%Desktop.tmp"
if exist "%TempFile%" del /F "%TempFile%"
if not defined FolderType (set "TypeLine=1") else set "TypeLine="
%SystemRoot%System32attrib.exe -h -s "%DesktopFile%"

(for /F "usebackq delims=" %%L in ("%DesktopFile%") do (
    set "LineOutput="
    if defined ShellClassInfo (
        for /F "delims==" %%V in ("%%L") do (
            if /I "%%V" == "IconFile" (
                echo Iconfile=%IconFile%
                set "IconLine=1"
                set "LineOutput=1"
            ) else if /I "%%V" == "FolderType" (
                if defined FolderType (
                    echo FolderType=%FolderType%
                    set "TypeLine=1"
                    set "LineOutput=1"
                )
            ) else (
                set "NewSection=1"
                for /F "eol=[" %%G in ("%%V") do set "NewSection="
                if defined NewSection (
                    if not defined IconLine echo Iconfile=%IconFile%
                    if not defined TypeLine echo FolderType=%FolderType%
                    set "ShellClassInfo="
                    set "UpdateComplete=1"
                )
            )
        )
    ) else if /I "%%L" == "[.ShellClassInfo]" (
        echo [.ShellClassInfo]
        set "ShellClassInfo=1"
        set "LineOutput=1"
    )
    if not defined LineOutput echo(%%L
)) >"%TempFile%"

if not defined UpdateComplete if not defined ShellClassInfo (
    echo [.ShellClassInfo]>>"%TempFile%"
    set "ShellClassInfo=1"
)

if defined ShellClassInfo (
    if not defined IconLine echo Iconfile=%IconFile%
    if not defined TypeLine echo FolderType=%FolderType%
) >>"%TempFile%"

move /Y "%TempFile%" "%DesktopFile%" >nul
if exist "%TempFile%" del "%TempFile%"

%SystemRoot%System32attrib.exe +h +s "%DesktopFile%"
%SystemRoot%System32attrib.exe +s "%Folder%"
goto :EOF

:EndBatch
endlocal

It is advisable to remove all comments which are the lines starting with command rem for faster processing of the batch file.

The command line cd must be removed or commented out if the batch file should process all subfolders of the current folder on starting the batch file instead of the subfolders of the current drive.

The DIR option /S can be appended to dir /AD /B to process the entire directory tree of current folder or current drive on start of this batch file.

The environment variable IconFolder can be defined with just . instead of Icon to search for first *.ico file in the folder itself instead of a subfolder Icon.

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 /?
  • call /?
  • cd /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?

Read the Microsoft article about Using command redirection operators for an explanation of > and >> and 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line with 2^>nul to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.

这篇关于如何获取文件夹中图标文件的文件名以创建/更新文件夹的 desktop.ini?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆