remove carriage return in dos command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lalitpct
Posts: 3
Joined: 16 Jan 2012 04:27

remove carriage return in dos command

#1 Post by lalitpct » 16 Jan 2012 04:33

Is it possible to have below outputs in one single line and not in two lines , basically carriage retun takes it to next line?
If possible I want one line command to remove the carraige return
echo %username; OS version is : &VER

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: remove carriage return in dos command

#2 Post by jeb » 16 Jan 2012 05:10

Hi lalitpct,

your question is a bit unclear.

I suppose you test with

Code: Select all

echo %username% OS version is : &VER


And you got
Output wrote:Bill G.

Microsoft Windows [Version 6.1.7601]


You can't remove the CR/LF but you can avoid it.
First you need to get the output of VER into a variable.
Then you can echo both on one single line.

Code: Select all

for /F "delims=" %%a in ('ver') do set version=%%a
echo %username%  OS is %version%


jeb

lalitpct
Posts: 3
Joined: 16 Jan 2012 04:27

Re: remove carriage return in dos command

#3 Post by lalitpct » 16 Jan 2012 05:49

seems the code doesnt work it gives error


C:\Users\Ld78977>for /F "delims=" %%a in ('ver') do set version=%%a
%%a was unexpected at this time.

C:\Users\Ld78977>echo %username% OS is %version%
ld78977 OS is %version%

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: remove carriage return in dos command

#4 Post by jeb » 16 Jan 2012 06:05

You should put it into a batch file.

Or you have to use single percent signs on the cmd line

Code: Select all

for /F "delims=" %a in ('ver') do set version=%a


jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: remove carriage return in dos command

#5 Post by dbenham » 16 Jan 2012 06:37

It is possible to do what you want with a single line of code without using a variable

Code: Select all

<nul set /p echo .=User is %username%; OS version is & for /f "delims=" %a in ('ver') do @echo %a

There are two commands above, separated by the command separator operator "&". It allows multiple commands on a single line.

1) The first command is a trick to print information to the screen without a <carriage return><line feed> (<CR><LF>). SET /P prompts the user for input and stores the input in a variable. The variable is "." in this case, and the prompt is everything after the "=". But since input is redirected to the NUL device, SET /P immediately terminates without changing the value of any variable.

2) The VER command prints a blank line, followed by the OS version info on a 2nd line. But the FOR /F command ignores blank lines. That is why it only prints the non-blank line.

Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: remove carriage return in dos command

#6 Post by Squashman » 16 Jan 2012 07:08

lalitpct wrote:seems the code doesnt work it gives error


C:\Users\Ld78977>for /F "delims=" %%a in ('ver') do set version=%%a
%%a was unexpected at this time.

C:\Users\Ld78977>echo %username% OS is %version%
ld78977 OS is %version%

As Jeb pointed out to you already I thought I would show you the relevant help file that tells you how that works. See the last sentence.

Code: Select all

H:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: remove carriage return in dos command

#7 Post by Aacini » 16 Jan 2012 09:30

Hi Dave
dbenham wrote:

Code: Select all

<nul set /p echo .=User is %username%; OS version is & for /f "delims=" %a in ('ver') do @echo %a

... The variable is "." in this case, and the prompt is everything after the "=".

Dave Benham
I think the variable name is "echo ."

If SET /P command is used this way, the entire variable name may be ommited:

Code: Select all

<nul set /p =User is %username%; OS version is & for /f "delims=" %a in ('ver') do @echo %a

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: remove carriage return in dos command

#8 Post by dbenham » 16 Jan 2012 10:16

Aacini wrote:Hi Dave
dbenham wrote:

Code: Select all

<nul set /p echo .=User is %username%; OS version is & for /f "delims=" %a in ('ver') do @echo %a

... The variable is "." in this case, and the prompt is everything after the "=".

Dave Benham
I think the variable name is "echo ."

If SET /P command is used this way, the entire variable name may be ommited:

Code: Select all

<nul set /p =User is %username%; OS version is & for /f "delims=" %a in ('ver') do @echo %a

:lol: You would be correct - I did not intend for ECHO to be there - it was a (harmless) cut and paste error.

I did not know the variable name was optional. Thanks

Dave Benham

Post Reply