Page 1 of 1

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

Posted: 01 May 2013 10:56
by Ranguna173
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. :)

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

Posted: 01 May 2013 15:02
by Endoro
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

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

Posted: 01 May 2013 16:42
by Ranguna173
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. :)