Ignore blank lines and set lines to variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Tigers!
Posts: 6
Joined: 01 Jul 2008 21:57
Location: Australia

Ignore blank lines and set lines to variables

#1 Post by Tigers! » 23 Jul 2008 18:40

I have some issues at present with opening text files and then manipulating them in DOS (it would be so easy in UNIX or Perl. Curse my employer for being a Windows shop!)
The code below shows the output of my ping that I wish to finally store in a file but first I want to tidy it up a little.

Code: Select all

Pinging sew36server.jp.eu.sew [10.16.4.235] with 32 bytes of data:


    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 184ms, Maximum = 230ms, Average = 206ms

I would like to remove the blank lines and the leading spaces.
Two issues
1. Ignore blank lines
2. set a non-blank line to a variable (string or otherwise) then manipulate it.
Taking issue 1 first
The body of the for loop is then called with the variable value(s) set to the found token string(s). By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped.

The quote above (from a web site) implies that a for loop will ignore blank line. I have tried

Code: Select all

for /f "eol=" %%a in (asiaPacificPing.txt) do echo %%a 

and

Code: Select all

for /f "eol= " %%a in (asiaPacificPing.txt) do echo %%a 

and

Code: Select all

for /f  %%a in (asiaPacificPing.txt) do echo %%a 

without success. How does one skip blank lines in a file using for (or any other method)?
Taking issue 2 now
How does one assign a line to a variable, say str?
I have tried

Code: Select all

for /f %%a in (asiaPacificPing.txt) do set str=%%a 

but that does not seem to work.
Please help before I kill my windows machine./

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#2 Post by greenfinch » 23 Jul 2008 20:50

Hi Tigers,

Had a mess around with this - ignored the EOL for blanks lines because it seems it wasn't needed.

The important bit is "delims=" which makes each line a token. The default delims are space and tab which is no use here - see http://www.computerhope.com/forhlp.htm

The cleanup subroutine uses "delims= " (space) and tokens=* (* means the rest of the line) - this strips leading spaces

See http://www.dostips.com/DtTipsStringMani ... rimLeftFOR

Code: Select all

setlocal enabledelayedexpansion
@echo off
set /a count=1
for /f "delims=" %%a in (asiaping.txt) do (call :cleanup "%%a")
GOTO:end

:cleanup
set var%count%=%1
set thisvar=!var%count%!
for /f "tokens=* delims= " %%a in (%thisvar%) do (set thisvar=%%a)
set var%count%=%thisvar%
SET /A count+=1
GOTO:EOF

:end
echo %var1%
echo %var2%
echo %var3%
echo %var4%


Then you can process the vars as you wish

I assume http://www.activestate.com/Products/act ... ndex.mhtml is not an option?

Post Reply