0

So I am the process of creating my first batch file and I am convinced that there is a better way. It seems I can accomplish what I want but from all the conventions I have read it seems sloppy.

@echo off

set /p xyzCode= Please enter the code.

set x=0001
set xyz=0002
set xyza=0003

if %xyzCode% ==x echo %x%

Problem is I would need about 600 variables, 600 if statements.

Where I work a product has two unique identifiers a code, and a materiel identifier,

I want to be able to quickly type in a code and get the materiel identifier.

If I were using Python I think the answer would be use dicts but need a way to do this in batch scripting.

hagello
  • 2,843
  • 2
  • 27
  • 37
Shadows
  • 3
  • 1
  • This question may be a duplicate of http://stackoverflow.com/questions/18423443/switch-statement-equivalent-in-windows-batch-file – hagello Feb 23 '16 at 19:59

3 Answers3

1

You might try keeping a separate text or csv file of all your codes and identifiers like this:

Code&ItemList.txt

  Code, Material Id
  0001, Abcd
  0123, Qwerty
  4567, Zxcvb

Then, you can write your batch script something like this:

@echo off
set /p xyzCode= Please enter the code:
for /f "skip=1 tokens=1,2 delims=, usebackq" %%a IN ("Code&ItemList.txt") do (
    if %xyzCode%==%%a echo Material ID is %%b
)

Type for /? for explanations of how and why that works.

Wes Larson
  • 1,042
  • 8
  • 17
  • Hi Wes I really like this idea , I have copied your answer into a .bat file and am playing with it at the moment. I have to learn exactly what all it means haha. I think this will ultimately solve my problem. I want to thoroughly understand and learn the for statement you used before I move on with my program. Sincere Thanks ! – Shadows Feb 24 '16 at 03:22
  • If the number of codes is large, then performance can be dramatically improved by using FINDSTR to quickly get the matching line, such that FOR /F only has one iteration. The FOR IN clause would become something like `('findstr "^%xyzCode%," "Code&ItemList.txt"')`, and you would remove the `usebackq` option. – dbenham Feb 24 '16 at 20:23
1

If I understand correctly, I think you can use delayed expansion to do what you want.

Add SETLOCAL ENABLEDELAYEDEXPANSION at the top of your script. Then, add echo !%xyzCode%! at the bottom to display the value of the code you entered.

Like this:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

set /p xyzCode=Please enter the code. 

set x=0001
set xyz=0002
set xyza=0003

echo !%xyzCode%!

This way you do not need to have an IF statement for every possible code. If you enter xyz, this script will display the value of the variable named xyz.

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • The concept you use in this solution is called _array_ and it is fully explained at http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Feb 24 '16 at 01:05
  • 1
    @Aacini - I don't see any array in this answer. There is no base name or index value, but seemingly random codes that have associated values. So xyzCode is somewhat like a pointer to a variable with a value. – dbenham Feb 24 '16 at 02:08
  • @dbenham: From [this definition](https://en.wikipedia.org/wiki/Array_data_structure): _"In computer science, an array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key."_ and _"The term array ... consists of a collection of values or variables that can be selected by one or more indices computed at run-time"_. – Aacini Feb 24 '16 at 18:49
  • @dbenham: Also, from [this one](https://en.wikipedia.org/wiki/Associative_array): _"In computer science, an associative array, ... is an **abstract data type** composed of a collection of (key, value) pairs, such that each possible key appears just once in the collection"_. IMHO this problem, and the way to solve it in this solution, matchs exactly these definitions... – Aacini Feb 24 '16 at 18:50
  • 1
    @Aacini - your cited definitions match my understanding, and also are consistent with my comment. If the variables were named something like `cd.x`, `cd.xyz`, `cd.xyza`, or `cd[x]`, `cd[xyz]`, `cd[xyza]`, then I would agree that they were functioning as an associative array. But that is not what is in the code. – dbenham Feb 24 '16 at 20:08
  • @Aacini - I agree that structuring the variable names to emulate an associative array would be an improvement to the code. – dbenham Feb 24 '16 at 20:17
  • @dbenham: Perhaps a more exact matching make things clearer: _"... an array ... is a ... collection of ... variables, each identified by at least one key"_ like `x`, `xyz` and `xyza`. _"The term array ... consists of a collection of ... variables"_ like `x`, `xyz` and `xyza` _"that can be selected by one index computed at run-time"_ like `xyzCode`. _"... an associative array ... is an **abstract data type** composed of a collection of (key, value) pairs"_ like `(x,0001)`, `(xyz,0002)` and `(xyza,0003)`. – Aacini Feb 25 '16 at 16:58
  • @dbenham: I want add that I referred to _the concept_ used in this solution and that encyclopedia definitions explain _concepts_ before describe the methods used to _implement_ such concepts. I invite you to refute this explanation not based on "your understanding" of this theme, but on a _definition_ copied from any site, like [this one](http://rosettacode.org/wiki/Abstract_type): _"The term **abstract data type** may denote a type with an implementation provided by the programmer rather than directly by the language"_. – Aacini Feb 25 '16 at 16:58
  • 1
    @Aacini - Your supplied definitions are all I need. The key concept where we disagree is the word "collection". Add additional variables named `abc`, `xxx`, `foo`. Which variables are considered to be part of the same array collection? In my interpretation of your definitions, an array requires some base name or notation to identify the collection, in addition to the index that specifies a single element within the collection. If you cannot see my point, then I guess we will have to agree to disagree, which won't be the first time. – dbenham Feb 25 '16 at 17:39
  • @dbenham: I think that the core point of our disagreement is that I am talking about _abstract concepts_ that may match a problem, but you refer to the practical use of an _implemented feature_. An example of this point: if I write `elem[1]=One` at hand in a piece of paper and I state: "This is an array (_concept_)", you can't deny that. If you say that X programming language does not manage (_native_) arrays because it have not _predefined facilities_ for such management, I can't deny that. The problems arise when these two limits start to blur and mix. May we agree in this point, perhaps? – Aacini Feb 25 '16 at 19:23
0

Sounds like a job for a switch case

switch statement equivalent in windows batch file

It's still going to take up a lot of space, but I believe they're much faster than IF statements

Community
  • 1
  • 1
Orion
  • 333
  • 1
  • 3
  • 11