2

I have set my arrays like this:

SET ORG[0]=Microsoft
SET ORG[1]=Google
SET ORG[2]=Yahoo
SET ORG[3]=Snapchat
SET ORG[4]=Whatsapp
SET ORG[5]=Facebook

Then I do a find in one of the files to get the value stored in the file:

For /F Tokens^=10^ Delims^=^" %%A in ('Find /I "%Client%"^<"C:\Env\Test\test.xml"') Do Echo "%%A" & SET "ORG=%%A"

Where ORG can be any of the above mentioned array values depending on what's stored in the file. For example say for this instance:

ECHO %ORG% gives Google.

I would like to compare my %ORG% value fetched from the file against the set array values and If its equal then I want my batch script to:

GOTO :Action Else 
ECHO "Client Name Not Found"

I am not sure how to compare a variable with the values in the array?

5
  • And your XML file How it looks ? Commented Jun 19, 2017 at 2:20
  • @Hackoo the information that my find would fetch would be from this line of the xml - <Org Updated="date" Owner="Test" Version="2/1/3/4"Database="Test" Client="Name">. I am able to get 'Name' from the xml so thats not an issue. All i need to know here is how to compare %ORG% variable with the fixed values stored in the array. the solution should cover any additional array value which might be added in the future. Commented Jun 19, 2017 at 2:24
  • @Hackoo I want to check the value fetched from the xml and stored in my %ORG% to be compared against the values stored in the array. Something if %ORG% == ORG[] GOTO Action ELSE ECHO Client Not found & GOTO Action2 please ignore the syntax here i am just trying to explain my requirement Commented Jun 19, 2017 at 2:31
  • 2
    Could you set ORG[ | find /i "%%~A" >NUL && (echo Found) || (echo Not Found) or similar? Commented Jun 19, 2017 at 2:48
  • @rojo I would actually prefer to compare the values because thats the requirement rather than just checking for null or not. Commented Jun 19, 2017 at 5:47

1 Answer 1

2
for /f "tokens=1*delims==" %%a in ('set ORG[ 2^>nul ') do if "%%b"=="%ORG" goto action

echo Client not found.

The set command lists the existing variables that start ORG[ in the format org[3]=Snapchat Setting delims to = only assigns ORG[3] to %%a and Snapchat to %%b.

If such a match is found, the client is valid on the list. If the for exhausts all entries, processing simply proceeds to the next instruction in the file.

To make the comparison case-insensitive, use if /i in place of if

Sign up to request clarification or add additional context in comments.

7 Comments

I have tried the above solution for /f "tokens=1*delims==" %%b in ('set ORG[ 2^>nul ') do if /i "%%c"=="%ORG%" GOTO action` but it doesnt seem to be going to "Action" even when the %ORG% value is found as Microsoft from the xml. Please note that i have changed the value of %%a to %%b because i have used %%A in the previous loop and it has the value fetched from the xml
change the if to an echo which will then display each comparison. You should then be able to determine where the problem lies.
i did and it just proceeds to the next instruction Echo not foundhere is the command for /f "tokens=1*delims==" %%b in ('set ORG[ 2^>nul ') do echo found Echo not found . I am definite the value of ORG is not null as i can see & display it using ECHO %ORG%
I asked you to replace the if with echo for a specific reason. The resultant statement, ` ECHO /i "%%c"=="%ORG%" GOTO action` would display each value of %%c against %ORG%. Since no echo found appears to be generated, it would appear that org[ has not been established. Use a set org command before the for /f... line to see the values of all variables with names starting org (batch is generally case-insensitive, and I hate CaMeLcAsE)
I tried this way and it seems to be ok. What do you think set len=6 set i=0 :loop if %i% equ %len% goto ClientNotFound for /f "usebackq delims== tokens=2" %%j in (set ORG[%i%]) do ( if "%%j"=="%Client%" GOTO RUNPATCH_Custom ) set /a i=%i%+1 goto loop
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.