在批处理文件中显示.txt文件中的文本

时间:2021-11-25 02:23:12

I'm scripting a big batch file.

我正在编写一个大批处理文件。

It records the date to a log.txt file:

它将日期记录到log.txt文件中:

@echo off
echo %date%, %time% >> log.txt
echo Current date/time is %date%, %time%.
@pause
exit

It can record it several times, on several lines. Now what I want to do is that the batch file file shows the last recorded date/time from the log.txt file.

它可以在几行上记录几次。现在我想要做的是批处理文件文件显示log.txt文件中最后记录的日期/时间。

How?

10 个解决方案

#1


type log.txt

But that will give you the whole file. You could change it to:

但那会给你整个文件。您可以将其更改为:

echo %date%, %time% >> log.txt
echo %date%, %time% > log_last.txt
...
type log_last.txt

to get only the last one.

只得到最后一个。

#2


hmm.. just found the answer. it's easier then i thought. it just needs a bunch more stuff:

嗯..刚刚找到了答案。我认为这比较容易。它只需要更多东西:

@echo off
if not exist log.txt GOTO :write
echo Date/Time last login:
type log.txt
del log.txt
:write
echo %date%, %time%. >> log.txt
@pause
exit

So it first reads the log.txt file and deletes it. After that it just get a new file (log.txt) with the date & time!

所以它首先读取log.txt文件并删除它。之后,它只会获得一个包含日期和时间的新文件(log.txt)!

I hope this helps other people!

我希望这有助于其他人!

(the only prob is that the first time it does not work, but then just enter in random value at log.txt.) (This problem is solved and edited.)

(唯一的问题是它第一次不起作用,但只需在log.txt中输入随机值。)(此问题已解决并编辑。)

#3


Use the tail.exe from the Windows 2003 Resource Kit

使用Windows 2003 Resource Kit中的tail.exe

#4


Try this: use Find to iterate through all lines with "Current date/time", and write each line to the same file:

试试这个:使用Find以“当前日期/时间”迭代所有行,并将每一行写入同一个文件:

for /f "usebackq delims==" %i in (`find "Current date" log.txt`) do (echo %i > log-time.txt)
type log-time.txt

Set delims= to a character not relevant in the date/time lines. Use %%i in batch files.

将delims =设置为与日期/时间行不相关的字符。在批处理文件中使用%% i。

Explanation (update):

Find extracts all lines from log.txt containing the search string.

查找从包含搜索字符串的log.txt中提取所有行。

For /f loops through each line the command inside (...) generates.

对于/ f遍历每一行,内部(...)中的命令生成。

As echo > log-time.txt (single > !) overwrites log-time.txt every time it's executed, only the last matching line remains in log-time.txt

由于echo> log-time.txt(single>!)每次执行时都会覆盖log-time.txt,因此只有最后一个匹配的行保留在log-time.txt中

#5


Here's a version that doesn't fail if log.txt is missing:

如果缺少log.txt,这是一个不会失败的版本:


@echo off
  if not exist log.txt goto firstlogin
  echo Date/Time last login:
  type log.txt
  goto end

:firstlogin
  echo No last login found.

:end
  echo %date%, %time%. > log.txt
  pause

#6


Ok I wonder when's the use but, here are two snipets you could use:

好吧我想知道什么时候使用但是,这里有两个你可以使用的snipets:

lastlog.cmd

@echo off
for /f "delims=" %%l in (log.txt) do set TimeStamp=%%l
echo %TimeStamp%

Change the "echo.." line, but the last log time is within %TimeStamp%. No temp files used, no clutter and reusable as it is in a variable.

更改“echo ..”行,但最后一次登录时间在%TimeStamp%之内。没有使用临时文件,没有杂乱和可重用的变量。

On the other hand, if you need to know this WITHIN your code, and not from another batch, change your logging for:

另一方面,如果您需要在代码中知道此内容而不是其他批次,请更改日志记录:

set TimeStamp=%date%, %time%
echo %TimeStamp% >> log.txt

so that the variable %TimeStamp% is usable later when you need it.

这样变量%TimeStamp%可在以后需要时使用。

#7


A handy timestamp format:

一个方便的时间戳格式:

%date:~3,2%/%date:~0,2%/%date:~6,2%-%time:~0,8%

#8


just set the time and date to variables if it will be something that will be in a loop then

只需将时间和日期设置为变量,如果它将在循环中

:top
set T=%time%
set D=%Date%

echo %T%>>log.txt
echo %d%>>log.txt
echo time:%T%
echo date:%D%
pause
goto top

i suggest making it nice and clean by putting

我建议通过推杆使其变得美观和干净

@echo off

in fromt of every thing it get rid of the crappy C:/users/example/...

在它的每一件事中它摆脱了蹩脚的C:/ users / example / ...

and putting

cls

after the :top to clear the screen before it add the new date and time to the display

之后:顶部清除屏幕,然后将新的日期和时间添加到显示屏

#9


Here is a good date and time code:

这是一个很好的日期和时间代码:

@echo off
if %date:~4,2%==01 set month=January
if %date:~4,2%==02 set month=February
if %date:~4,2%==03 set month=March
if %date:~4,2%==04 set month=April
if %date:~4,2%==05 set month=May
if %date:~4,2%==06 set month=June
if %date:~4,2%==07 set month=July
if %date:~4,2%==08 set month=August
if %date:~4,2%==09 set month=September
if %date:~4,2%==10 set month=October
if %date:~4,2%==11 set month=November
if %date:~4,2%==12 set month=December

if %date:~0,3%==Mon set day=Monday
if %date:~0,3%==Tue set day=Tuesday
if %date:~0,3%==Wed set day=Wednesday
if %date:~0,3%==Thu set day=Thursday
if %date:~0,3%==Fri set day=Friday
if %date:~0,3%==Sat set day=Saturday
if %date:~0,3%==Sun set day=Sunday
echo.
echo The Date is %day%, %month% %date:~7,2%, %date:~10,4% the current time is: %time:~0,5%
pause

Outputs: The Date is Sunday, September 27, 2009 the current time is: 3:07

输出:日期是2009年9月27日星期日,当前时间是:3:07

#10


@echo off
set log=%time% %date%
echo %log%

That is a batch for saving the date and time as a temporary variable, and displaying it. In a hurry, I don't have time to write a script to open a txt, maybe later.

这是一个批处理,用于将日期和时间保存为临时变量并显示它。匆忙,我没有时间编写脚本来打开txt,也许以后。

#1


type log.txt

But that will give you the whole file. You could change it to:

但那会给你整个文件。您可以将其更改为:

echo %date%, %time% >> log.txt
echo %date%, %time% > log_last.txt
...
type log_last.txt

to get only the last one.

只得到最后一个。

#2


hmm.. just found the answer. it's easier then i thought. it just needs a bunch more stuff:

嗯..刚刚找到了答案。我认为这比较容易。它只需要更多东西:

@echo off
if not exist log.txt GOTO :write
echo Date/Time last login:
type log.txt
del log.txt
:write
echo %date%, %time%. >> log.txt
@pause
exit

So it first reads the log.txt file and deletes it. After that it just get a new file (log.txt) with the date & time!

所以它首先读取log.txt文件并删除它。之后,它只会获得一个包含日期和时间的新文件(log.txt)!

I hope this helps other people!

我希望这有助于其他人!

(the only prob is that the first time it does not work, but then just enter in random value at log.txt.) (This problem is solved and edited.)

(唯一的问题是它第一次不起作用,但只需在log.txt中输入随机值。)(此问题已解决并编辑。)

#3


Use the tail.exe from the Windows 2003 Resource Kit

使用Windows 2003 Resource Kit中的tail.exe

#4


Try this: use Find to iterate through all lines with "Current date/time", and write each line to the same file:

试试这个:使用Find以“当前日期/时间”迭代所有行,并将每一行写入同一个文件:

for /f "usebackq delims==" %i in (`find "Current date" log.txt`) do (echo %i > log-time.txt)
type log-time.txt

Set delims= to a character not relevant in the date/time lines. Use %%i in batch files.

将delims =设置为与日期/时间行不相关的字符。在批处理文件中使用%% i。

Explanation (update):

Find extracts all lines from log.txt containing the search string.

查找从包含搜索字符串的log.txt中提取所有行。

For /f loops through each line the command inside (...) generates.

对于/ f遍历每一行,内部(...)中的命令生成。

As echo > log-time.txt (single > !) overwrites log-time.txt every time it's executed, only the last matching line remains in log-time.txt

由于echo> log-time.txt(single>!)每次执行时都会覆盖log-time.txt,因此只有最后一个匹配的行保留在log-time.txt中

#5


Here's a version that doesn't fail if log.txt is missing:

如果缺少log.txt,这是一个不会失败的版本:


@echo off
  if not exist log.txt goto firstlogin
  echo Date/Time last login:
  type log.txt
  goto end

:firstlogin
  echo No last login found.

:end
  echo %date%, %time%. > log.txt
  pause

#6


Ok I wonder when's the use but, here are two snipets you could use:

好吧我想知道什么时候使用但是,这里有两个你可以使用的snipets:

lastlog.cmd

@echo off
for /f "delims=" %%l in (log.txt) do set TimeStamp=%%l
echo %TimeStamp%

Change the "echo.." line, but the last log time is within %TimeStamp%. No temp files used, no clutter and reusable as it is in a variable.

更改“echo ..”行,但最后一次登录时间在%TimeStamp%之内。没有使用临时文件,没有杂乱和可重用的变量。

On the other hand, if you need to know this WITHIN your code, and not from another batch, change your logging for:

另一方面,如果您需要在代码中知道此内容而不是其他批次,请更改日志记录:

set TimeStamp=%date%, %time%
echo %TimeStamp% >> log.txt

so that the variable %TimeStamp% is usable later when you need it.

这样变量%TimeStamp%可在以后需要时使用。

#7


A handy timestamp format:

一个方便的时间戳格式:

%date:~3,2%/%date:~0,2%/%date:~6,2%-%time:~0,8%

#8


just set the time and date to variables if it will be something that will be in a loop then

只需将时间和日期设置为变量,如果它将在循环中

:top
set T=%time%
set D=%Date%

echo %T%>>log.txt
echo %d%>>log.txt
echo time:%T%
echo date:%D%
pause
goto top

i suggest making it nice and clean by putting

我建议通过推杆使其变得美观和干净

@echo off

in fromt of every thing it get rid of the crappy C:/users/example/...

在它的每一件事中它摆脱了蹩脚的C:/ users / example / ...

and putting

cls

after the :top to clear the screen before it add the new date and time to the display

之后:顶部清除屏幕,然后将新的日期和时间添加到显示屏

#9


Here is a good date and time code:

这是一个很好的日期和时间代码:

@echo off
if %date:~4,2%==01 set month=January
if %date:~4,2%==02 set month=February
if %date:~4,2%==03 set month=March
if %date:~4,2%==04 set month=April
if %date:~4,2%==05 set month=May
if %date:~4,2%==06 set month=June
if %date:~4,2%==07 set month=July
if %date:~4,2%==08 set month=August
if %date:~4,2%==09 set month=September
if %date:~4,2%==10 set month=October
if %date:~4,2%==11 set month=November
if %date:~4,2%==12 set month=December

if %date:~0,3%==Mon set day=Monday
if %date:~0,3%==Tue set day=Tuesday
if %date:~0,3%==Wed set day=Wednesday
if %date:~0,3%==Thu set day=Thursday
if %date:~0,3%==Fri set day=Friday
if %date:~0,3%==Sat set day=Saturday
if %date:~0,3%==Sun set day=Sunday
echo.
echo The Date is %day%, %month% %date:~7,2%, %date:~10,4% the current time is: %time:~0,5%
pause

Outputs: The Date is Sunday, September 27, 2009 the current time is: 3:07

输出:日期是2009年9月27日星期日,当前时间是:3:07

#10


@echo off
set log=%time% %date%
echo %log%

That is a batch for saving the date and time as a temporary variable, and displaying it. In a hurry, I don't have time to write a script to open a txt, maybe later.

这是一个批处理,用于将日期和时间保存为临时变量并显示它。匆忙,我没有时间编写脚本来打开txt,也许以后。