Page 1 of 1

Help in regard to my post of "setting next line to variable"

Posted: 06 Nov 2011 01:15
by Rileyh
Hi,
This post is an addition to my previous post of how to set the next characters on a line as a variable".
I have gotten that to work, but how can I split the contents of that variable by the spaces in it. For example:
(in a text file)
hello world I am Rileyh

My batch file searches for "hello" and makes the rest a variable. I can do that.

MY PROBLEM:
I need to split "world I am Rileyh" (one variable) into individual variables by their spaces. So the batch file needs to recognize that there are spaces and use those spaces to determine what to put into the variables.

THE RESULT:
I should have "world" "I" "am" "Rileyh" in four differently labeled variables.

Thank you for your time,
Rileyh

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 01:35
by matt12582
Rileyh

I'm new to FOR loops but it seems like this might get the job done

Code: Select all

for /f " tokens=1-4," %%i in ('ping google.com') DO SET VAR1=%%i SET VAR2=%%j SET VAR3=%%k SET VAR4=%%l


gives me this


Code: Select all

C:\>for /f " tokens=1-4," %i in ('ping.exe google.com') DO SET VAR1=%i SET VAR2=
%j SET VAR3=%k SET VAR4=%l

C:\>SET VAR1=Pinging SET VAR2=google.com SET VAR3=[173.194.64.99] SET VAR4=with


C:\>SET VAR1=Reply SET VAR2=from SET VAR3=173.194.64.99: SET VAR4=bytes=32

C:\>SET VAR1=Reply SET VAR2=from SET VAR3=173.194.64.99: SET VAR4=bytes=32

C:\>SET VAR1=Reply SET VAR2=from SET VAR3=173.194.64.99: SET VAR4=bytes=32

C:\>SET VAR1=Reply SET VAR2=from SET VAR3=173.194.64.99: SET VAR4=bytes=32

C:\>SET VAR1=Ping SET VAR2=statistics SET VAR3=for SET VAR4=173.194.64.99:

C:\>SET VAR1=Packets: SET VAR2=Sent SET VAR3== SET VAR4=4,

C:\>SET VAR1=Approximate SET VAR2=round SET VAR3=trip SET VAR4=times

C:\>SET VAR1=Minimum SET VAR2== SET VAR3=54ms, SET VAR4=Maximum

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 01:59
by Rileyh
I need to make the "ping" section a variable, not a command.

Regards,
Rileyh

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 04:46
by Ed Dyreen
'
like:

Code: Select all

@echo off

set "$file=yourfile.txt"

for /f "usebackq tokens=1-26 delims= " %%a in ( "%$file%" ) do (
echo.set "$varA=%%~a"
echo.set "$varB=%%~b"
echo.set "$varC=%%~c"
echo.set "$varEtcetera=etc.."
)

pause

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 22:08
by Rileyh
Thanks Ed.
Could you show me how I would implement the code you gave me with this code:

Code: Select all

@echo off
cls
setlocal disableDelayedExpansion
set "file="test.txt"
set "search=hello "
for /f "delims=" %%A in ('findstr /i /c:"%search%" %file%') do (
  set "ln=%%A"
  at !ln:*%search%=! /interactive
  setlocal enableextensions
  break
  endlocal


Can you understand this code and how it works?
If yes, could you show me how to split the !ln:*%search%=! into separate variables by spaces?

Thanks,
Rileyh

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 23:18
by Ed Dyreen
'
like this:
enable usebackq if file has special chars !

Code: Select all

@echo off &setlocal EnableDelayedExpansion

set     "$file="test.txt"
set "$match=hello"
set    "$cmd="
for /f "tokens=*" %%! in (
   "!$file!"
) do if not defined $cmd (
   set "$t=%%~!"
   set "$compare=!$t:*%$match%=!"
   if /i ["!$compare!"] neq ["!$t!"] (
      set "cmd=at !$compare! /interactive"
   )
)
echo.$cmd=!$cmd!_

pause

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 23:21
by Rileyh
Sorry about my poor code- I copied it directly out of a completely BUGGY test batch.
Here is the more relevant code:

Code: Select all

@echo off
setlocal disableDelayedExpansion
set file="test.txt"
set "search=print "
for /f "delims=" %%A in ('findstr /i /c:"%search%" %file%') do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  echo(!ln:*%search%=!
  endlocal

NOTE- I was trying to make a "print" command and it "print hello world" is in a text document "test.txt"

This may be more helpful.

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 23:27
by Ed Dyreen
'
My previous solution should work.

-Why disable delayed if you enable it later anyways ?
-findstr is slow, you should not need it.
-you don't need delims, instead use if.

Re: Help in regard to my post of "setting next line to varia

Posted: 06 Nov 2011 23:50
by Rileyh
It didn't work sorry.
I am not sure why.
I am running windows 7 if it helps.