通过通配符递归合并文件以创建报告(批处理)
Merge files by wildcard recursively to create a report (batch)
我想遍历给定目录中的所有子目录,并将每个文件的内容(以及文件路径)写入单个报告文件。
示例:
File tree:
c:\root_dir:
dull_file.txt
subdir
relevant_file.txt
c:\root_dir\subdir:
really_relevant_file.txt
also_relevant_file.dat
输入:
C:\> make_report.bat c:\root_dir *relevant*
写入时的输出C:\root_dir> type report.txt:
c:\root_dir\relevant_file.txt
<file contents here>
c:\root_dir\subdir\really_relevant_file.txt
<file contents here>
c:\root_dir\subdir\also_relevant_file.dat
<file contents here>
到目前为止,我已经设法递归列出所有文件:
dir /s /b /a-d *.txt > file_names.txt
接下来,我需要将 file_names.txt 中的每一行的路径写入 report.txt 和 type filepath > report.txt
。我该怎么做?
您可以使用 for /r "path\to\directory" %%I in (*)
从 path\to\directory
开始递归地遍历所有文件和目录。回显完全限定路径,然后键入文件内容。将所有输出重定向到 report.txt
.
@echo off
>report.txt (
(
for /r "%~1" %%I in (%2) do (
echo %%~fI
type "%%~fI"
)
)
)
在控制台 window 中,键入 for /?
以获取有关 for /r
的更多信息。
我想遍历给定目录中的所有子目录,并将每个文件的内容(以及文件路径)写入单个报告文件。
示例:
File tree:
c:\root_dir:
dull_file.txt
subdir
relevant_file.txt
c:\root_dir\subdir:
really_relevant_file.txt
also_relevant_file.dat
输入:
C:\> make_report.bat c:\root_dir *relevant*
写入时的输出C:\root_dir> type report.txt:
c:\root_dir\relevant_file.txt
<file contents here>
c:\root_dir\subdir\really_relevant_file.txt
<file contents here>
c:\root_dir\subdir\also_relevant_file.dat
<file contents here>
到目前为止,我已经设法递归列出所有文件:
dir /s /b /a-d *.txt > file_names.txt
接下来,我需要将 file_names.txt 中的每一行的路径写入 report.txt 和 type filepath > report.txt
。我该怎么做?
您可以使用 for /r "path\to\directory" %%I in (*)
从 path\to\directory
开始递归地遍历所有文件和目录。回显完全限定路径,然后键入文件内容。将所有输出重定向到 report.txt
.
@echo off
>report.txt (
(
for /r "%~1" %%I in (%2) do (
echo %%~fI
type "%%~fI"
)
)
)
在控制台 window 中,键入 for /?
以获取有关 for /r
的更多信息。