我有一个log.txt文件,其中包含DATE的出现次数,格式如下,为过去1年的数据。
25/11/2013
我们如何回显log.txt文件中最早的日期?
我在google和Stack下面搜索仅以文件结尾,没有得到关于内部文件内容的任何内容。
@echo off
for /f %%i in ('dir /b/a-d/o-d/t:c') do set OLD=%%i
echo The most oldest file is %OLD%
日志摘要:
SERVERNAME
--------------------------------------------------------------------------------
APPNAME TO_CHAR((TO_DATE('0 TO_CHAR((TO_DATE('0
-------------------- ------------------- -------------------
STRDESCRIPTION
--------------------------------------------------------------------------------
SUSERNAME
--------------------------------------------------------------------------------
Windows64
Metadata 25/11/2013 14:14:41 25/11/2013 14:25:29
user@DBgroup
答案 0 :(得分:2)
一个解决方案:
编辑:
@echo off&cls
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type sacha.txt ^| findstr /i "./"') do (
for %%b in (%%a) do (set $test=%%b
if "!$test:~2,1!"=="/" (set $date=%%b
set #!$date:~6,4!!$date:~3,2!!$date:~0,2!=%%b)))
for /f "tokens=2 delims==" %%a in ('set #') do (echo The Oldest Date Is : %%a
exit /b)
endlocal
显然,您的日期始终是包含日期的行的第三个字。如果是这种情况你可以使用下面的代码,如果你现在不能使用上面那个,那么测试行中单词的第三个字符是否是“/”如果是,它将单词设置为日期。但两者都与您提供的代码片段一起使用。第一个给你更多的安全性。但我认为第二个是好的和更快的:
@echo off&cls
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type sacha.txt ^| findstr /i "./"') do (
for /f "tokens=3 delims= " %%b in ('echo %%a') do set #!$date:~6,4!!$date:~3,2!!$date:~0,2!=%%b)
for /f "tokens=2 delims==" %%a in ('set #') do (echo The Oldest Date Is : %%a
exit /b)
endlocal
答案 1 :(得分:1)
此处您不需要delayed expansion
:
@ECHO OFF &SETLOCAL
FOR /F "tokens=1-3delims=/" %%a IN (log.txt) DO set "$%%c%%b%%a=%%a/%%b/%%c"
FOR /F "tokens=2delims==" %%A IN ('set $') DO if not defined OldestDate set "OldestDate=%%A"
ECHO(The most oldest file is %OldestDate%
答案 2 :(得分:1)
看看这是怎么回事,晴天:
@echo off
del tmp.tmp 2>nul
for /f "tokens=2,3,4 delims=/ " %%a in (' find "/" ^< "log.txt" ') do >>tmp.tmp echo %%c/%%b/%%a
for /f "delims=" %%a in ('sort /r ^<tmp.tmp ') do set lastdate=%%a
echo "%lastdate%"
pause
del tmp.tmp 2>nul