批处理 .txt reader
Batch .txt reader
所以,基本上我想要一个批处理文件来读取 .txt。问题是每次将新行写入 .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。
对不起我糟糕的英语
谢谢
要启动批处理,您需要执行以下操作call batch.cmd txtname.txt
如果您不想使用第三方选项并希望保持纯批处理,这是很有可能的。从你的问题来看,听起来你希望阅读文本文件的最后一行并让它在每次编辑文本文件时更新该文本。此外,这个批处理文件可以call
'ed到需要使用的时候。
为此,我们可以在 for loop
中使用 forfiles
比较上次修改的日期。这样做的原因是,如果我们使用文件属性 EX: ECHO Last-Modified Date : %%~ta
,我们将无法将属性精确到秒。因此,该文件只会比较到分钟。
现在我们可以获取最后修改的属性,我们可以使用 IF
语句来查找文件何时获得新的时间戳。从那里我们可以使用修改后的脚本,该脚本只读取由 @Patrick Cuff
制作的文本文件的最后一行(可由 set /a LINES=LINES+1
LINES+1 - Infin 配置)
要调用此批处理文件,您需要使用 call ReadFile.bat txtname.txt
- 调用 - 命令
- ReadFile.bat - 批处理脚本的名称
- txtname.txt - 要读取的文本文件的名称
下面是完整的脚本。
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
如需任何命令的帮助,请执行以下操作:
- 呼叫/?
- 设置/?
- 对于/?
- 如果/?
- 等等。
一个基本的tail命令可以这样写。感谢@dbenham 在 DosTips.com
上的初步解决方案
@echo off
call :Loop <"tailme.txt"
exit
:Loop
set "line="
set /p "line="
if defined line (
echo %line%
) else (
pathping -q 1 -p 300 localhost >nul
)
goto :loop
所以,基本上我想要一个批处理文件来读取 .txt。问题是每次将新行写入 .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。
对不起我糟糕的英语
谢谢
要启动批处理,您需要执行以下操作call batch.cmd txtname.txt
如果您不想使用第三方选项并希望保持纯批处理,这是很有可能的。从你的问题来看,听起来你希望阅读文本文件的最后一行并让它在每次编辑文本文件时更新该文本。此外,这个批处理文件可以call
'ed到需要使用的时候。
为此,我们可以在 for loop
中使用 forfiles
比较上次修改的日期。这样做的原因是,如果我们使用文件属性 EX: ECHO Last-Modified Date : %%~ta
,我们将无法将属性精确到秒。因此,该文件只会比较到分钟。
现在我们可以获取最后修改的属性,我们可以使用 IF
语句来查找文件何时获得新的时间戳。从那里我们可以使用修改后的脚本,该脚本只读取由 @Patrick Cuff
set /a LINES=LINES+1
LINES+1 - Infin 配置)
要调用此批处理文件,您需要使用 call ReadFile.bat txtname.txt
- 调用 - 命令
- ReadFile.bat - 批处理脚本的名称
- txtname.txt - 要读取的文本文件的名称
下面是完整的脚本。
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
如需任何命令的帮助,请执行以下操作:
- 呼叫/?
- 设置/?
- 对于/?
- 如果/?
- 等等。
一个基本的tail命令可以这样写。感谢@dbenham 在 DosTips.com
上的初步解决方案@echo off
call :Loop <"tailme.txt"
exit
:Loop
set "line="
set /p "line="
if defined line (
echo %line%
) else (
pathping -q 1 -p 300 localhost >nul
)
goto :loop