0

For example, following commands (in Windows 7):

date/t>>t.txt
time/t>>t.txt

create following lines in t.txt:

Fri 06/12/2015 
01:37 PM

Is it possible to join output of two commands (see above) in one line (see below)?

Fri 06/12/2015 01:37 PM

The above mentioned goal can be reached by command:

echo %date% %time%

But it is not an answer on this question, because this question is not about only above commands.

valeryan
  • 344
  • 1
  • 5
  • 15

3 Answers3

1

For any number of commands, in a simpler way:

@echo off
setlocal EnableDelayedExpansion

set "output="
for %%a in ("date /t" "time /t" "echo Hello world") do (
   for /F "delims=" %%b in ('%%~a') do set "output=!output! %%b"
)
echo %output:~1%>> t.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Sorry @Aachi, but if for /F "delims=" %%b in ('%%~a') do set "output=!output! %%b" then there is extra space after 2015: "Mon 06/15/2015 08:41 AM" and if for /F "delims=" %%b in ('%%~a') do set "output=!output!%%b" then there is missed letter: "on 06/15/2015 08:42 AM" – valeryan Jun 15 '15 at 05:45
  • I asummed you want each output separated by space as in your `echo %date% %time%` example! The first additional space is eliminated in the ":~1" part of `echo %output:~1%>> t.txt`. If you want not separation spaces, also change the last line by `echo %output%>> t.txt` – Aacini Jun 15 '15 at 15:48
  • @Aachi: Yes, one space is needed. However, if You will see source of this page, then after "2015" You will see not one, but two spaces (whose changed to one spaces by browser). – valeryan Jun 16 '15 at 05:42
  • I meant after "2015" in previous comment. – valeryan Jun 16 '15 at 05:52
  • But, anyway, thanks! Because with `"output=!output!%%b"` and `echo %output:~0%>> t.txt` Your code works as needed too. – valeryan Jun 16 '15 at 05:54
  • yep, but with `~0` it is more demonstrative :) – valeryan Jun 16 '15 at 11:18
0

The output of two commands can be joined in one line by using following commands (in batch file):

for /f "delims=" %%x in ('date/t') do set d=%%x
for /f "delims=" %%x in ('time/t') do set t=%%x
echo %d%%t%>>t.txt
valeryan
  • 344
  • 1
  • 5
  • 15
  • Thanks to Joey (joey) and Frank Bollack (frank-bollack)! ( http://stackoverflow.com/questions/1425807/redirecting-output-to-a-command-line-argument-of-another-command-in-windows ) – valeryan Jun 12 '15 at 14:19
0

The output of two commands can be joined in one line also by using following commands (in batch file):

date /t>t_t.txt
set /p t=<t_t.txt
time /t>t_d.txt
set /p d=<t_d.txt
echo %t%%d%>>t.txt
valeryan
  • 344
  • 1
  • 5
  • 15
  • Thanks to Stefanos Kalantzis (stefanos-kalantzis)! http://stackoverflow.com/questions/4568941/reading-the-content-of-a-text-file-into-an-environment-variable – valeryan Jun 16 '15 at 13:23