Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Batch File not capturing the full line due to spaces

Hi guys hope you are well and can help.

I have a batch file that is trying to read a text file.
This text file has one column of user names, with each name being in format:

Firstname Lastname

So, a scenario might be:

script.bat users.txt

If is users.txt, we have:

--------------------------------------------------- users.txt
Simon Jones
Peter Tims
Sue Morgan

When I run the batch file, we get

Simon
Peter
Sue

So, what Im trying to do is to capture the entire line, but it seems that it only captures the firsname and stops at the space. Note that each name could have multiple spaces.
Im just not sure how to modify my batch file to cater for spaces.

Any help greatly appreciated.

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
rem for each line in textfile
for /f %%c in (%1) do (
  call :GETINFO %%c
)
goto :eof
 
:GETINFO
echo %1

Open in new window

Avatar of Bill Prew
Bill Prew

Try this:

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
rem for each line in textfile
for /f "tokens=*" %%c in (%1) do (
  call :GETINFO %%c
)
goto :eof
 
:GETINFO
echo %1
Avatar of Simon336697

ASKER

Hi bill, thanks so much, but this still does not echo the entire line, and stops at first space.
Modification of bill's script:

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
rem for each line in textfile
for /f "tokens=*" %%c in (%1) do (
  echo %%c
)
goto :eof
Or following the original using bill's script:

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
rem for each line in textfile
for /f "tokens=*" %%c in (%1) do (
call :GETINFO %%c
)
goto :eof

:GETINFO
echo %1 %2
Doh, silly me, try this:

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
rem for each line in textfile
for /f "tokens=*" %%c in (%1) do (
  call :GETINFO "%%c"
)
goto :eof
 
:GETINFO
echo %~1

~bp
pony10us!, how ya been...

~bp
pretty good.  Just had to jump in on one of your answers to say hey.   :)  How you been?

Speaking of which, what is the difference between:

echo %1 %2  and echo %~1

Is it just that the first will only return the first two "items" and the second will return the entire string regardless of count?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
==> pony10us

See my last post, let me know if that answers your last question.

I'm doing well, haven't been as active on EE the last few weeks, had some oral surgery, then a business trip, etc, so not as active here.  But still like to try and lend a hand when I see a question and there isn't already someone helping.

~bp
@billprew

Very good explanation. Once again I learned something. I didn't know about the quotes and ~ functions.   I miss our conversations about methods.  :)  
Hi bill that is so awesome what you have done here.
Bill,

If the file you were reading had the following:

======================================= file.txt
   Joe Bloggs
Simon Jones
   Doctor van Dough
Peter Stevens  <spaces here at the end>

and you wanted to return

Joe Bloggs
Simon Jones
Doctor van Dough
Peter Stevens (with no spaces at the end)

How could you return this by removing the starting blank spaces and trailing blank spaces?

If you want me to start another question, very happy to as you have been awesome.

Ill award you the points now in advance. Thanks so much again.
Bill your explanation is very very impressive. Thanks so much.
Give this a try, I think it should do what you are looking for:

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
rem for each line in textfile
for /f "tokens=*" %%c in (%1) do (
  call :GETINFO %%c
)
goto :eof
 
:GETINFO
echo %*
exit /b

~bp