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或数据库中的日志文件,以便可以对其进行一些处理……但是有数百个文件……如何将它们合并为一个文件?
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帽子到底是什么样的?
本文介绍了一种在Windows环境下使用命令行工具快速合并多个日志文件的方法。通过for命令和type命令结合使用,可以轻松地将同一目录下的多个日志文件合并成一个文件,便于后续的数据处理。

768

被折叠的 条评论
为什么被折叠?



