Question with Array setup

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Question with Array setup

#1 Post by SIMMS7400 » 12 Sep 2021 16:02

Hi Folks -

I have a unique situation that I'm wondering how to solve. I have an array and i'm initializing as follows:

Code: Select all

FOR %%a IN (
	"FDR|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRBS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRCS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
) DO FOR /F "tokens=1-4 delims=|" %%A IN (%%a) DO (
        SET "STR[%%A].DLR=%%B"
        SET "STR[%%A].LOC=%%C"
        SET "STR[%%A].IMP=%%D"
        SET "STR[%%A].EXP=%%E"
)
The first part of the array (%%A) is a portion (i.e. a "mask") of a file which is part of a file I will be deriving at a later step. After the array is initialized, I'm going to another function which scans an "Inbox" directory and sets a file to variable called FILENAME. Based on the value of FILENAME, I then need to find the portion of the array that match the file name.

I can dynamically build a check each time I set the FILENAME variable, but where I'm failing is once it hits "FDR", it always stops there because it always renders true. Here is what I tried:

Code: Select all

FOR /L %%A IN (1,1,100) DO (
    CALL SET "MASK=%%FILENAME:~0,%%A%%"
    CALL SET "_CHECK=%%STR[!MASK!].DLR%%"
    
    IF DEFINED _CHECK GOTO BREAK
)
:BREAK
echo %MASK%
echo %_CHECK%
pause
Here are my file names:

Code: Select all

FDR_20210912.csv
FDRBS_20210912.csv
FDRCS_20210912.csv
How do I get around this? Or should I rename the "FDR" file to make it more unique?

I hope I made sense, let me know if you need more info.

T3RRY
Posts: 249
Joined: 06 May 2020 10:14

Re: Question with Array setup

#2 Post by T3RRY » 13 Sep 2021 03:05

SIMMS7400 wrote:
12 Sep 2021 16:02
Hi Folks -

I have a unique situation that I'm wondering how to solve. I have an array and i'm initializing as follows:

Code: Select all

FOR %%a IN (
	"FDR|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRBS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRCS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
) DO FOR /F "tokens=1-4 delims=|" %%A IN (%%a) DO (
        SET "STR[%%A].DLR=%%B"
        SET "STR[%%A].LOC=%%C"
        SET "STR[%%A].IMP=%%D"
        SET "STR[%%A].EXP=%%E"
)
The first part of the array (%%A) is a portion (i.e. a "mask") of a file which is part of a file I will be deriving at a later step. After the array is initialized, I'm going to another function which scans an "Inbox" directory and sets a file to variable called FILENAME. Based on the value of FILENAME, I then need to find the portion of the array that match the file name.

I can dynamically build a check each time I set the FILENAME variable, but where I'm failing is once it hits "FDR", it always stops there because it always renders true. Here is what I tried:

Code: Select all

FOR /L %%A IN (1,1,100) DO (
    CALL SET "MASK=%%FILENAME:~0,%%A%%"
    CALL SET "_CHECK=%%STR[!MASK!].DLR%%"
    
    IF DEFINED _CHECK GOTO BREAK
)
:BREAK
echo %MASK%
echo %_CHECK%
pause
Here are my file names:

Code: Select all

FDR_20210912.csv
FDRBS_20210912.csv
FDRCS_20210912.csv
How do I get around this? Or should I rename the "FDR" file to make it more unique?

I hope I made sense, let me know if you need more info.
If your hardcoding definitions, or using tokens, you don't necessarily need to change conflicting names, simple use a character to flag the end of the string.

That said, powershell is much more efficient with regards to arrays, both in establishing and manipulating them, and has the added benefit of opening up regex -match and -replace options.

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

Re: Question with Array setup

#3 Post by SIMMS7400 » 13 Sep 2021 03:24

Thanks, T! Where would I add that character? To the array itself when it initializes?

T3RRY
Posts: 249
Joined: 06 May 2020 10:14

Re: Question with Array setup

#4 Post by T3RRY » 13 Sep 2021 06:34

SIMMS7400 wrote:
13 Sep 2021 03:24
Thanks, T! Where would I add that character? To the array itself when it initializes?
Change the logic a bit in the mask / _Check setion to make use of the variables ] as the terminating character. (Untested):

Code: Select all

    CALL SET "MASK=%%FILENAME:~0,%%A%%]"
    CALL SET "_CHECK=%%STR[!MASK!.DLR%%"
    
    If defined _CHECK  (
      Set ""MASK=!Mask:~0,-1!" %= remove this line if mask var not used again =%
      GOTO BREAK
     )


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

Re: Question with Array setup

#5 Post by SIMMS7400 » 13 Sep 2021 06:44

Thanks, T, but that didn't work.

Lets say I have FDRBS_20210912.csv. it's still returning the array definition for the FDR line, not the FDRBS definition.

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

Re: Question with Array setup

#6 Post by Squashman » 13 Sep 2021 07:42

Why wouldn't you use the underscore as a delimiter from the filename to help you match the array?

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

Re: Question with Array setup

#7 Post by SIMMS7400 » 13 Sep 2021 07:58

Squash -

Absolutely I can. But there will be situationnwhere I won't have the delimiter to use and will be stuck with this situation that I'm hopping to solve with some additional logic

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

Re: Question with Array setup

#8 Post by Squashman » 13 Sep 2021 08:14

SIMMS7400 wrote:
13 Sep 2021 07:58
Squash -

Absolutely I can. But there will be situationnwhere I won't have the delimiter to use and will be stuck with this situation that I'm hopping to solve with some additional logic
Well you certainly can't do the substring from left to right because that will always match your shorter array variable first that has the same string at the beginning of your longer array variables. So I see your only solution as looping through from right to left and subtracting 1 from the string comparison. It is somewhat inefficient but should be rather quick if you are not going much bigger than 100 for your file names.

Another thing to consider is breaking out of a FOR /L. You have to use special logic for that to happen. It has been discussed several times on the forums. So you will want to look that up. It is not doing what you think it is doing.

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

Re: Question with Array setup

#9 Post by SIMMS7400 » 13 Sep 2021 08:40

Thanks, Squash! What do you mean "subtract"?

I'll also review the threads on how to properly break out of a loop.

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

Re: Question with Array setup

#10 Post by Squashman » 13 Sep 2021 08:47

SIMMS7400 wrote:
13 Sep 2021 08:40
What do you mean "subtract"?

Code: Select all

FOR /L %%A IN (10,-1,1) DO......

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

Re: Question with Array setup

#11 Post by SIMMS7400 » 13 Sep 2021 09:06

Squash, thank you sir. That seemed to do the trick!!

Now one more question, if perhaps the string on the RIGHT side of the file, like this:
20210913_FDRBS.csv
Is it also possible to search that way too?

Aacini
Expert
Posts: 1910
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Question with Array setup

#12 Post by Aacini » 13 Sep 2021 14:26

I think this do what you want:

Code: Select all

set "MASK=%FILENAME%"

:nextChar
set "MASK=%MASK:~0,-1%"
if not defined STR[%MASK%].DLR goto nextChar

echo File: %FILENAME%, MASK: %MASK%
Antonio

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

Re: Question with Array setup

#13 Post by SIMMS7400 » 18 Sep 2021 07:57

Thanks Squash and Aacni - both options worked great!

Post Reply