remove carriage return in dos command
Moderator: DosItHelp
remove carriage return in dos command
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
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
Hi lalitpct,
your question is a bit unclear.
I suppose you test with
And you got
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.
jeb
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
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%
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
You should put it into a batch file.
Or you have to use single percent signs on the cmd line
jeb
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
It is possible to do what you want with a single line of code without using a variable
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
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
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
Hi Dave
If SET /P command is used this way, the entire variable name may be ommited:
I think the variable name is "echo ."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
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
Aacini wrote:Hi DaveI think the variable name is "echo ."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
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
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