Strange Issue with FOR LOOP; ignoring last record?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Strange Issue with FOR LOOP; ignoring last record?

#1 Post by SIMMS7400 » 17 Jul 2019 04:00

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

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

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

#2 Post by SIMMS7400 » 17 Jul 2019 04:15

To correct for now, I added a second sort with SORT /R.

kwsiebert
Posts: 42
Joined: 20 Jan 2016 15:46

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

#3 Post by kwsiebert » 17 Jul 2019 09:17

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.

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

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

#4 Post by SIMMS7400 » 17 Jul 2019 09:58

Thank you, will do that!

Post Reply