How to concatenate command outputs of a Windows batch file

22,302

Solution 1

If you want to concatenate the lines in the output, you can set CMD_STR to be your desired command, and use a for /f loop, like so:

@echo off
setlocal enabledelayedexpansion
set "CMD_STR=time /t && echo "--" && date /t && echo " -I- Purging " && FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c echo @file @fdate""
set CONCAT_STR=
for /f %%i in ('%CMD_STR%') do set "CONCAT_STR=!CONCAT_STR! %%i"
echo !CONCAT_STR!

The loop iterates through the lines of the output and appends them one by one to CONCAT_STR.

Solution 2

Putting everything on one line except for the FOR output is easy. You just need to use the dynamic %TIME% and %DATE% variables instead of the TIME and DATE commands

@echo off    
echo %time% "--" %date% -I- Purging 
FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c echo @file @fdate"
rem FORFILES that will purge the files

If you also want the file names to appear on the same line, then you can use a temp variable as EitanT suggested. But that limits the number of files to what can fit in the max 8191 variable size. To process an unlimited number of files you can use SET /P instead. It doesn't seem like the FOR /F statement should be necessary, but there is a quoting issue that I couldn't solve without it.

@echo off
<nul (
  set/p="%time% -- %date% -I- Purging "
  for /f "delims=" %%A in (
    'FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c echo @file @fdate"'
  ) do set/p="%%A "
)
rem FORFILES that will purge the files

There is no reason not to purge the files at the same time that you are listing them. Since FORFILES is so slow, it would be much more efficient to purge and list in the same command.

@echo off
<nul (
  set/p="%time% -- %date% -I- Purging "
  for /f "delims=" %%A in (
    'FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c del @path&echo @file @fdate"'
  ) do set/p="%%A "
)


Update 2015-01-06

I figured out a solution without using FOR /F. I use 0x22 to enclose the SET /P prompt in quotes, and I use FINDSTR to eliminate the empty line that FORFILES writes before any requested output.

@echo off
<nul (
  set/p="%time% -- %date% -I- Purging "
  forfiles /p "d:\logs" /m *.log /d -90 /c "cmd /c del @path&set/p=0x5e0x22@file @fdate 0x5e0x22"'|findstr .
)
Share:
22,302
TomaszRykala
Author by

TomaszRykala

Aspiring Android Craftsman by day, Dark Disco producer by night. They give out Gold Badges to anyone these days.

Updated on January 07, 2020

Comments

  • TomaszRykala
    TomaszRykala over 4 years

    I'm writing a Windows batch file that will purge logs older than 90 days. How can I concatenate the outputs of the commands so that they appear in one line? I also want to append this output to a file later. My batch file so far is:

    @echo off    
    time /t && echo "--" && date /t && echo " -I- Purging " && FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c echo @file @fdate"
    rem FORFILES that will purge the files
    

    and this outputs:

    12:08
    --
    14/08/2012
     -I- Purging
    
    "<filename>" 02/08/2012
    "<filename>" 30/07/2012
    

    How can I concatenate these outputs? Thanks.

  • TomaszRykala
    TomaszRykala over 11 years
    Thank you, but I needed to replace %CMD_STR% with %%CMD_STR%% and add another 'echo' before Purging to make it work.
  • Eitan T
    Eitan T over 11 years
    @TomaszRykala Glad to help. Why the %%?
  • TomaszRykala
    TomaszRykala over 11 years
    cmdline barfed "&& was unexpected at this time." without it. apparently all variables in batch files have to have %%, and this is the only one in your script that didn't.
  • Eitan T
    Eitan T over 11 years
    No, variables should have only one %. The behavior you get is probably due to another reason: the batch interpreter has trouble dealing with && and so you need to prevent CMD_STR from unrolling by escaping the % (with %%). But... as long as it works :-)
  • Wolf
    Wolf over 8 years
    my favorite solution for getting timestamps into ad-hoc batches
  • jeb
    jeb over 8 years
    Nice, but you missed the caret 0x5E at set/p=0x5E0x22.. to avoid problems with & in file names