batch-file - 适用于 Windows 的本机 cmd tail 命令
问题描述
我正在寻找构建一个tail
仅使用 native 来读取 Windows 上的日志文件cmd
。
有多种读取文件的方法:
type file.txt
more file.txt
但是,它们没有从文件读取更新的默认选项,因此不代表tail
.
解决方案
通过一些小技巧并使用more
来读取文件,跳过之前已经读取的内容,我们能够“几乎”实时读取tail
文件。
@echo off & set cnt=0
if "%~1" == "" echo no file specified, usage: "tail.cmd <filename>" & goto :eof
if not exist "%~1" echo file "%~1" does not exist & goto :eof
:tail_sub
2>nul (>>"%~1" echo off) && (goto :file) || (goto :tail_sub)
:file
for /f "tokens=1*delims=]" %%i in ('more "%~1" +%cnt% ^| find /v /n ""') do (
set "line=%%j"
set /a cnt+=1
call echo(%%line%%
)
goto :tail_sub
目前,在不添加 atimeout
来减慢无限循环的情况下,它会消耗大约 5.6MB 的内存,这在我看来是非常可以接受的。这还没有处理所有特殊字符,比如|<>&
,但我会花一些时间来满足我能想到的所有场景。
推荐阅读
- visual-studio - 如何自动发布 .NET Web 应用程序
- redis - 了解您为什么要在未来处理消息队列
- dialogflow-es - DialogFlow 的 Webhook 和 javascript,我在哪里可以找到文档?
- django - 将文件从 Docker 容器上传到 Google Cloud Storage
- colors - 如何使用 Applescript(或命令行)获取加载的显示器颜色配置文件?
- c - C中函数声明中的编译错误
- python - 使用函数将 hbar 散景图嵌入到烧瓶应用程序中
- angularjs - AngularJS 1.7.9:如何调试“可能未处理的拒绝:{}”?
- javascript - 重定向时将数据传递到我的 ejs 视图
- import - Rust 从另一个文件导入结构