Determine lowest & highest number from column

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Determine lowest & highest number from column

#1 Post by SIMMS7400 » 13 Jun 2019 18:29

Hi Folks -

What is the easiest way to determine the lowest and highest number from a specific column? Is it JSORT?

I have a csv file with (3) columns. Column (2) is the column I need to check and the numbers will range from 01 to 12.

Example:
State,01,2018
State,03,2018
State,05,2018
State,01,2019
State,09,2018
State,10,2018
State,12,2018
State,10,2019
Thanks!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Determine lowest & highest number from column

#2 Post by SIMMS7400 » 14 Jun 2019 02:15

I arrived at this solution, although I'm not sure if it's as efficient as it can be:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

(  
	FOR %%F IN ("*.csv") DO (

      REM Sort PERIOD Column in reverse order to get MAX value
		SET "MAX="
		SET "MIN="
		FOR /F "tokens=2,3 delims=," %%A IN ('SORT /R "%%F"') DO (
			IF NOT DEFINED MAX (
				SET "MAX=%%A"
				SET "MAXYR=%%B"
				echo Period = !MAX!
				echo Year = !MAXYR!
			 )
		)
		REM Sort PERIOD Column in regular order to get MIN value
		FOR /F "tokens=2,3 delims=," %%A IN ('SORT "%%F"') DO (
			IF NOT DEFINED MIN (
				SET "MIN=%%A"
				SET "MINYR=%%B"
				echo Period = !MIN!
				echo Year = !MINYR!
			 )
		)
   )
)

pause
Any thoughts?

Post Reply