# Batch file defining values



## gsingh2011 (Jan 11, 2009)

At first I wanted to use ASCII values for character in a batch file I was making. But then I learned that it wasn't possible. So now I'm trying to define my own characters. Is there a way to do that with batch files? For example, I need to define the letter 'a' as 01, so whenever I use the number 01, it interprets it as 'a'. This way if I have something set up like this

set chr=1
:loop
%chr%>>mytext.txt
set chr=chr+1
goto loop

then it write the letter 'a', then chr will equal 2 and print 'b' and so on. If there are mistakes in this script I'm sorry, but you should get what I'm asking.

EDIT: I came back and realized the script is wrong. I thought that might be how you increase a value, but its not. Anyway, I know how to do that, I just forgot. It's in one of my old scripts... The point is the value increase, so the letter changes.


----------



## Squashman (Apr 4, 2003)

You would basically have to put all of the alphabet in a variable. Then have some other variable that is incrementing. Then do a substring of the alphabet variable with your counter variable.

set alphabet=abcdefghi..........etc
set /a var1=var1+1
set alphabit=!alphabet:~%var1%,1! (This step is substring the alphabet variable with your counter)

It goes something like that. I think this will work but I am only going off of examples that Outcaste has given to me in the past. I will ask him if he can check this thread out.


----------



## gsingh2011 (Jan 11, 2009)

I see what you're saying. I'm going to try something like this out, but if you can ask someone that would be great. Thanks for the help.


----------



## TheOutcaste (Aug 8, 2007)

Squashman's idea is correct.
Some suggestions:
*alphabit=!alphabet:~%var1%,1!*
Not sure if *alphabit* was a typo, but you wouldn't want to use alphabet=, or you would have to define the alphabet string all over again. Once the character string *alphabet* is defined, don't change it.
So *char=!alphabet:~%var1%,1!* would be better.
To use delayed expansion (the ! symbols), you need to enable it first with a *Setlocal EnableDelayedExpansion* statement
Remember that the first character position in a variable is zero, not one, so *a* would be selected when var=0. If a has to be one, you just need to add a placeholder to take the "0" position.
So this will output a-z to mytext.txt


```
SetLocal EnableDelayedExpansion
set alphabet=.abcdefghijklmnopqrstuvwxyz
Set var=0
:loop
set /a var1=var1+1
If %var% GEQ 27 Goto :EOF
set chr=!alphabet:~%var1%,1!
Echo %chr%>>mytext.txt
Goto loop
```
You could also create a subroutine to emulate the CHR$(x) function of other languages, at least for ASCII characters from Decimal 34-126:

```
@Echo Off
Set alphabet="#$%%&'()*+,-./0123456789:;<=>[email protected][\]^_`abcdefghijklmnopqrstuvwxyz{|}~
:loop
Set /P ASCII_Value=Enter a number between 34 and 126, inclusive (Zero to exit)
If %ASCII_Value%==0 Goto :EOF
Call :ASCII %ASCII_Value% chr
Echo.%chr%
Goto loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::  Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:error
Echo Value must be between 34 and 126 inclusive
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ASCII Function
:: Usage Call :ASCII X returnvar
:: Returns the decimal Ascii value of X in the variable named returnvar
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ASCII
SetLocal EnableDelayedExpansion
Set /a var=%1-34
If %var% LSS 0 Goto error
If %var% GTR 92 Goto error
Set character=!alphabet:~%var%,1!
Endlocal&Set %2=^%character%&Goto :EOF
```
You could start at #32, but because the *!* symbol is used for Delayed Expansion, it cannot be echoed if Delayed Expansion is enabled. And, it cannot be escaped with the caret (^)
It can be done, but if you will be using Delayed Expansion anywhere in your batch file other than the subroutine, you have to disable it before using any variable that contains a !, then re-enable it afterwards.
Try this to see what I mean:

```
@Echo Off
Set test=Hello !World! Test^!
Echo %test%
SetLocal EnableDelayedExpansion
Echo %test%
SetLocal DisableDelayedExpansion
Echo %test%
```
HTH

Jerry


----------



## Squashman (Apr 4, 2003)

Nope, I meant to say alphabet and alphab*i*t. But I suppose just calling the variable char would be more understandable. I was just doing a play on words.


----------



## gsingh2011 (Jan 11, 2009)

Thank you both! That was very helpful.


----------



## TheOutcaste (Aug 8, 2007)

Squashman said:


> Nope, I meant to say alphabet and alphab*i*t. But I suppose just calling the variable char would be more understandable. I was just doing a play on words.


Good one.:up: I claim proximity to an airport, so lots of stuff goes over my head


----------



## Squashman (Apr 4, 2003)

TheOutcaste said:


> Good one.:up: I claim proximity to an airport, so lots of stuff goes over my head


I learned all this stuff from you and Devil_Himself, so I really can't claim anything other than being a good student!


----------



## derson (Feb 4, 2009)

Naturally, if you have a CHR$ function, you also need an ASC$ function. And a Len$ function never hurts either... Also, the CHR routine below returns a space if the value is outside the printable lower ASCII character set while the ASC function will return a zero if the provided character is outside the printable characters.


```
REM must DisableDelayedExpansion in order to set the alphabet variable as this one includes space and !
SetLocal DisableDelayedExpansion
Set alphabet= !"#$%%&'()*+,-./0123456789:;<=>[email protected][\]^_`abcdefghijklmnopqrstuvwxyz{|}~
SetLocal EnableDelayedExpansion

:CHR
   SetLocal EnableDelayedExpansion
   Set /a var=%1-32
   (Set character= )
   IF %var% GEQ 0 IF %var% LEQ 94 Set character=!alphabet:~%var%,1!
   EndLocal&Set %2=^%character%&Goto :EOF

:ASC
   REM can't pass % or ^ to this function.  pass the variable in "c" OR assign to chr=c
   SetLocal EnableDelayedExpansion
   Set i=0
   :ASC_Loop
      SET test=^!alphabet:~%i%,1!
      REM IF ^%test%==^%chr% Set /A var=i+32& GOTO :ASC_END
      IF ^%test%==^%~1 Set /A var=i+32& GOTO :ASC_END
      IF %i% GEQ 94 Set var=0& GOTO :ASC_END
      Set /A i+=1
      GOTO :ASC_Loop
   :ASC_END
   EndLocal&Set %2=%var%&Goto :EOF

:Len
   SetLocal
   SET /A Len=0
   SET Str=%1
   IF NOT DEFINED Str GOTO :EOF
   SET Str=%Str:"=.%987654321
   :LenLoop
      IF NOT "%Str:~18%"=="" SET Str=%Str:~9%& SET /A Len+=9& GOTO :LenLoop
   SET Num=%Str:~9,1%
   SET /A Len=Len+Num
   EndLocal&Set %2=%Len%&GOTO :EOF
```


----------

