Extract file name with largest suffix

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 446
Joined: 07 Jan 2016 07:47

Extract file name with largest suffix

#1 Post by SIMMS7400 » 14 Dec 2017 18:35

Hi Folks -

I have a text file I pull down from a cloud environment which I need to parse and then based off that file, download that file.

Here is a sample file:

Code: Select all

 inbox/EBS/Client_GL_Detail_PBCS_121017_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_DEC2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_OCT2017.txt
 inbox/Client/Client_GL_Detail_PBCS_111417_SEP2017.txt
 inbox/Client/Client_GL_Detail_PBCS_111417_SEP2017.xls
 outbox/Client_2678.dat
 outbox/Client_2682.dat
 outbox/logs/Client_2954.log
 outbox/logs/Client_2955.log
 outbox/logs/Client_2956.log
 outbox/logs/Client_2957.log
 outbox/logs/Client_2958.log
 outbox/logs/Client_2959.log
 outbox/logs/Client_2960.log
 outbox/logs/Client_2961.log
 outbox/logs/Client_2962.log
 outbox/logs/Client_2963.log
 outbox/logs/Client_2964.log
 
 Total 489
The only content I care about is as follows : outbox/logs/Client_

My code to grab only that content and figure out the highest 4 digit suffix is as follows:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MAXFILE="
FOR /F "tokens=*" %%A IN ('FINDSTR /c:"outbox/logs/Client_" file.txt') DO (
  SET "NAME=%%~nA"
  IF !NAME! GTR !MAXFILE! SET "MAXFILE=!NAME!"
)
ECHO file is : %MAXFILE%
pause
Is my method acceptable or is there a better way?

Thank you.

aGerman
Expert
Posts: 3761
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Extract file name with largest suffix

#2 Post by aGerman » 14 Dec 2017 19:12

As long as the suffix is always 4 digits long you could also compare the entire paths. But if the length differs it's not sufficient to compare alphanumeric (not even with the name only).
Try

Code: Select all

@echo off &setlocal
if "outbox/logs/Client_1000.log" gtr "outbox/logs/Client_0999.log" (echo first is greater) else echo first is not greater
if "outbox/logs/Client_1000.log" gtr "outbox/logs/Client_999.log" (echo first is greater) else echo first is not greater
pause
Steffen

Squashman
Expert
Posts: 4114
Joined: 23 Dec 2011 13:59

Re: Extract file name with largest suffix

#3 Post by Squashman » 14 Dec 2017 23:10

You could just compare the numbers if you used the tokens and delims options.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Extract file name with largest suffix

#4 Post by thefeduke » 15 Dec 2017 22:50

This seems to work on your test file:

Code: Select all

@ECHO OFF&SETLOCAL 
>sortfile.txt      FINDSTR /c:"outbox/logs/Client_" file.txt
Sort /R sortfile.txt /O sortfile.txt
SET /P <sortfile.txt "MAXFILE="
ECHO file is : %MAXFILE%
pause
with the simplest assumptions about the real data.

John A

Post Reply