[英]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.