Page 1 of 1

remove carriage return in dos command

Posted: 16 Jan 2012 04:33
by lalitpct
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

Re: remove carriage return in dos command

Posted: 16 Jan 2012 05:10
by jeb
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

Re: remove carriage return in dos command

Posted: 16 Jan 2012 05:49
by lalitpct
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%

Re: remove carriage return in dos command

Posted: 16 Jan 2012 06:05
by jeb
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

Re: remove carriage return in dos command

Posted: 16 Jan 2012 06:37
by dbenham
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

Re: remove carriage return in dos command

Posted: 16 Jan 2012 07:08
by Squashman
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.

Re: remove carriage return in dos command

Posted: 16 Jan 2012 09:30
by Aacini
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

Re: remove carriage return in dos command

Posted: 16 Jan 2012 10:16
by dbenham
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