Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Insert a carriage return linefeed in a variable with a set command

9,213 views
Skip to first unread message

Arno

unread,
Mar 1, 2012, 12:22:01 PM3/1/12
to
Hi all,

is it possible to append a CRLF to a variable value in the following
way:
REM Some assignment like this:
set CRLF=H'0D'H'0A'
for ....(
call %%a %*
if ERRORLEVEL NEQ 0 (
set ERROR_LIST=%ERROR_LIST% Process %%a ends with exitcode
'%ERRORLEVEL%'.%CRLF%
)
)
REM Do something else
:
:

echo %ERROR_LIST%

That I get the output similar to:
Process Prog1 ends with exitcode '11'.
Process Prog3 ends with exitcode '33'.
Process Prog5 ends with exitcode '55'.
Process Prog6 ends with exitcode '66'.
:
:

regards
Arno

Frank P. Westlake

unread,
Mar 1, 2012, 12:52:07 PM3/1/12
to
On 2012-03-01 09:22, Arno wrote:

> is it possible to append a CRLF to a variable value in the following
> way:

> set CRLF=H'0D'H'0A'

> set ERROR_LIST=%ERROR_LIST% Process %%a ends with exitcode
> '%ERRORLEVEL%'.%CRLF%

No.

You can sometimes append a linefeed:

::::::::::::::::::::::
(Set LF=^

)

Set var=text%LF%
::::::::::::::::::::::

But the linefeed will terminate input early with many operations.

Frank

jeb

unread,
Mar 1, 2012, 4:53:47 PM3/1/12
to
On 1 Mrz., 18:52, "Frank P. Westlake" <frank.westl...@gmail.net>
wrote:
Like Frank shows, it's easy to create a single linefeed character with
SET LF=^


rem ** Two empty lines are required here

And there are multiple ways to use it.
echo Line1^%LF%%LF%Line2

Or

set NLM=^^^%LF%%LF%^%LF%%LF%
echo Line1%NLM%Line2

Or the easiest one

setlocal EnableDelayedExpansion
echo Line1!LF!Line2

With EnableDelayedExpansion it's also works inside of quotes without
any problems

To create a CR character you could use the copy /z technic

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

To use the CR character you could only use %%Loop-Variables or
DelayedExpansion, I didn't found a way to use it with percents.
I assume CR's are removed directly after the percent expansion phase.

But this one works
echo You and me!CR!Tim

Creates the screen output "Tim and me" (the word "Two" will be
overridden

jeb

Todd Vargo

unread,
Mar 1, 2012, 5:29:48 PM3/1/12
to
This is an impractical idea. Once free environment space is exhausted,
any remaining errors will be truncated. Better methods are to either
display the errors as they occur or just log them in a file.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Arno

unread,
Mar 2, 2012, 3:35:43 AM3/2/12
to
thanks for all the helpful answers.
It works now! :-)

@Todd: I can't use a file, because I have no rights to create them in
this special environment. Is there a limit of the size of an
environment variable, because I expect a maximum of 500 lines. Perhaps
I have to reduce the lines content?

--
Arno

Brian Cryer

unread,
Mar 2, 2012, 4:55:23 AM3/2/12
to
"Arno" <arno.s...@sqs.de> wrote in message
news:8b8da9a2-3247-4a97...@k6g2000vbz.googlegroups.com...
Do you need it in a single environment variable? or are you simply after
output being structured how you've shown? If you are after how you've shown
the output then you'd be better off echoing each line as you go.

Otherwise, like Frank has already pointed out, no you can't use a carriage
return.

Other options:

1. Redesign as above - but you may have other reasons for wanting what you
are after.

2. Write to a file - as Todd has suggested.

3. Don't use echo but cryecho which will interpret \n as a newline and \r as
a carriage return. Its something I put together when I needed to echo some
output but without a carriage return at the end - its free, download from
http://www.cryer.co.uk/downloads/cryecho/ Of course if you are working in a
restricted environment (as you indicated to Todd) then you might not be able
to use a third party exe.

Hope this helps.
--
Brian Cryer
http://www.cryer.co.uk/brian

Frank P. Westlake

unread,
Mar 2, 2012, 9:22:42 AM3/2/12
to
On 2012-03-02 00:35, Arno wrote:
> Is there a limit of the size of an environment variable,
> because I expect a maximum of 500 lines.

The limit is system dependent. I think on my NT4 system it was 32k, then
on XP maybe it was 64k.

The lines in your sample aren't long but it might work more reliably if
you just collect the program names and error codes then add the text
when it's time to print.


@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion

Set errorlist="prog 1" 1 "prog 2" 0 "prog 3" 1

Set "prog="
For %%a in (%errorlist%) Do (
If NOT DEFINED prog (
Set "prog=%%~a"
) Else (
Echo Process !prog! ends with exitcode '%%a'.
Set "prog="
)
)

Frank

Arno

unread,
Mar 2, 2012, 10:41:50 AM3/2/12
to
thanks Frank, that's a good idea. I will take this proposal.

many thanks to all
--
Arno


Frank P. Westlake

unread,
Mar 2, 2012, 12:56:08 PM3/2/12
to
On 2012-03-01 13:53, jeb wrote:
> for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

That's very useful. Thanks jeb.

@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion

For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set /A n=0

::BUSY SPINNER
For /L %%i in (0,1,10) Do (
Set /A "n=%%i%%4"
For /L %%n in (!n! 1 !n!) Do Set /P "=Calculating !busy:~%%n,1!
!CR!"<NUL:
PING -n 2 127.0.0.1 >NUL:
)
::COUNTDOWN
For /L %%i in (10,-1,1) Do (
Set /P "=Backup will begin in %%i seconds. !CR!"<NUL:
PING -n 2 127.0.0.1 >NUL:
)
::PROGRESS
For %%i in (*) Do (
Set /P "=Copying %%i
!CR!"<NUL:
PING -n 2 127.0.0.1 >NUL:
)

Frank

Petr Laznovsky

unread,
Mar 2, 2012, 2:22:30 PM3/2/12
to
FYI:

On "Copying" part, few last characters of long filenames remaining on
the screen.

Win7-32bit

L.
0 new messages