Error in setting variable in FOR loop & extracting text from a textfile

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
One Batch
Posts: 8
Joined: 30 Aug 2015 04:58

Error in setting variable in FOR loop & extracting text from a textfile

#1 Post by One Batch » 16 Oct 2017 07:21

In a batch script that I am writing I got a problem with a variable %calc% that is not showing the correct value.
It should display per letter from a word that is pulled from the file Woorden.txt. And per letter check which letter it is, and change some values. Then go to the next letter and continue till all letters are processed. Then go to the next word on the list and go through the process again, until all words are processed.
Even if the error is fixed so that the %calc% does show the correct letter, and processes the name from the file Woorden.
How would I make the loop so that after all letters of a word are processed that it doesnt exit like it does now, but that it continuous to extract the next word in the file Woorden.txt?

Code: Select all

aecho off
setlocal enabledelayedexpansion
setlocal enableextensions
:variable
set simplenum=
set previousword=%word%
set word=
set characters=
set reverseword=
set startw=0
set lengthw=1
set calc=

:nextletter
for /f "tokens=1* delims=" %%A in (woorden.txt) do (
call set calc=%%A:~%startw%,%lengthw%%%%
call :calculate
set /a startw=%startw%+1
goto nextletter

:calculate
IF '%calc%' EQU '' echo EXIT
IF '%calc%' EQU 'a' set /a simplenum=%simplenum%+1&&set /a characters=%characters%+1&&set word=%word%A&&echo %word%&&set reverseword=%reverseword%Z
IF '%calc%' EQU 'b' set /a simplenum=%simplenum%+2&&set /a characters=%characters%+1&&set word=%word%B&&echo %word%&&set reverseword=%reverseword%Y
IF '%calc%' EQU 'c' set /a simplenum=%simplenum%+3&&set /a characters=%characters%+1&&set word=%word%C&&echo %word%&&set reverseword=%reverseword%X
IF '%calc%' EQU 'd' set /a simplenum=%simplenum%+4&&set /a characters=%characters%+1&&set word=%word%D&&echo %word%&&set reverseword=%reverseword%W
IF '%calc%' EQU 'e' set /a simplenum=%simplenum%+5&&set /a characters=%characters%+1&&set word=%word%E&&echo %word%&&set reverseword=%reverseword%V
IF '%calc%' EQU 'f' set /a simplenum=%simplenum%+6&&set /a characters=%characters%+1&&set word=%word%F&&echo %word%&&set reverseword=%reverseword%U
IF '%calc%' EQU 'g' set /a simplenum=%simplenum%+7&&set /a characters=%characters%+1&&set word=%word%G&&echo %word%&&set reverseword=%reverseword%T
IF '%calc%' EQU 'h' set /a simplenum=%simplenum%+8&&set /a characters=%characters%+1&&set word=%word%H&&echo %word%&&set reverseword=%reverseword%S
IF '%calc%' EQU 'i' set /a simplenum=%simplenum%+9&&set /a characters=%characters%+1&&set word=%word%I&&echo %word%&&set reverseword=%reverseword%R
IF '%calc%' EQU 'j' set /a simplenum=%simplenum%+10&&set /a characters=%characters%+1&&set word=%word%J&&echo %word%&&set reverseword=%reverseword%Q
IF '%calc%' EQU 'k' set /a simplenum=%simplenum%+11&&set /a characters=%characters%+1&&set word=%word%K&&echo %word%&&set reverseword=%reverseword%P
IF '%calc%' EQU 'l' set /a simplenum=%simplenum%+12&&set /a characters=%characters%+1&&set word=%word%L&&echo %word%&&set reverseword=%reverseword%O
IF '%calc%' EQU 'm' set /a simplenum=%simplenum%+13&&set /a characters=%characters%+1&&set word=%word%M&&echo %word%&&set reverseword=%reverseword%N
IF '%calc%' EQU 'n' set /a simplenum=%simplenum%+14&&set /a characters=%characters%+1&&set word=%word%N&&echo %word%&&set reverseword=%reverseword%M
IF '%calc%' EQU 'o' set /a simplenum=%simplenum%+15&&set /a characters=%characters%+1&&set word=%word%O&&echo %word%&&set reverseword=%reverseword%L
IF '%calc%' EQU 'p' set /a simplenum=%simplenum%+16&&set /a characters=%characters%+1&&set word=%word%P&&echo %word%&&set reverseword=%reverseword%K
IF '%calc%' EQU 'q' set /a simplenum=%simplenum%+17&&set /a characters=%characters%+1&&set word=%word%Q&&echo %word%&&set reverseword=%reverseword%J
IF '%calc%' EQU 'r' set /a simplenum=%simplenum%+18&&set /a characters=%characters%+1&&set word=%word%R&&echo %word%&&set reverseword=%reverseword%I
IF '%calc%' EQU 's' set /a simplenum=%simplenum%+19&&set /a characters=%characters%+1&&set word=%word%S&&echo %word%&&set reverseword=%reverseword%H
IF '%calc%' EQU 't' set /a simplenum=%simplenum%+20&&set /a characters=%characters%+1&&set word=%word%T&&echo %word%&&set reverseword=%reverseword%G
IF '%calc%' EQU 'u' set /a simplenum=%simplenum%+21&&set /a characters=%characters%+1&&set word=%word%U&&echo %word%&&set reverseword=%reverseword%F
IF '%calc%' EQU 'v' set /a simplenum=%simplenum%+22&&set /a characters=%characters%+1&&set word=%word%V&&echo %word%&&set reverseword=%reverseword%E
IF '%calc%' EQU 'w' set /a simplenum=%simplenum%+23&&set /a characters=%characters%+1&&set word=%word%W&&echo %word%&&set reverseword=%reverseword%D
IF '%calc%' EQU 'x' set /a simplenum=%simplenum%+24&&set /a characters=%characters%+1&&set word=%word%X&&echo %word%&&set reverseword=%reverseword%C
IF '%calc%' EQU 'y' set /a simplenum=%simplenum%+25&&set /a characters=%characters%+1&&set word=%word%Y&&echo %word%&&set reverseword=%reverseword%B
IF '%calc%' EQU 'z' set /a simplenum=%simplenum%+26&&set /a characters=%characters%+1&&set word=%word%Z&&echo %word%&&set reverseword=%reverseword%A
)
echo %simplenum% %word%>>DATAbases.txt
GOTO :EOF


Content of Woorden.txt:
we
were
werein
whatiteachyou
howtoget
makethis
makethiswork
cometogether
information

After running this batchscript from a CMD /K, command prompt I get the message:
1' was unexpected at this time
IF 'we:~0,1' EQU '' EXIT

Code: Select all

call set calc=%%A:~%startw%,%lengthw%%%%

I think the error is in this particular line, but I dont know for sure.
What is causing the error to come up?

I also want to ask what the proper way of using the call set command is. As far as my research goes, I couldnt find proper info.
Have I used the right code here to set the calc value to the 0,1 position of a string?
I want the first letter of a word which is in the variable %%A to be set to the variable calc.
And after that, I increase the variable, and check for the second letter, the third, fourth etc.
Which below that line is checked with a series of IF's.

Code: Select all

call set calc=%%A:~%startw%,%lengthw%%%%
call :calculate
set /a startw=%startw%+1
goto nextletter

:calculate
IF '%calc%' EQU '' echo EXIT
IF '%calc%' EQU 'a' set /a simplenum=%simplenum%+1&&set /a characters=%characters%+1&&set word=%word%A&&echo %word%&&set reverseword=%reverseword%Z


After each cycle of checking which letter is found in the %calc% variable, I want it to return to this command:
call :calculate
And start the process again with the next letter. Im not sure if my current use of the FOR loop and closing parenthesis is done in the right way. Because I think the GOTO :EOF command should be on the inside of the loop, and not on the outside of the FOR loop.

How do I solve these problems?
I wish to have provided enough information. And im willing to provide clarify if needed.

Thanks in advance

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

Re: Error in setting variable in FOR loop & extracting text from a textfile

#2 Post by Squashman » 16 Oct 2017 07:56

You can't do substrings with a FOR variable. You have to assign it to an environmental variable first.

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

Re: Error in setting variable in FOR loop & extracting text from a textfile

#3 Post by Aacini » 16 Oct 2017 09:59

And you have not shown what is the expected output given your example input. Sorry, but I don't understand your involved explanation...

Antonio

One Batch
Posts: 8
Joined: 30 Aug 2015 04:58

Re: Error in setting variable in FOR loop & extracting text from a textfile

#4 Post by One Batch » 17 Oct 2017 08:52

Thanks for replying.

My desired output is a textfile called DATAbases.txt, in here i want to have the output of all words letter by letter from the Woorden.txt, but specifically, I want to use a batchfile that adds a value to a variable, the value depends on the placement of the letter in the alphabet. IF A is found, add by 1, If B is found add by 2 etc.
And thus, I want the following format, for example the word 'we' will be shown in the DATAbases.txt:
23 W
5 E
So first the number and then the letter after it.

Do I have to assign the variable within the FOR loop or before?
I tried to change the lines of code under the :nextletter label, but I dont quite understand it.

changed code:
added set words=%%A
added %words% in call set calc=%words%:~%startw%,%length%%%%

Code: Select all

:nextletter
for /f "tokens=1* delims=" %%A in (woorden.txt) do (
set words=%%A
call set calc=%words%:~%startw%,%lengthw%%%%
call :calculate
set /a startw=%startw%+1
goto nextletter

And using the above change code I get a similar output of:
'1 was unexpected at this time.
IF 'we:~0,1' EQU '' EXIT

In the code shown above, I did assign the %%A to a variable with the name 'words'.
But that test didnt succeed, because of the error.
How do I set the environment variable correctly?

Thanks

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Error in setting variable in FOR loop & extracting text from a textfile

#5 Post by Compo » 17 Oct 2017 18:46

I'm not sure I understand your requirements, is this the output you're looking for:

Code: Select all

23 W
5 E
23 W
5 E
18 R
5 E
23 W
5 E
18 R
5 E
9 I
14 N
23 W
8 H
1 A
20 T
9 I
20 T
5 E
1 A
3 C
8 H
25 Y
15 O
21 U
8 H
15 O
23 W
20 T
15 O
7 G
5 E
20 T
13 M
1 A
11 K
5 E
20 T
8 H
9 I
18 S
13 M
1 A
11 K
5 E
20 T
8 H
9 I
18 S
23 W
15 O
18 R
11 K
3 C
15 O
13 M
5 E
20 T
15 O
7 G
5 E
20 T
8 H
5 E
18 R
9 I
14 N
6 F
15 O
18 R
13 M
1 A
20 T
9 I
15 O
14 N

One Batch
Posts: 8
Joined: 30 Aug 2015 04:58

Re: Error in setting variable in FOR loop & extracting text from a textfile

#6 Post by One Batch » 18 Oct 2017 08:48

Yes, Compo. That is the desired output that I want.
With the current code, I somehow have wrongly set the variable %%A to the variable called words.
This command is found in the code starting with the label :nextletter
Because instead of only one letter to be shown in the variable called calc when the IF statements are checked I get this error.
'1 was unexpected at this time.
IF 'we:~0,1' EQU '' EXIT

Code: Select all

:nextletter
for /f "tokens=1* delims=" %%A in (woorden.txt) do (
set words=%%A
call set calc=%words%:~%startw%,%lengthw%%%%
call :calculate
set /a startw=%startw%+1
goto nextletter

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

Re: Error in setting variable in FOR loop & extracting text from a textfile

#7 Post by Aacini » 18 Oct 2017 10:23

I am sorry, I don't like to write extensive explanations. I prefer to show working code that include comments:

Code: Select all

@echo off
setlocal

rem Create a mapping array with this form: valueOf[a]=1, valueOf[b]=2, etc...
set i=0
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
   set /A "i+=1, valueOf[%%a]=i"
)

rem Process all *lines* (with one word each) in input file
for /F "delims=" %%A in (woorden.txt) do (
   set "word=%%A"
   call :calculate
)
goto :EOF


:calculate

set "characters=0"
set "reversedWord="

rem Process *the word* in "word" variable
echo Word: %word%

:nextLetter
   rem Take the first character of "word" into "letter"
   set "letter=%word:~0,1%"
   set /a simpleNum=valueOf[%letter%]
   echo %simpleNum% %letter%
   set /a characters+=1
   set "reversedWord=%letter%%reversedWord%"
   rem Take *the rest* of characters
   set "word=%word:~1%"
if defined word goto nextLetter

echo Characters: %characters%, Reversed word: %reversedWord%
echo -------------------------
exit /B

Output example:

Code: Select all

Word: we
23 w
5 e
Characters: 2, Reversed word: ew
-------------------------
Word: were
23 w
5 e
18 r
5 e
Characters: 4, Reversed word: erew
-------------------------
Word: werein
23 w
5 e
18 r
5 e
9 i
14 n
Characters: 6, Reversed word: nierew

Antonio

One Batch
Posts: 8
Joined: 30 Aug 2015 04:58

Re: Error in setting variable in FOR loop & extracting text from a textfile

#8 Post by One Batch » 19 Oct 2017 09:14

The code you provided is clear. And works the way I wanted it.
The commentary to the code is helpful, and doesnt need extended explanation. Thank you.

Whenever I have future problems, and I make a topic to ask for help. Ofcourse I be sure not to post about this issue in a different topic.
Since this issue with the error in setting a variable is now clear and solved.
I will take better note of what I want. Since I wasnt to the point.

Post Reply