简体   繁体   English

在所有子目录中显示具有相同文件名的多个 .txt 文件的内容

[英]Displaying content of multiple .txt files with the same filename in all subdirectories

I have multiple files with the same filename under various subdirectories, for example:我在各个子目录下有多个具有相同文件名的文件,例如:

c:\kjhsd\client.txt
c:\ekjjs\client.txt
c:\oiwnk\client.txt

I do not know the middle part of the path represented by random letters above, but the filename is always consistent.我不知道上面随机字母表示的路径的中间部分,但文件名总是一致的。

I need a way to use this command: type client.txt (or) more client.txt but to display the content of all text files with the filename "client.txt" under any directory at the same time.我需要一种使用此命令的方法: type client.txt (或) more client.txt但同时显示任何目录下文件名为“client.txt”的所有文本文件的内容。

So if:因此,如果:

c:\\kjhsd\\client.txt contained: hello c:\\kjhsd\\client.txt包含: hello

c:\\ekjjs\\client.txt contained: helloworld c:\\ekjjs\\client.txt包含: helloworld

c:\\oiwnk\\client.txt contained: helloagain c:\\oiwnk\\client.txt包含: helloagain

After running the single command, I would see:运行单个命令后,我会看到:

hello
helloworld
helloagain

Thanks in advance :)提前致谢 :)

from cmd.exe :cmd.exe

@for /r c:\ %a in ("client.txt") do @type "%~a"2>nul

From a Batch-File :Batch-File

@for /r c:\ %%a in ("client.txt") do @type "%%~a"2>nul

Edit: as per the newline requirement:编辑:根据换行要求:

@echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
    type "%%~a"2>nul
    echo(
)
Popd

or if you want to see which file had what text:或者如果你想查看哪个文件有什么文本:

@echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
    echo content: "%%~a"
    type "%%~a"2>nul
    echo(
)
Popd

The easiest approach is to use a for /D loop to find the different directories, and to let find /V "" return the contents of the files, because then a line-break is automatically appended to every file in case there is none in the file itself (opposed to type ).最简单的方法是使用for /D循环来查找不同的目录,并让find /V ""返回文件的内容,因为如果没有文件,则会自动将换行符附加到每个文件中文件本身(与type相对)。

In Command Prompt do this:在命令提示符中执行以下操作:

@for /D %I in ("D:\some\root\*") do @(< "%~I\client.txt" find /V "") 2> nul

In a batch file do this:在批处理文件中执行以下操作:

@for /D %%I in ("D:\some\root\*") do @(< "%%~I\client.txt" find /V "") 2> nul

The 2> nul portion suppresses eerror messages if a directory does not contain a file called client.txt .如果目录不包含名为client.txt的文件,则2> nul部分会抑制 eerror 消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件列出在Windows Server 2008上具有特定文件名和特定内容的所有txt文件 - Batch file to list all txt files with a certain filename and certain content on windows server 2008 Windows中的每个子目录将多个子目录中的多个txt合并为1个txt - Merge multiple txt in several subdirectories into 1 txt for each subdirectories in Windows 如何合并所有子目录中具有相同名称的文本文件? - How to merge text files with the same name from all subdirectories? 遍历多个文件,在每个文件中显示文件名和第一个单词 - Iterate through multiple files displaying filename and first word in each Windows批处理-尝试合并所有子目录中具有相同名称的文本文件时出现空文件 - Windows Batch - empty files while trying to merge text files with the same name from all subdirectories 合并具有相同文件名的多个文件夹中的PDF文件 - Merge PDF files from multiple folders with same filename Windows打开文件对话框,其中包含所有文件+子目录 - Windows open files dialog with all files + subdirectories Windows批处理文件 - 连接子目录中的所有文件 - Windows batch file - Concatenate all files in subdirectories 打印给定路径中的所有文件和子目录 - print all files and subdirectories in a give path 在所有文件和子目录中找到任何提及 google 的内容? - Find any mention of google in all files and subdirectories?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM