在目录/子目录中生成文件文件夹 list.txt 文件并在批处理文件中使用 dir 和 ren 命令将 list.txt 重命名为文件夹/子文件夹名称

Posted

技术标签:

【中文标题】在目录/子目录中生成文件文件夹 list.txt 文件并在批处理文件中使用 dir 和 ren 命令将 list.txt 重命名为文件夹/子文件夹名称【英文标题】:Generate file-folder list.txt file in a directory/subdirectory & rename list.txt to folder/subfolder name using dir & ren command in a batch file 【发布时间】:2017-03-13 10:49:32 【问题描述】:

我正在尝试:

    在文本文件的子文件夹中列出每个文件夹的内容, 将文本文件放在父文件夹和子文件夹中, 将输出文本文件重命名为父文件夹/子文件夹的名称。

为了实现这一点,我尝试了以下批处理脚本

del /s __List.txt
for /F "delims=" %%G IN ('dir /b /s') DO @echo "%%G">>"%%~__List.txt"
for /r %%a in (__List.txt) do for %%b in ("%%~dpa\.") do ren "%%~a" "%%~nxb%%~xa"
pause

现在

    我可以列出每个文件夹的文件, __List.txt 正在创建中, __List.txt 正在重命名为子文件夹。

问题是:

    未打印空文件夹。 如果任何目录已经有“目录/子目录名称.txt”,则会出现此错误

    存在重复的文件名或找不到文件

    在控制台窗口中显示错误。 (首选方法可能是创建错误日志并将其放在父文件夹中。)但是它是可选的。

由于部分查询单独发布在其中,因此可以在回答时参考以下内容:

    .bat rename files in folders/sub-folders to specific name Batch Files: List all files in a directory, print as .txt and place output file in all sub directories Batch File - Rename files based on parent name and (sub)folder(s) name

文件夹结构示例:

父文件夹 子文件夹-01 __filelist.txt使用命令创建内容列表 dir 并转换为 Sub Folder-01.txt。 一些数据文件 1.xyz 一些数据文件 2.xyz 一些数据文件 3.xyz 子文件夹-02-空 子子文件夹-01 __filelist.txt可能的原因“文件已存在”错误。 一些数据文件_A.xyz 一些数据文件_B.xyz 一些数据文件_C.xyz __filelist.txt由于文件夹空白,未生成。 “找不到文件”错误的可能原因。 batch_file.bat __filelist.txt some-file.xyz

可能需要双重解决方案

    命令 dir 命令应该生成filelist.txt 即使文件夹是空的,它也会解决“找不到文件”错误。

    命令 ren 应按递增顺序覆盖现有的 filelist.txt 或将现有的 filelist.txt 重命名为 filelist1-100.txt。它可能会解决“文件已存在”错误。

【问题讨论】:

@Mofi,我能说什么......你的回答太棒了。正是我想要达到的目标。您已经很好地解释了这使我能够轻松地进行自定义和修改。非常感谢。 (一个愚蠢的问题)顺便说一句,接受这个答案的绿色复选标记按钮在哪里? @mofi 刚刚遇到一个文件夹中的脚本问题,该文件夹中存在大量文件,显示“系统找不到指定的路径。”。它在文件数量较少的文件夹中运行良好。 where lacs of files present 是什么意思?我不明白这句话。注意:文件夹路径的最大长度在 Windows 命令进程中被限制为 MAX_PATH (260)。更多详情请见Why does the 260 character path length limit exist in Windows?您是否因为文件夹树中的文件夹名称过多或过长而遇到此限制? 【参考方案1】:

试试这个带注释的批处理文件:

@echo off
setlocal
if "%~1" == "" goto UseCD

rem The start folder is either the current folder on running the batch file
rem or the folder specified as first argument on starting the batch file.
rem All / (Unix/Mac) in specified folder path are replaced by \ (Windows).
rem The folder path must not end with a backslash for this task.

set "StartFolder=%~1"
set "StartFolder=%StartFolder:/=\%"
if "%StartFolder:~-1%" == "\" set "StartFolder=%StartFolder:~0,-1%"
if exist "%StartFolder%\" goto CreateLists

rem The environment variable CD (Current Directory) holds the entire path
rem to the current folder ending not with a backslash, except the current
rem folder is the root folder of a drive.

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

rem The error log file in start folder existing perhaps from a previous
rem run is deleted first before the list file is created recursively in
rem each folder of the start folder.

:CreateLists
set "ErrorLogFile=%StartFolder%\Error.log"
%SystemRoot%\System32\attrib.exe -h -r -s "%ErrorLogFile%" >nul
del "%ErrorLogFile%" 2>nul

call :RecursiveList "%StartFolder%"

endlocal

rem Avoid an unwanted fall through to the subroutine.
goto :EOF


rem RecursiveList is a subroutine called for each folder found
rem in the start folder and its subfolders.

rem For the root folder of a drive like C: the list file name is "DriveC.txt".
rem For all other folders the list file name is "Folder Name.txt".

rem The command DEL does not delete a file which has hidden, read-only or
rem system attribute set. For that reason ATTRIB is called first to remove
rem those attributes from a perhaps already existing list file in current
rem folder. ATTRIB outputs an error message because of not existing file
rem to handle STDOUT which is the reason for >nul which redirects this
rem not important error message to device NUL.

rem Next the list file is deleted with suppressing the error message output
rem by command DIR to handle STDERR with 2>nul if the file does not exist
rem at all. But in case of the file really exists and could not be deleted
rem (NTFS permissions, file access denied because file is opened in another
rem application), an error message is logged into error log file in start
rem folder which is hopefully not write-protected, too.

rem Creating a list file in a folder is skipped if there is already
rem a list file and it could not be deleted by command DEL.

rem Otherwise the command DIR is used to write first the names of the
rem subfolders in alphabetical order according to name (not alphanumeric)
rem into the list file of current folder and next append the names of all
rem files in current folder also ordered by name. The name of the list file
rem is included in list file. Comment the two lines with command DIR and
rem uncomment the 3 lines below to avoid this by first writing the folder
rem and files names into a list file in temporary files folder and then
rem move this list file with correct list file name to the current folder.

rem Last for each subfolder in current folder the subroutine RecursiveList
rem calls itself until all subfolders in current folder have been processed.

:RecursiveList
set "FolderPath=%~1"
if "%FolderPath:~2%" == "" (
    set "ListFileName=Drive%FolderPath:~0,1%.txt"
) else (
    set "ListFileName=%~nx1.txt"
)

%SystemRoot%\System32\attrib.exe -h -r -s "%FolderPath%\%ListFileName%" >nul
del "%FolderPath%\%ListFileName%" >nul 2>&1
if exist "%FolderPath%\%ListFileName%" (
    echo Failed to update: "%FolderPath%\%ListFileName%">>"%ErrorLogFile%"
    goto ProcessSubFolders
)

dir /AD /B /ON "%FolderPath%">"%FolderPath%\%ListFileName%" 2>nul
dir /A-D /B /ON "%FolderPath%">>"%FolderPath%\%ListFileName%" 2>nul

rem dir /AD /B /ON "%FolderPath%">"%TEMP%\%~n0.tmp" 2>nul
rem dir /A-D /B /ON "%FolderPath%">>"%TEMP%\%~n0.tmp" 2>nul
rem move "%TEMP%\%~n0.tmp" "%FolderPath%\%ListFileName%"

:ProcessSubFolders
for /D %%I in ("%FolderPath%\*") do call :RecursiveList "%%~I"
goto :EOF

rem The command above exits the subroutine RecursiveList. The batch
rem file processing is continued in previous routine which is again
rem the subroutine RecursiveList or finally the main batch code above.

批处理文件仅将子文件夹的名称和该文件夹中的文件写入每个文件夹列表文件。

例如Sub Folder-02-Empty.txt 只包含

Sub-Sub Folder-01
Sub Folder-02-Empty.txt

对于给定的示例,Sub-Sub Folder-01.txt 包含:

__filelist.txt
some-Data-files_A.xyz
some-Data-files_B.xyz
some-Data-files_C.xyz
Sub-Sub Folder-01.txt

如果列表文件的文件名不应包含在列表文件中,请阅读 cmets 以了解如何排除列表文件。

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

attrib /? call /? del /? dir /? echo /? endlocal /? for /? goto /? if /? rem /? set /? setlocal /?

【讨论】:

以上是关于在目录/子目录中生成文件文件夹 list.txt 文件并在批处理文件中使用 dir 和 ren 命令将 list.txt 重命名为文件夹/子文件夹名称的主要内容,如果未能解决你的问题,请参考以下文章

如何在 dist 目录中生成单独的 package.json 文件?

在spring boot中生成密码加密的excel文件时没有遇到此类文件或目录错误

执行在 Oracle 中生成 csv 文件的过程时目录路径无效(在 Windows 中)

新建以自己学号为名字的目录,并在此目录中生成(学号%1000)个文件,并证明生成了相应个文件。

Excel VBA:在 Word 文件中生成页脚

Maven 在现有目录中生成原型