How to arrange and array/list?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

How to arrange and array/list?

#1 Post by SIMMS7400 » 02 Jan 2022 17:54

Hi Folks -

I have the following array (well, I believe it's technically called a "list):

Code: Select all

@echo off
setlocal EnableDelayedExpansion

FOR %%A IN (
	"CITY.txt|CITY_JOB|CITY_Errors.zip|"
	"COUNTY.txt|COUNTY_JOB|COUNTY_Errors.zip|2"
	"REGION.txt|REGION_JOB|REGION_Errors.zip|1"
) DO SET "GROUP=%%~A" & CALL :DEFINE_ARRAY


:: Order file list from greatest to least
:: end result should be comma delimited variable as such = COUNTY.txt,REGION.txt,CITY.txt



pause

:DEFINE_ARRAY

FOR %%v IN ("!GROUP:||=|null|!") DO FOR /F "tokens=1-4 delims=|" %%A IN ("%%~v") DO (
    IF NOT "[%%B]"=="[null]" SET "STR[%%A].OLU_JOB=%%B"
    IF NOT "[%%C]"=="[null]" SET "STR[%%A].OLU_ERR=%%C"
    IF NOT "[%%D]"=="[null]" SET "STR[%%A].OLU_TYPE=%%D"
)
GOTO :EOF
What happens is that there is a directory with all oft he above files so I read them into a for loop. But what I need do is arrange the file names in a comma delimited list from highest to lowest. You'll see the 4th position of the array/list definition dictates the order. I'm sure there is an easy way to do this but can't figure it out. Thanks!

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

Re: How to arrange and array/list?

#2 Post by Squashman » 02 Jan 2022 18:38

Dbenham created JSORT.bat which can sort by a defined column and delimiter.

I guess you don't remember this solution given to you a few years ago.

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

Re: How to arrange and array/list?

#3 Post by SIMMS7400 » 03 Jan 2022 02:36

Squash -

Either my explanation wasn't good enough or you're still hitting the egg-nog. Dave's JSORT solution does not work for me in this situation. I'm not looking to sort file contents or files themselves. As stated, I'm using the 4th position in my array/list definition to dictate how I need to process the files. Once I have that variable with the files in the order I need, I then will loop through the directory where they exist and process them accordingly.

I'm looking for a way put the files in "highest to lowest order" based on the OLU_TYPE value.

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

Re: How to arrange and array/list?

#4 Post by SIMMS7400 » 03 Jan 2022 04:34

I suppose I was over thinking. I can force the order I need to process the files in based on my array definition. Here is what I came up with and it works, but not sure if this is considered "too clunky". I'm sure there are better ways?

Code: Select all

@echo off
setlocal EnableDelayedExpansion

SET "FILE_BIN=C:\TEMP\"

FOR %%A IN (
    "REGION|REGION_JOB|REGION_Errors.zip"
    "COUNTY|COUNTY_JOB|COUNTY_Errors.zip"
	"CITY|CITY_JOB|CITY_Errors.zip"
) DO SET "GROUP=%%~A" & CALL :DEFINE_ARRAYS


:: Body of script - start

PUSHD "%FILE_BIN%"

    REM Ensure all array definitions can be matched to a file in %FILE_BIN%
    FOR %%A IN ("*") DO (
        SET "FILENAME=%%~nxA"
		CALL :MASK
    )

	REM Exit if error detected
	IF DEFINED ERR EXIT /B 1
    
    REM Modify file system time to force defined order based on FILE_CHECK order
    FOR %%A IN (%FILE_LIST%) DO (
        FOR %%a IN ("*") DO (
            SET "FILENAME=%%~nxa"
            FOR /L %%1 IN (100,-1,1) DO (  
                IF /I ["!FILENAME:~0,%%1!"]==["%%~A"] (
                    IF NOT DEFINED STR[%%~nxa] (
                        COPY "!FILENAME!",,+ >nul && (SET "STR[%%~nxa]=T")
                        echo !FILENAME!
                    )
                )
            )
        )
    )
POPD

::-- Process oldest file first --::
FOR /F "tokens=*" %%A IN (
	'DIR /b /a-d /tw "%FILE_BIN%*"2^>nul'
) DO echo "%%~nxA"
pause


:DEFINE_ARRAYS 

FOR %%v IN ("!GROUP:||=|null|!") DO FOR /F "tokens=1-3 delims=|" %%A IN ("%%~v") DO (
    IF NOT "[%%B]"=="[null]" SET "STR[%%A].OLU_JOB=%%B"
    IF NOT "[%%C]"=="[null]" SET "STR[%%A].OLU_ERR=%%C"
    
     SET "FILE_LIST=!FILE_LIST! "%%~A""
)
GOTO :EOF

:MASK
::-- Determine Array Definition --::
FOR %%a IN ("DLR" "OLU_JOB") DO (
    FOR /L %%A IN (100,-1,1) DO (

        CALL SET "MASK=%%FILENAME:~0,%%A%%"
        CALL SET "_CHECK=%%STR[!MASK!].%%~a%%"
        
        REM Breakout of loop once match is found
        IF DEFINED _CHECK EXIT /B 0
        
    )
)
echo file name could not be matched
SET "ERR=T"
EXIT /B 1

Post Reply