0

I'm having the below script to display some data on-screen, using one small CMD file:

@echo off

set description=%1

ver
date /t
time /t
echo %description%

This provides output, similar to:

Microsoft Windows [Version 6.1.7601]
ma 08/12/2014
17:00
sometext

But, I would like to have this output:

Microsoft Windows [Version 6.1.7601] [ma 08/12/2014 17:00] sometext

I can't think of easy coding (without including variables and/or temporary files) to have something like that achieved. Is it possible in an easy manner (like using backticks on Linux/Unix) ? How ?

As a bonus, it would be even more better if that one line does not end with a return character. That way, I can add additional text (also in 1 line format) to the file, just using ECHO. Knowing that ECHO itself will add a return eventually. I know I can do this, using Cygwin's PRINTF, but if possible I would like to avoid Cygwin, and use standard CMD.

EDIT: changed the question to include the "ver" command so that it matches the title : output of real commands, to be converted into 1 line

tvCa
  • 796
  • 6
  • 13
  • Windows cmd: `echo [%date% %time%] %description%` or something like `echo [%date% %time:~0,5%] %description%` – JosefZ Dec 08 '14 at 16:14
  • Indeed, but I really wanted to know how to convert output, not by chance having it in variables already. Which is good on itself, but it was a bad example by me. I've added something to make it harder. – tvCa Dec 08 '14 at 16:31
  • Bad or good example, I don't care: `%date%` and `%time%` are _built-in_ variables... And, as you stated, `ECHO` itself will add a return eventually... Sorry, I know no workaround for it. – JosefZ Dec 08 '14 at 16:57
  • Below is the trick I've learned today; This will display all on same line : @echo off set /p=This is line 1 < nul set /p=While this is line 2 < nul – tvCa Dec 13 '14 at 20:21

2 Answers2

1
@echo off
set myVar=
set description=%1
for /F "delims=" %%I in ('ver') do (set myVar=%%I)
for /F "delims=" %%I in ('date /t') do (set myVar=%myVar% %%I)
for /F "delims=" %%I in ('time /t') do (set myVar=[%myVar% %%I])
set myVar=%myVar% %description%
echo %myVar%

How do I get the result of a command in a variable in windows?

Community
  • 1
  • 1
houssam
  • 1,823
  • 15
  • 27
  • I'm sure it works, and you'll get my upvote if it appears to be the most elegant solution, but I was looking for something shorter and/or simplier. Still, technically OK, I'm sure. – tvCa Dec 08 '14 at 16:36
1

No temporary file, no visible auxiliary variable (cf. no change to %xyz% variable formally used in the :echo subroutine). Simple to change echo anything to call :echo anything

  @ECHO OFF >NUL
  :: unrem next line to see no change
  :: set "xyz=XYZ"

  Call :echo %time%

  Call :echo after 5 secs
  ping 1.1.1.1 -n 1 -w 5000 > nul
  Call :echo %time%

  Call :echo another 5 secs
  ping 1.1.1.1 -n 1 -w 5000 > nul
  Call :echo %time%

  :: add new line in next ECHO. command 
  echo.

  :: ensure variable xyz is not defined / changed
  set xyz
  :: and STDOUT command output
  for /F "tokens=* delims=" %%G in ('ver /?') do call :echo %%G
  for /F "tokens=* delims=" %%G in ('ver') do call :echo %%G
  goto :eof

:echo
  <nul (set/p xyz=%* )
goto :eof

And to see your task solution:

  @ECHO OFF >NUL
  for /F "tokens=* delims=" %%G in ('ver') do call :echo %%G
  for /F "tokens=* delims=" %%G in ('date /t') do call :echo %%G
  for /F "tokens=* delims=" %%G in ('time /t') do call :echo %%G
  call :echo sometext
  call :echo
  goto :eof

:echo
  if "%*"=="" echo.&goto :eof
  <nul (set/p xyz=%* ) 2>NUL
goto :eof

In last code snippet adhered to rule only one command for the same task: echo. replaced by call :echo to add new line to output. However, let echo ON and echo OFF apart...

JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • That's pretty smart. It took some time, but I now understand that `set -p` does not display any line-feed, because it is waiting for user input to provide exactly that. And if forcing `nul` to be reversly redirected, a line-feed is avoided. – tvCa Dec 13 '14 at 20:13