[SOLVED] [Batch] IF...THEN and Arrays

AoN

Internet Programmer
Joined
Aug 1, 2012
Posts
114
Now, anyone that has read through the reads I've made should know, I am particallys braindead when it comes to these languages. I can code in PHP, integrating MySQL databases, JavaScript, CSS, AJAX, and so much else without batting an eye, but simple batch and VBScript is murder. :(

So, hopefully this one is relatively easy, compared to my last major project (the profile rebuild script).

Basically, since noone could figure out why we could get the "Documents" directory to copy, no matter what, they basically told me not to bother wasting more time on it and that they would submit it for approval with a note that agents would still have to remote into machine for that one directory. Now, I'm going through our hundreds of "fixes" and fixing them. Many use sloppy commands and execution methods, and even more don't take into account that our Win7 machines do not have partitioned drives like the XP machines do.

Well, this current fix is kicking my butt, mainly because I don't understand these web documents from 1992 explaining how to use arrays in batches. So, simply put, the script pulls two pieces of information from the target computers FQMN. The first piece will determine the order of some DNS suffixes. That second piece is the problem. I get it, no sweat, but I can't use that piece as it is. Instead, I need to compare it to a master list to determine what is needed for the script to run.

Yeah, vague, sorry, network-related complications with providing details.

Basic concept, I have several lists, the longest being 1,796 possibilities. Basically, if any of those that second piece of information retrieved from the FQMN is one of those 1,796 then I need to set a third variable to 1. If that second piece is from one of the another list, the third variable is 2, etc.

Now, I know I can test the second piece of information individually to determine that third variable, but considering that there's 1,796 possibilities of it being 1, I'd rather not have to make 1,796 if statements.

Let me know if that was too confusing and I'll try to be clearer. :S
 
nvm, I worked around the need for the list by pulling information from the domain instead of the machine. Probably less code anyways. ^^'
 
I find your original post too confusing to follow, where's the question you want answered? lol!

By your title, that's the only thing I see that helps me understand the issue. I'll write an example though, as there really isn't anything in batch that would support an array of variables, you're going to have to mock up your own method.

Code:
[PLAIN]@echo off & setlocal enabledelayedexpansion
set arrCount=0
for /f %%G in (Z:\file.txt) do (
	set arr!arrCount!=%%G
	set /a arrCount+=1
)

:loop
set /p index=Get value at index: 
set value=!arr%index%!
if defined value (
	echo %value%
) else (
	echo NULL
)
goto loop

pause[/PLAIN]

Okay, so here I had a file located at Z:\file.txt that looked like this:
Code:
line_1 testing
line_2 testing
line_3 testing
line_4 testing
line_5 testing
line_6 testing
line_7 testing
line_8 testing
line_9 testing
line_10 testing

Array indexing traditionally starts at 0, so that's what was done here as well. If out of bounds, unlike other languages, this piece of code will return null instead of a runtime exception. If you wanted you could write a method to get the length of an array, assuming that numbers were consistent here... You'd just have to loop through the name and indexes to find something that is not defined , and then you would use that + 1 to get the length, or the last index that had a value that was defined to get the upperbound of the array (the last index that holds a value in the array, not the length; thus the length - 1).

One thing to note here is that in this way, there's no defined collection range here, so if you remove a value, in this way, it acts like an array, but because you don't have to allocate space in memory for this range of values, it also acts more like a list instead of an array.
 
Last edited:
Yeah, that's what I was afraid of. As I had mentioned, I've read a few support pages about "arrays in batch", but they were an older generation of technician whom wrote them, so I had a hard time understand what they had written. Playing around with it, I found a simplier, but more evasive solution to my original problem. I still can't give details due to the situation but I'll try to make it a little easier to understand.

Basically, I start with a computer name from our network. These names have a type, location, and ID number combined to form the name. Now, each type has different access restrictions, including domains that they are permitted access to. These restrictions are enforced using the DNS suffixes (yes, I know this only prevents using the machine name, not IP address, not my call). Anyways, the DNS suffixes are then determined by the locations server farm. So, we could have one location, or two-hundred, utilizing the same server farm depending upon their geographical location. I was trying to use the location, pulled from the machine name, to designate the farm, then utilize the type to build the DNS suffixes registry entry. Initially, the easiest method, to me, seemed to be arrays, but as I did more research into networks, I decided to use an "nslookup" for the machine to pull the farm data directly from the DNS. It's a bit sloppier, and I imagine slower to process (its a big network), than simply breaking up the name, but it works.
 
Grrr...inability to edit posts...

Personally, I'd've preferred to have done the whole thing in VBScript, since I'm getting more comfortable with using it as a computer programming language, but I have my hands tied on most of what I've been working on.

I'm sure I'll be looking for more help was I delve further into updating our administrative fixes and tools for our network. It's just not the same as programming in PHP, MySQL, HTML, and JavaScript. If I could integrate it into the system, I'd do all of it in PHP and MySQL. So much simplier! ^^'

Anyways, I'll play around with your "array" script. As I read more and more, I started to understand those older articles and they basically say the same thing. Arrays aren't supported by batch, but you can emulate the functionality of arrays through complex coding, just like being able to emulate the functionality of anything if you put the effort into building the functions.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top