简体   繁体   English

如何连接Windows批处理文件的命令输出

[英]How to concatenate command outputs of a Windows batch file

I'm writing a Windows batch file that will purge logs older than 90 days. 我正在编写一个Windows批处理文件,它将清除超过90天的日志。 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. 谢谢。

Putting everything on one line except for the FOR output is easy. 将所有内容放在除FOR输出之外的一行上很容易。 You just need to use the dynamic %TIME% and %DATE% variables instead of the TIME and DATE commands 您只需使用动态%TIME%和%DATE%变量而不是TIME和DATE命令

@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. 如果您还希望文件名出现在同一行,那么您可以使用EitanT建议的临时变量。 But that limits the number of files to what can fit in the max 8191 variable size. 但是这会将文件数限制为最大8191变量大小。 To process an unlimited number of files you can use SET /P instead. 要处理无限数量的文件,您可以使用SET / P. 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. 似乎FOR / F声明似乎不是必要的,但有一个引用问题,没有它我无法解决。

@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. 由于FORFILES非常慢,因此在同一命令中清除和列出效率会更高。

@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 更新2015-01-06

I figured out a solution without using FOR /F. 我想出了一个没有使用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. 我使用0x22将SET / P提示符括在引号中,并使用FINDSTR消除FORFILES在任何请求的输出之前写入的空行。

@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 .
)

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: 如果要连接输出中的行,可以将CMD_STR设置为所需的命令,并使用for /f循环,如下所示:

@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 . 循环遍历输出行,并将它们逐个附加到CONCAT_STR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM