Removing extra carriage return

May 29, 2009 at 06:16:17
Specs: Windows XP
I have been redesigning several old batch files to redirect their output to HTML.

Most of the redirects work fine, but of some not so fine.

Sometime the output adds an extra carriage return between lines, which is fine on some occasions.

The following code snippet is one that produces the extra line(s). I want the extra line after the title, but not after each individual line.

How do I prevent these extra lines?

for /f "delims=" %%a in ('ipconfig /all') do (
echo ^<pre^>^<span style="font-size: 12pt"^>%%a ^</span^>^</pre^> ) >> test.html


See More: Removing extra carriage return

Report •


#1
May 29, 2009 at 17:48:16
Double carriage returns strike again!

for /f "delims=" %%a in ('ipconfig /all') do (
	set line=%%a
	for /f "tokens=1* delims=]" %%b in ('call echo."%%line:~0,-1%%"^|find /n /v ""') do >>test.htm echo.^<pre^>^<span style="font-size: 12pt"^>%%~c ^</span^>^</pre^>
)

If you are using delayed expansion you can exchange this:

for /f "tokens=1* delims=]" %%b in ('call echo."%%line:~0,-1%%"^|find /n /v ""') do >>test.htm echo.^<pre^>^<span style="font-size: 12pt"^>%%~c ^</span^>^</pre^>

With this:

>>test.htm echo.^<pre^>^<span style="font-size: 12pt"^>!line:~0,-1! ^</span^>^</pre^>


Report •

#2
May 29, 2009 at 18:23:01
Hi Judago,

I took another approach. The (ia) is just a file created by ipconfig.

============================
@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (ia) do (
call :sub1 %%a
echo.!str! >> newfile
)
goto :eof

:sub1
set str=%*
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
May 29, 2009 at 19:16:21
Good one M2, didn't think of that, it could be simplified further(special characters allowing):

for /f "delims=" %%a in ('ipconfig /all') do (
call :sub1 %%a
)
goto :eof

:sub1
>> newfile echo.%*
goto :eof


Report •

Related Solutions

#4
May 29, 2009 at 19:32:46
Pretty slick, Slick.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
May 29, 2009 at 20:59:06
Thanks! Both code examples are much cleaner. I tested the last posted code and noticed that it introduced a new problem. It removes the blank line separator between the adapter type and the first entry. Is it possible to prevent that removal?

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "delims=" %%a in ('ipconfig /all') do (
call :sub1 %%a
)
goto :eof

:sub1
echo.^<pre^>^<span style="font-size: 12pt"^>%*^</span^>^</pre^>
goto :eof


Report •

#6
May 29, 2009 at 21:21:12
Perhaps:

@ECHO OFF
echo ^<pre^>^<span style="font-size: 12pt"^>
for /f "delims=" %%a in ('ipconfig /all') do (
call :sub1 %%a
)
echo ^</span^>^</pre^>
goto :eof

:sub1
echo.%*
goto :eof

It should also save on bytes.....


Report •

#7
May 30, 2009 at 06:05:48
Thanks Judago!!

This is a much cleaner solution.

I noticed that this newer code removes all preformatted tabs. For example, If you have multiple DNS servers configured the tab used to align the server's IPs is removed on the second, third, etc. I also tested this on WINs, Routes, etc. and it produces the same issue.

Is there a way to maintain the preformatted "tabs"?


Report •

#8
May 30, 2009 at 15:57:34
I thought I tested that, but your right.....

echo ^<pre^>^<span style="font-size: 12pt"^>
for /f "delims=" %%a in ('ipconfig /all') do (
call :sub1 "%%a"
)
echo ^</span^>^</pre^>
goto :eof

:sub1
echo.%~1
goto :eof


Report •

#9
May 31, 2009 at 13:00:34
You rock....this worked. I have used the code examples to change several other format issues.

Thanks again


Report •


Ask Question