[英]Batch file to list all txt files with a certain filename and certain content on windows server 2008
[英]Displaying content of multiple .txt files with the same filename in all subdirectories
我在各个子目录下有多个具有相同文件名的文件,例如:
c:\kjhsd\client.txt
c:\ekjjs\client.txt
c:\oiwnk\client.txt
我不知道上面随机字母表示的路径的中间部分,但文件名总是一致的。
我需要一种使用此命令的方法: type client.txt
(或) more client.txt
但同时显示任何目录下文件名为“client.txt”的所有文本文件的内容。
因此,如果:
c:\\kjhsd\\client.txt
包含: hello
c:\\ekjjs\\client.txt
包含: helloworld
c:\\oiwnk\\client.txt
包含: helloagain
运行单个命令后,我会看到:
hello
helloworld
helloagain
提前致谢 :)
从cmd.exe
:
@for /r c:\ %a in ("client.txt") do @type "%~a"2>nul
从Batch-File
:
@for /r c:\ %%a in ("client.txt") do @type "%%~a"2>nul
编辑:根据换行要求:
@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
或者如果你想查看哪个文件有什么文本:
@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
最简单的方法是使用for /D
循环来查找不同的目录,并让find /V ""
返回文件的内容,因为如果没有文件,则会自动将换行符附加到每个文件中文件本身(与type
相对)。
在命令提示符中执行以下操作:
@for /D %I in ("D:\some\root\*") do @(< "%~I\client.txt" find /V "") 2> nul
在批处理文件中执行以下操作:
@for /D %%I in ("D:\some\root\*") do @(< "%%~I\client.txt" find /V "") 2> nul
如果目录不包含名为client.txt
的文件,则2> nul
部分会抑制 eerror 消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.