I would like to create a logfile with this information : echo %time%;%date%;%computername%;"findstr /I "%%a" %MYFILES%\Dir_ALL.txt" >> %computername%_File.csv
我想创建一个包含以下信息的日志文件: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 ?
我怎么能写一些不会写“findstr / I ...”的东西,但这个命令的输出呢?
I would like to have everything in the same line on my output file.
我想在输出文件的同一行中包含所有内容。
Thank you
2 个解决方案
#1
write a line without linefeed with set /p
, followed by the second line:
用set / p写一行没有换行的行,然后是第二行:
<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 for
statement. I suggest using !time!
instead of %time%
to get the actual time (using delayed expansion of course)
注意:由于我猜想%%,你在一个forstatement中使用这个代码片段。我建议使用!时间!而不是%time%来获得实际时间(当然使用延迟扩展)
#2
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.
您必须使用命令结果设置变量。在Windows Batch帮助中,使用从文件内容设置循环变量的伪for循环,在命令输出中设置变量时,可以回答这一问题。
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.
有关set的MSDN文章显示了一些其他功能,您可以使用这些功能来控制如何解析文件中的数据。通常它会将结果解析为由空格分隔的标记。但您可以使用delims关键字覆盖它。
#1
write a line without linefeed with set /p
, followed by the second line:
用set / p写一行没有换行的行,然后是第二行:
<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 for
statement. I suggest using !time!
instead of %time%
to get the actual time (using delayed expansion of course)
注意:由于我猜想%%,你在一个forstatement中使用这个代码片段。我建议使用!时间!而不是%time%来获得实际时间(当然使用延迟扩展)
#2
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.
您必须使用命令结果设置变量。在Windows Batch帮助中,使用从文件内容设置循环变量的伪for循环,在命令输出中设置变量时,可以回答这一问题。
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.
有关set的MSDN文章显示了一些其他功能,您可以使用这些功能来控制如何解析文件中的数据。通常它会将结果解析为由空格分隔的标记。但您可以使用delims关键字覆盖它。