[英]Loop through folders in subdirectories and combine text files
我想遍历子目录中的文件夹,并将所有文本文件合并为一个文件。 我在网上找到了一些答案,但似乎都没有用。 任何帮助深表感谢。 我提供了以下内容。 在下面的示例中, DummyFolder
具有多个子目录,这些子目录包含需要合并为1个文件的.txt files
文件。 我昨天获得了代码3的支持,但是以某种方式我做了一些更改,并且由于某种原因它不再起作用。
代码1:
@echo off
set "header=C:\Users\user\Desktop\DummyFolder\Headings.txt"
set "folder=C:\Users\user\Desktop\DummyFolder\"
set "tempFile=%folder%\temp.txt"
for %%F in ("%folder%\*.txt") do (
type "%header%" >"%tempFile%"
type "%%F" >>"%tempFile%"
move /y "%tempFile%" "%%F" >nul
)
还发现此代码(代码2):
$startingDir = 'C:\Users\user\Desktop\DummyFolder\'
$combinedDir = 'C:\Users\user\Desktop\DummyFolder\CombinedTextFiles'
Get-ChildItem $startingDir -Recurse | Where-Object {
$txtfiles = Join-Path $_.FullName '*.txt'
$_.PSIsContainer -and (Test-Path $txtfiles)
} | ForEach-Object {
$merged = Join-Path $combinedDir ($_.Name + '_Merged.txt')
Get-Content $txtfiles | Set-Content $merged
}
还找到了以下代码(代码3):
@echo on
set folder="C:\Users\user\Desktop\DummyFolder\"
for /F %%a in ('dir /b /s %folder%') do (
if "%%~xa" == ".txt" (
(echo/------------------------------
type %%~a
echo/)>>"%~dp0list.txt"
)
)
在CMD中,您将执行以下操作:
@echo off
set "basedir=C:\some\folder"
set "outfile=C:\path\to\output.txt"
(for /r "%basedir%" %f in (*.txt) do type "%~ff") > "%outfile%"
要在批处理文件中使用,您需要将%f
更改为%%f
,将%~ff
更改为%%~ff
。
在PowerShell中,您可以执行以下操作:
$basedir = 'C:\some\folder'
$outfile = 'C:\path\to\output.txt'
Get-ChildItem $basedir -Include *.txt -Recurse | Get-Content |
Set-Content $outfile
代码3不错,但是它不能与路径中的空格一起使用,因为您使用的是标准delims
因为您没有提供。 此外,还有其他关于在路径中使用空格的错误。
以下代码可以正常工作并将所有子目录中的所有txt files
合并在一起。 它将在该批处理文件所在的文件夹中创建一个新文件list.txt
。 如果已经存在一个list.txt
,它将被覆盖。 请注意,这是一个批处理文件:
@echo off
set "folder=C:\Users\user\Desktop\DummyFolder\"
rem create new empty file: list.txt in directory of batch file: %~dp0
break>"%~dp0list.txt"
rem loop through all output lines of the dir command, unset delimns
rem so that space will not separate
for /F "delims=" %%a in ('dir /b /s "%folder%"') do (
rem just look for txt files
if "%%~xa" == ".txt" (
rem don't use the list.txt
if not "%%a" == "%~dp0list.txt" (
rem append the output of the whole block into the file
(echo/------------------------------
type "%%a"
echo/)>>"%~dp0list.txt"
)
)
)
如果您不了解某些内容,则可以在Internet上找到不错的内容,这很容易,因为这里有许多出色的批处理脚本站点。 此外,您始终可以使用echo This is a message visible on the command prompt
用于显示可能有用的内容(例如变量等)。通过该操作,您可以“调试”并查看发生的情况。 代码中除注释( rem This is a comment
)之外的一些解释:
1. break
命令:
要清除文件,我使用break命令,该命令根本不会产生任何输出。 空的输出我重定向到文件,请在此处阅读: https : //stackoverflow.com/a/19633987/8051589 。
2.常规变量:
您可以通过set varname=Content
设置变量。我更喜欢用引号来设置变量: set "varname=Content"
因为它也适用于重定向字符。 将变量以一个开头%
和一个结尾%
例如echo %varname%
。 您可以在https://ss64.com/nt/set.html上阅读很多内容。 我认为ss64可能是在那里进行批处理脚本的最佳站点。
3.重定向>
和>>
:
您可以使用>
或>>
重定向命令的输出,其中>
创建一个新文件并覆盖现有文件,并将>>
追加到文件中,或者创建一个(如果不存在)。 还有更多可能: https : //ss64.com/nt/syntax-redirection.html 。
4. for /f
循环:
在批处理文件中,您可以使用for /f
循环遍历命令输出的各行。 使用的变量将在其前面写入2 %
,这里是%%a
。 我还将定界符delimns
设置为delimns
,以便命令输出不会分成多个标记。
您可以在以下位置阅读有关for /f
循环的许多详细信息: https : //ss64.com/nt/for_cmd.html 。
5.特殊变量语法%%~xa
%~dp0
和%~dp0
:
包含dir
命令一行的变量%%a
只能通过以下方式扩展为文件扩展名: %%~xa
,如此处所述: https : //stackoverflow.com/a/5034119/8051589 。 %~dp0
变量包含批处理文件所在的路径,请参见此处: https : %~dp0
。
6.块重定向( ... )>>
:
要一次重定向多个命令,您可以打开一个块(
,执行命令,关闭该块)
并使用重定向。 您还可以执行每个命令,并重定向仅具有相同效果的命令。
有很多方法可以做到这一点。 例如,使用Wolfram语言,您可以:
StringJoin @@
FileSystemMap[
If[FileExtension[#] == "txt", Import[#, "Text"]] &,
"C:\\Users\\user\\Desktop\\DummyFolder\\", Infinity, 1]
然后使用写结果
Export[C:\\Users\\user\\Desktop\\, %, "Text"]
您也可以使用Python,Perl等执行此操作。仅在需要共享解决方案并且希望避免安装程序时才使用PowerShell。 我不会花太多时间学习1981年技术(CMD)。
这可能是您要查找的内容的简单答案,usebackq对于允许在路径中使用“”非常重要。 tokens = *以包含所有信息。 要在控制台中使用而不是批处理文件,请将%%更改为%。
for /f "tokens=*" %%a in ('dir /s /b C:\testpath\*.txt') do (for /f "usebackq tokens=*" %%b in ("%%a") do (echo %%b >> C:\test.txt))
假设你的源文件都位于根目录的直接子目录DummyFolder
和想要的内容Headings.txt
发生一次仅在生成的文件的顶部,你可以使用下面的脚本完成你的任务:
@echo off
rem // Define constants here:
set "folder=C:\Users\user\Desktop\DummyFolder"
set "header=%folder%\Headings.txt"
set "result=%folder%\merged.txt"
rem // Prepare result file, copy content of header file:
copy "%header%" "%result%" > nul
rem // Enumerate immediate sub-directories of the given root directory:
for /D %%D in ("%folder%\*") do (
rem // Enumerate matching files per sub-directory:
for %%F in ("%%~D\*.txt") do (
rem // Append content of current file to result file:
copy /Y "%result%" + "%%~F" "%result%" /B > nul
)
)
如果源文件位于目录树DummyFolder
中的任何位置,则需要确保头文件Headings.txt
和结果文件merged.txt
没有被迭代:
@echo off
rem // Define constants here:
set "folder=C:\Users\user\Desktop\DummyFolder"
set "header=Headings.txt"
set "result=merged.txt"
rem // Prepare result file, copy content of header file:
copy "%folder%\%header%" "%folder%\%result%" > nul
rem // Enumerate matching files in the whole given directory tree:
for /R "%folder%" %%F in ("*.txt") do (
rem // Exclude the header file to be re-processed:
if /I not "%%~nxF"=="%header%" (
rem // Exclude the result file to be processed:
if /I not "%%~nxF"=="%result%" (
rem // Append content of current file to result file:
copy /Y "%folder%\%result%" + "%%~F" "%folder%\%result%" /B > nul
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.