How to sort file contents based on pattern?
Posted: 09 Apr 2019 01:38
Hi Folks -
I have a text file that looks like such:
This text file is written to by an application and it writes in in descending order, and since the batch will start at the top go down, its opposite of what I need.
Here is a batch script that works if there is no variation in length of numbers after the "_" :
But obviously rhats not dynamic and I can't always gaurentee that will always be the case so I need to force it to grab the highest. Is this easy to achieve?
Thanks!
I have a text file that looks like such:
As you can see, the string after the "_" varies from 2 to 4 digits. I ALWAYS need to get the file names with the "highest" numerical value after the "_".
outbox/logs/PLAN - Custom_8001.log
outbox/logs/PLAN - Custom_242.log
outbox/logs/PLAN - Custom_243.log
outbox/logs/PLAN - Custom_244.log
outbox/logs/PLAN - Custom_245.log
outbox/logs/PLAN - Custom_246.log
outbox/logs/PLAN - Custom_40.log
outbox/logs/PLAN - Custom_41.log
outbox/logs/PLAN - Custom_42.log
outbox/logs/PLAN - Custom_43.log
outbox/logs/PLAN - Custom_44.log
outbox/logs/PLAN - Custom_45.log
outbox/logs/PLAN - Custom_46.log
This text file is written to by an application and it writes in in descending order, and since the batch will start at the top go down, its opposite of what I need.
Here is a batch script that works if there is no variation in length of numbers after the "_" :
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CD C:\TEMP
SET "SEARCH=outbox/logs/PLAN - Custom_"
SET "FILE=temp.txt"
SORT "%FILE%"
FOR /F "tokens=*" %%A IN ('FINDSTR /c:"%SEARCH%" %FILE%') DO (
SET "NAME=%%~nA" & IF "!NAME!" GTR "!MAXFILE!" SET "MAXFILE=!NAME!.log"
)
Thanks!