如何根据文件名中的最大数量从批处理文件中打开记事本中的文件?

时间:2014-08-28 09:39:03

标签: batch-file

请帮我在记事本中使用以下方案中的批处理脚本打开app.log.192192193这样的文件。

c:\Abcd\app\log
  app.log.123123123 --- 200KB      same date time
  app.log.143143143 --- 20000KB    same date time
  app.log.192192193 --- 0KB        same date time

简单来说,所有文件都具有相同的日期和时间,并且大小和文件名不同。 需要打开文件名中具有更大价值的文件,即1921192193> 143143143

3 个答案:

答案 0 :(得分:4)

打开最大的文件。

@echo off
set log_dir=c:\log_dir
pushd "%log_dir%"

for /f "delims=" %%f in ('dir /b /o:s app.log.*') do (
    set "largest_log=%%~ff"
)

start notepad "%largest_log%"

编辑 - 打开文件中名称最大的文件

@echo off
set log_dir=c:\log_dir
pushd "%log_dir%"
setlocal enableDelayedExpansion
set "current_number=0"
for /f "tokens=3 delims=." %%f in ('dir /b /o:s app.log.*') do (
    if %%~f GTR !current_number! (
        set "current_number=%%f"
    )
)

start notepad "app.log.%current_number%"

答案 1 :(得分:1)

这是我的批处理解决方案,使用较少的行,这绝对适用于示例文件名。

@echo off
setlocal EnableDelayedExpansion
set GreatestNumber=.0
for /F %%f in ('dir "C:\Abcd\app\log\app.log.*" /B 2^>nul') do (
   if "%%~xf" GTR "!GreatestNumber!" set "GreatestNumber=%%~xf"
)
if not "%GreatestNumber%" == ".0" (
   start "Notepad" %SystemRoot%\System32\Notepad.exe "C:\Abcd\app\log\app.log%GreatestNumber%"
)
endlocal

答案 2 :(得分:1)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "mostnum="
SET "moststr=."
SET "dots=..........................................................."
FOR %%a IN ("%sourcedir%\app.log.*") DO (
 SET tempstr=%dots%%%~xa
 SET "tempstr=!tempstr:~-30!"
 IF !tempstr! gtr !moststr! (
  set "moststr=!tempstr!"
  SET mostnum=%%~xa
 )
)

ECHO(notepad "%sourcedir%\app.log%mostnum%"

GOTO :EOF

您需要更改sourcedir的设置以适合您的具体情况。

您还没有告诉我们您的电话号码可能有多长,或者它是否可变长。这应该处理多达30位数字。使用gtr但不填充数字的解决方案假设最终数字部分的长度为常数九位或更少。

notepad命令仅用于echo用于测试目的。 在您确认命令正确后,将ECHO(notepad更改为notepad以实际让记事本打开文件。