键盘忍者:在Windows中串联多个文本文件

本文介绍了一种在Windows环境下使用命令行工具快速合并多个日志文件的方法。通过for命令和type命令结合使用,可以轻松地将同一目录下的多个日志文件合并成一个文件,便于后续的数据处理。

You have a directory full of log files that you want to import into Excel or a database so you can do some processing on them… but there are hundreds of files… how do you make them into a single file?

您有一个目录,其中包含要导入到Excel或数据库中的日志文件,以便可以对其进行一些处理……但是有数百个文件……如何将它们合并为一个文件?

image

Answer: Pull out your DOS hat, open a command prompt, and then use the “for” command.

答:拔出DOS帽子,打开命令提示符,然后使用“ for”命令。

The syntax works something like this:

语法如下所示:

for <variablename> in (<directorylisting>) do <command> <variablename>

对于(< 目录列表>)中的< 变量名 >,请执行< 命令 > < 变量名 >

So if you wanted to append all of the *.log files in a directory, you’d use the “type” command and then pipe it into a single file using the >> operator.

因此,如果要在目录中附加所有* .log文件,则可以使用“ type”命令,然后使用>>运算符将其通过管道传输到单个文件中。

The difference between >> and > is that the former appends data to the end of the file, and the latter will completely replace the file, which would be pointless for what we want to do.

>>和>之间的区别在于,前者将数据追加到文件的末尾,而后者将完全替换文件,这对于我们想要做的事情毫无意义。

So here’s the command you’d run, assuming you are in the directory containing the log files.

所以这是您要运行的命令,假设您位于包含日志文件的目录中。

for %f in (*.log) do type "%f" >> aggregate.txt

for %f in (*.log) do type "%f" >> aggregate.txt

And yes, I actually just used this command for a project at work, which is why I’m writing up this article. =)

是的,实际上我只是在工作中的项目中使用了此命令,这就是为什么我撰写本文的原因。 =)

Random thought: What on earth would a DOS hat look like?

随机的想法:DOS帽子到底是什么样的?

翻译自: https://www.howtogeek.com/howto/keyboard-ninja/keyboard-ninja-concatenate-multiple-text-files-in-windows/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值