Computing.Net > Forums > Programming > Removing extra carriage return

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Removing extra carriage return

Reply to Message Icon

Name: needToCode
Date: May 29, 2009 at 06:16:17 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: May 29, 2009 at 17:48:16 Pacific
Reply:

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^>


0

Response Number 2
Name: Mechanix2Go
Date: May 29, 2009 at 18:23:01 Pacific
Reply:

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


0

Response Number 3
Name: Judago
Date: May 29, 2009 at 19:16:21 Pacific
Reply:

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


0

Response Number 4
Name: Mechanix2Go
Date: May 29, 2009 at 19:32:46 Pacific
Reply:

Pretty slick, Slick.


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

M2


0

Response Number 5
Name: needToCode
Date: May 29, 2009 at 20:59:06 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Judago
Date: May 29, 2009 at 21:21:12 Pacific
Reply:

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.....


0

Response Number 7
Name: needToCode
Date: May 30, 2009 at 06:05:48 Pacific
Reply:

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"?


0

Response Number 8
Name: Judago
Date: May 30, 2009 at 15:57:34 Pacific
Reply:

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


0

Response Number 9
Name: needToCode
Date: May 31, 2009 at 13:00:34 Pacific
Reply:

You rock....this worked. I have used the code examples to change several other format issues.

Thanks again


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Removing extra carriage return

remove carriage return at file end www.computing.net/answers/programming/remove-carriage-return-at-file-end/17101.html

remove duplicate blank lines in.txt www.computing.net/answers/programming/remove-duplicate-blank-lines-intxt/15294.html

Eval reg entry w/carriage returns www.computing.net/answers/programming/eval-reg-entry-wcarriage-returns/15573.html