Page 1 of 1

Extract file name with largest suffix

Posted: 14 Dec 2017 18:35
by SIMMS7400
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.

Re: Extract file name with largest suffix

Posted: 14 Dec 2017 19:12
by aGerman
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

Re: Extract file name with largest suffix

Posted: 14 Dec 2017 23:10
by Squashman
You could just compare the numbers if you used the tokens and delims options.

Re: Extract file name with largest suffix

Posted: 15 Dec 2017 22:50
by thefeduke
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