Page 1 of 1

Strange Issue with FOR LOOP; ignoring last record?

Posted: 17 Jul 2019 04:00
by SIMMS7400
HI Folks -

I have the following script created to output unique values from a list. Prior to this step, I sort a single columntext file extracted from a data-dump from one of my data bases. From here, I need to pull out any unique records.

The issue is when the uniq record is the LAST record, for some reason it doesn't pull it out. Here is my ssyntax:

Code: Select all

                rem Output unique values
                SET "LAST="
                SET "COUNT=0"
                (FOR /F "usebackq delims=" %%A IN ("out2.txt") DO (
                                IF "%%A" equ "!LAST!" (
                                                SET /A "COUNT+=1"
                                ) ELSE (
                                                IF "!COUNT!" equ "1" ECHO Non_Combo_Firms,!LAST!
                                SET COUNT=1
                                )
                                SET "LAST=%%A"
                )) > "%NCF_LF%"
Here is my file, notice the last record is unque but it's not pulling it out. Anyone know why?

Code: Select all

C-12528168
C-12528168
C-12528170
C-12528170
C-12528174
C-12528174
C-12528180

Re: Strange Issue with FOR LOOP; ignoring last record?

Posted: 17 Jul 2019 04:15
by SIMMS7400
To correct for now, I added a second sort with SORT /R.

Re: Strange Issue with FOR LOOP; ignoring last record?

Posted: 17 Jul 2019 09:17
by kwsiebert
I didn't try running it, but it looks like it's only testing and outputting the value from the previous loop. LAST is getting set to C-12528180 only at the very end, and then the loop is done, there are no more tests to check if it should be output. You need one more instance of your output statement following the loop.

Re: Strange Issue with FOR LOOP; ignoring last record?

Posted: 17 Jul 2019 09:58
by SIMMS7400
Thank you, will do that!