Words from a phrase into Variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Words from a phrase into Variables

#1 Post by Ranguna173 » 28 Aug 2011 08:55

Hello Everyone!! :D :D
Long time no see!

I have another question..

Is it possible to dived a phrase and save the words into variables?
Like this:

The Phrase

Code: Select all

Hello everyone!


Now i would like a code to separate those two word and save them into two different variables:

Code: Select all

 "Hello / everyone!"
 set a=Hello ::the 1st word.
 set b=everyone! ::the 2nd word.
 cls
 echo %a% %b%
 pause
 

And You Would see

Code: Select all

Hello everyone!


So.. That's all!
If you want more information just ask! :D
And please reply if you know something.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Words from a phrase into Variables

#2 Post by !k » 28 Aug 2011 10:41

Code: Select all

@echo off &setlocal enableextensions

set "phrase=Hello everyone! Hello! Helllooo!!1"

set /a word=0
call :loop "%phrase%"
set word
goto :eof

:loop
for /f "tokens=1,*" %%a in ("%~1") do (
  set "word#%word%=%%a"
  set /a word+=1
  call :loop "%%b"
)
goto :eof

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Words from a phrase into Variables

#3 Post by Ranguna173 » 28 Aug 2011 15:26

!k wrote:

Code: Select all

@echo off &setlocal enableextensions

set "phrase=Hello everyone! Hello! Helllooo!!1"

set /a word=0
call :loop "%phrase%"
set word
goto :eof

:loop
for /f "tokens=1,*" %%a in (%1) do (
  set "word#%word%=%%a"
  set /a word+=1
  call :loop "%%b"
)
goto :eof


Everyone uses "for"... :cry:

Anyway, where are the a and the b variables?

I want to edit the words..

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Words from a phrase into Variables

#4 Post by aGerman » 28 Aug 2011 15:47

Ranguna173 wrote:Everyone uses "for"... :cry:

What else? It's the best way I can think.

Ranguna173 wrote:Anyway, where are the a and the b variables?

Why a and b? If "phrase" has mor than 26 words you can't handle it. !k used %word#0%, %word#1%, ... instead.
In %word% you can find the number of words.

If your "phrase" has always 2 words you could use

Code: Select all

for /f "tokens=1,2" %%a in ("%phrase%") do (set "a=%%a" &set "b=%%b")


Regards
aGerman

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Words from a phrase into Variables

#5 Post by Ranguna173 » 28 Aug 2011 16:40

aGerman wrote:Why a and b?


It was a metaphor.. When i asked you where the a and b were I was referring to the variables in general..

aGerman wrote:!k used %word#0%, %word#1%, ... instead.


Okay.. I tried to use them but..

Code: Select all

 C:\>test.cmd
 word=4
 word#0=Hello
 word#1=everyone!
 word#2=Hello!
 word#3=Helllooo!!1
 C:\>echo %word#1%
 %word#1%
 C:\>echo %word%
 %word%
 


Am I doing something wrong?



I'm gonna use this for a encryption batch.. :D

I write a notepad with the 1st line has:
Hello

Then i select the notepad with the batch and it encrypts it..
Output:
1001c00 0001101 0110100 0110100 100001b

Like that..

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Words from a phrase into Variables

#6 Post by aGerman » 28 Aug 2011 17:24

It's because you didn't process the variables in the batch file (insert your ECHO commands into the batch code and it would work).

Delete " &setlocal enableextensions" to preserve the variables for the calling command prompt. I'm virtually certain you're working with "enabled extensions" by default (it's a registry setting).

Regards
aGerman

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Words from a phrase into Variables

#7 Post by Ranguna173 » 29 Aug 2011 06:51

aGerman wrote:Delete " &setlocal enableextensions" to preserve the variables for the calling command prompt.


Yeah.. I had to delete the "&setlocal enableextensions"..

Ok, all done!

But one last thing: :P
How do I split the characters in the word?

Sorry for being a pain in the ass, but the only thing i find in google is this...

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Words from a phrase into Variables

#8 Post by !k » 29 Aug 2011 09:23

Code: Select all

@echo off &setlocal enableextensions

set "word#1=everyone!"

call :ch "%word#1%"
set char
goto :eof

:ch
for /l %%c in (0,1,127) do (
  set "w=%~1"
  call set "char#%%c=%%w:~%%c,1%%"
)
goto :eof


By words

Code: Select all

@echo off &setlocal enableextensions

set "phrase=Hello everyone! Hello! Helllooo!!1"

set /a word=0
call :loop "%phrase%"
set word
pause
set /a words=word-1
for /l %%a in (0,1,%words%) do (
  call :ch "%%word#%%a%%"
  set char
  pause
)
goto :eof

:loop
for /f "tokens=1,*" %%a in (%1) do (
  set "word#%word%=%%a"
  set /a word+=1
  call :loop "%%b"
)
goto :eof

:ch
for /l %%c in (0,1,127) do (
  set "w=%~1"
  call set "char#%%c=%%w:~%%c,1%%"
)
goto :eof


All in Together

Code: Select all

@echo off &setlocal enableextensions

set "phrase=Hello everyone! Hello! Helllooo!!1"

set /a word=0
call :loop "%phrase%"
set word
pause
set /a words=word-1
for /l %%a in (0,1,%words%) do (
  call :ch "%%word#%%a%%" word#%%a
)
set word
pause
goto :eof

:loop
for /f "tokens=1,*" %%a in (%1) do (
  set "word#%word%=%%a"
  set /a word+=1
  call :loop "%%b"
)
goto :eof

:ch
for /l %%c in (0,1,127) do (
  set "w=%~1"
  call set "%2_char#%%c=%%w:~%%c,1%%"
)
goto :eof

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Words from a phrase into Variables

#9 Post by Ranguna173 » 29 Aug 2011 16:58

!k wrote:

Code: Select all

...


By words

Code: Select all

...


All in Together

Code: Select all

...

(the post was big so i replace the codes with "...")

Thank You !k !!

ALL Done!!

Now the only thing left to do is writing the encryption code and applying your code to mine....
Hell.. This is gonna take forever... :(

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Words from a phrase into Variables

#10 Post by Ranguna173 » 29 Aug 2011 17:03

Thank You aGerman and !k
You realy helped!!

People that have this question too can find the answerer on !k post.

Thank You!!

Post Reply