1

I would like to create a logfile with this information : echo %time%;%date%;%computername%;"findstr /I "%%a" %MYFILES%\Dir_ALL.txt" >> %computername%_File.csv

How can I write something that will not write "findstr /I ..." but the output of this command ?

I would like to have everything in the same line on my output file.

Thank you

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Isabelle
  • 33
  • 1
  • 5

2 Answers2

1

You have to set a variable with the command result. This is answered in Windows Batch help in setting a variable from command output, using a dummy for-loop whose loop variable is set from the content of a file.

That would be something like this:

findstr /I "%%a" %MYFILES%\Dir_ALL.txt >%temp%\temp.txt
for /f %%i in (%temp%\temp.txt) do echo %time%;%date%;%computername%;%%i >> %computername%_File.csv
del %temp%\temp.txt

There is a limitation on this: the variable cannot contain multiple lines. However, rephrasing your script fragment as a loop would probably solve that issue as well.

The MSDN article on set shows some additional features which you can use to control how the data from the file is parsed. Normally it parses the result into tokens separated by spaces. But you can override that using the delims keyword.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Thank you for your answer. When trying your solution, if %%i contains a space, the result is truncated in my csv file. Is there a way without having to create a temp file (as there is another loop before and I think it will not be the most efficient way to do that) ? – Isabelle May 06 '15 at 08:45
  • You might be able to work around the space by quoting (unsure without some testing). The temp-file is used because the `set` command has no way to read the output of a command (the closest is the `/p` option, which does not seem to be a way to solve the problem). – Thomas Dickey May 06 '15 at 09:01
1

write a line without linefeed with set /p, followed by the second line:

<nul set /p .=%time%;%date%;%computername%;>>%computername%_File.csv
findstr /I "%%a" %MYFILES%\Dir_ALL.txt>>%computername%_File.csv

Note: Because of the %%a I guess, you are using this codefragment inside a forstatement. I suggest using !time! instead of %time% to get the actual time (using delayed expansion of course)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you I will try that. Can you tell me why I need to use "!" instead of "%" if I am in a "FOR" statement ? (sorry for my noob question). – Isabelle May 06 '15 at 09:38
  • no problem. The whole `for`statement is parsed at once, so `%time%` would not change (may be a problem, if you loop for some time) `!time!` will be evaluated each time, it occures). See [here](http://stackoverflow.com/a/26786046/2152082) or [here](http://stackoverflow.com/a/20854972/2152082) for a demonstration. – Stephan May 07 '15 at 05:36