Reputation: 75
Please help me to open a file like app.log.192192193
in Notepad using batch script from the below scenario.
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
In simple words all files were of same date and time and differ in size and file name. Need to open the file which has greater value in the file name, i.e. 1921192193 > 143143143
Upvotes: 2
Views: 148
Reputation: 49127
Here is my batch solution with less lines and which definitely works for the example file names.
@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
Upvotes: 1
Reputation: 80113
@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
You would need to change the setting of sourcedir
to suit your circumstances.
You haven't told us how long your number may be or whether it may be variable-length. This should deal with up to 30 digits. Solutions using gtr
but not padding the number assume that the length of the final numeric part is constant and nine digits or fewer.
The notepad
command is merely echo
ed for testing purposes. After you've verified that the commands are correct, change ECHO(notepad
to notepad
to actually have notepad open the file.
Upvotes: 1
Reputation: 57272
Open the largest file.
@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%"
EDIT - opens the file with the largest number in it's name
@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%"
Upvotes: 4