Save what's between "," and what's before "=" to variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Save what's between "," and what's before "=" to variables

#1 Post by Ranguna173 » 01 May 2013 10:56

Hello everyone.

I've searched through the web but I didn't manage to find anything so here goes.

I need two pieces of code, one that saves what's between "," Ex:.
I have a text file/variable with the following contents:

Code: Select all

ab,abb,ba,asf,ade,te

I would like the code to capture the ab,abb,ba.. to different variable like:

Code: Select all

word#1=ab
word#2=abb
word#2=ba
...

Also notice the the first word "ab" doesn't have a comman behind it and that the last word "te" doesn't have a comma in front of it. I can add the commas if they become to much of a trouble.


That was for the first code for the second one I was searching how to get the contents of what's behind a "="
I already have one that reads what's after the "=":

Code: Select all

for /f "tokens=2 delims==" %%a in ('FINDSTR /C:"'a'" db.txt') do echo %%a

(By foxidrive)

But now I want to do it in reverse, Ex:.
Text file:

Code: Select all

'a'=b
'b'=c
'c'=d

I would like a code to find b and provide 'a'. One at a time.

Thanks for reading and if you know the answer please help. :)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Save what's between "," and what's before "=" to variabl

#2 Post by Endoro » 01 May 2013 15:02

code 1

Code: Select all

@echo off & setlocal
set /a counter=1
set "line="
for /f "delims=" %%i in (myfile.txt) do set "line=%%i"&call:process
set "word#"
goto:eof

:process
if not defined line goto:eof
for /f "tokens=1*delims=," %%a in ("%line%") do set "left=%%a"&set "line=%%b"
set "word#%counter%=%left%"
set /a counter+=1
goto:process


code 2

Code: Select all

for /f "delims==" %%a in ('FINDSTR /C:"=b" db.txt') do echo %%a

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

Re: Save what's between "," and what's before "=" to variabl

#3 Post by Ranguna173 » 01 May 2013 16:42

Endoro wrote:code 1

Code: Select all

@echo off & setlocal
set /a counter=1
set "line="
for /f "delims=" %%i in (myfile.txt) do set "line=%%i"&call:process
set "word#"
goto:eof

:process
if not defined line goto:eof
for /f "tokens=1*delims=," %%a in ("%line%") do set "left=%%a"&set "line=%%b"
set "word#%counter%=%left%"
set /a counter+=1
goto:process


code 2

Code: Select all

for /f "delims==" %%a in ('FINDSTR /C:"=b" db.txt') do echo %%a


They are both perfect man, thanks :D
I don't undestand why there isn't an "expert" tag under your name yet, you've helped a lot with realy good answers.

(and as usual on my post here) Thanks again for all your work. :)

Post Reply