Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot echo in the same line in Batch script

Tags:

batch-file

I have txt files that contain several lines and I need to create a log out of them to store in a log the following information:
File Name
Last modified
Count of lines containing the word "valid"

I've put together a .bat file but it splits the output in two lines.

type nul >  FilesReceived.txt & for %f in (*.log) do (
find /c "valid" %f & echo(%~tf)>> LogsReceived.txt
)

With type nul I clear the contents of the FilesReceived.txt file. Then I loop through the files of type log.
Then I count lines that contain the word valid with find /c and I also echo the last modified time stamp.

However the output looks like:

---------- transaction_20160505_1005A.log: 6492
10/06/2016 04:37 p.m.

I don't know what's generating those dashes. Ultimately I'd like to have one line per log file as follows:

transaction_20012B.log: 6492 10/06/2016 04:37 p.m.

Hope you guys can help me.

Thanks,

Bruce

like image 600
Bruce Avatar asked Jun 11 '16 00:06

Bruce


People also ask

What is @echo off in batch script?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

What is %% A in batch script?

Required. Represents a replaceable parameter. Use a single percent sign ( % ) to carry out the for command at the command prompt. Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c.

What does @echo off do?

The ECHO-ON and ECHO-OFF commands are used to enable and disable the echoing, or displaying on the screen, of characters entered at the keyboard. If echoing is disabled, input will not appear on the terminal screen as it is typed. By default, echoing is enabled.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


2 Answers

find prints the dashes if it processes a file. It doesn't, when processing from STDIN (type file.ext /c |find "string" prints the count only).

There is a trick to write without linefeed: <nul set /p"=Hello"

If you can live with another order, it's quite easy to assemble it::

@echo off
for %%f in (*.bat) do (
  <nul set /p "=%%f %%~tf "
  type %%f|find /c "echo"
)

If you want to keep your order it's a little bit more complicated: you can't force find to write without linefeed, so you have to use a trick (another for):

@echo off
(for %%f in (*.txt) do (
  <nul set /p "=%%f: "
  for /f %%i in ('type %%f^|find /c "valid"') do (<nul set /p "=%%i ")
  echo %%~tf
))>LogsReceived.txt
like image 144
Stephan Avatar answered Oct 22 '22 00:10

Stephan


You may get the output of find command via another for and put it at any place you wish:

@echo off

(for %%f in (*.log) do (
   for /F %%c in ('find /c "valid" ^< %%f') do echo %%f: %%c %%~tf
)) > LogsReceived.txt
like image 26
Aacini Avatar answered Oct 21 '22 22:10

Aacini