批处理.txt阅读器 [英] Batch .txt reader

查看:191
本文介绍了批处理.txt阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我希望批处理文件读取.txt.问题在于,每当将新行写入.txt

So, basically I want a Batch file to read a .txt. The problem is that the Batch file needs to update everytime a new line gets written to the .txt

@echo off
set "pc=%1"
FOR /F "delims=:" %%A IN ('findstr /N .* "%pc%"') DO set "zeilen=%%A" 
type %pc%
set /A zeilen1=%zeilen%
:loop
if not %zeilen% == %zeilen1% (
set "line="
set zeilen2=%zeilen% - 1
for /f %%a in ('more/e +%zeilen2% ^< %pc%') do (
   if not defined line set "line=%%a"
)
 echo %line%   
 set /A zeilen+=1
)
FOR /F "delims=:" %%A IN ('findstr /N .* "%pc%"') DO set "zeilen1=%%A
goto loop

我也不能使用type命令(第9-13行),因为我不想只刷新最后一行的整个.txt.

I also can't use the type command (line 9-13) because I don't want to refresh the whole .txt only the last line.

为我的英语不好对不起

谢谢

要启动批处理,您需要执行以下操作call batch.cmd txtname.txt

To start the Batch you need to do something like this call batch.cmd txtname.txt

推荐答案

如果您不想使用第三方选项并希望保持纯批处理,则很有可能.从您的问题看来,您似乎希望阅读文本文件的最后一行,并在每次编辑文本文件时让它更新该文本.此外,该批处理文件在需要使用时会被call修改.

If you don't wish to use third party options and wish to keep it pure batch, it is very possible. From your question, it sounds like you wish to read the last line of a text file and have it update that text each time the text file is edited. Further more, this batch file much be call'ed to when it needs to be used.

为此,我们可以在for loop中比较使用forfiles对其上次修改的日期.这样做的原因是,如果我们使用文件属性EX:ECHO Last-Modified Date : %%~ta,我们将不会把属性缩短到几秒钟.因此,该文件只会与分钟进行比较.

To do this, we can compare the date it was last modified using forfiles in an for loop. The reason for this is that if we use the file properties EX: ECHO Last-Modified Date : %%~ta we will not get the properties down to seconds. Thus the file will only compare down to the minutes.

现在,我们可以获取最后修改的属性,可以使用IF语句查找文件何时获得新的时间戳.从那里,我们可以使用修改后的脚本,该脚本仅读取

Now that we can grab the last modified properties we can use an IF statement to look for when the file get a new time stamp. From there we can use a modified script that reads only the last line of a text file (Configurable by set /a LINES=LINES+1 LINES+1 - Infin) made by @Patrick Cuff

要调用此批处理文件,您将要使用call ReadFile.bat txtname.txt

To call this batch file you will want to use call ReadFile.bat txtname.txt

  • 呼叫-命令
  • ReadFile.bat -批处理脚本的名称
  • txtname.txt -要读取的文本文件的名称
  • Call - Command
  • ReadFile.bat - Name of batch script
  • txtname.txt - Name of textfile to read

下面是完整的脚本.

ReadFile.bat

@ECHO OFF
@GOTO READ

:LOOP
Rem | Look for changes
FOR /f %%a in ('forfiles /M %1 /C "cmd /c echo @fdate-@ftime"') DO (set FileTimeCurrent=%%a)

IF "%FileTimeLoad%"=="%FileTimeCurrent%" (goto LOOP) else (goto READ)

:READ
cls

Rem | Get current date
FOR /f %%a in ('forfiles /M %1 /C "cmd /c echo @fdate-@ftime"') DO (set FileTimeLoad=%%a)

Rem | Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (%1) do (
    set /a LINES=LINES+1
)

Rem | Print the last line
set /a LINES=LINES-1
more +%LINES% < %1

goto LOOP

要获取有关任何命令的帮助,请执行以下操作:

For help on any of the commands do the following:

  • 致电/?
  • 设置/?
  • /?
  • 如果/?
  • 等等.

这篇关于批处理.txt阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆