<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; CMD/BAT: дублирование вывода команды]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17105&amp;type=atom" />
	<updated>2022-05-09T22:34:41Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=17105</id>
		<entry>
			<title type="html"><![CDATA[Re: CMD/BAT: дублирование вывода команды]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153154#p153154" />
			<content type="html"><![CDATA[<p>Открыл обсуждение в теме: <a href="https://forum.script-coding.com/viewtopic.php?id=17116">CMD/BAT: дублирование вывода команды (обсуждение)</a>.</p>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2022-05-09T22:34:41Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153154#p153154</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[CMD/BAT: дублирование вывода команды]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153089#p153089" />
			<content type="html"><![CDATA[<p><em>Без гарантий. Используете на свой страх и риск.</em></p><p>Можно попробовать дублировать вывод StdOut консольной команды одновременно в консоль и файл:<br /></p><div class="codebox"><pre><code>
команда | CScript [имя скрипта] /параметры
</code></pre></div><p>CScript забирает выходной поток команды, после чего, с помощью WSH скрипта, возвращает его в консоль и дублирует в файл.<br />OC Win 7</p><p>Получение списка процессов:<br /><strong>plist.cmd</strong><br /></p><div class="codebox"><pre><code>
@set @x=1/*
::
:: Список процессов с дублированием вывода
::

 @echo off
 setlocal enableextensions
 rem chcp 1251&gt;nul

 wmic process list brief 2&gt;&amp;1|CScript /e:jscript &quot;%~f0&quot;
 pause
 exit /b 0

::--------------------------------------------
*/

redirect();
WScript.Quit();

//
// Дублирование вывода в консоль и файл
//
function redirect()
{

	var sdata;
	var oFs = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
	//------------------------------------
	sFilePath = WScript.ScriptFullName;
	sFileOut = sFilePath.substring(0,sFilePath.lastIndexOf(&#039;\\&#039;)+1) + &quot;proclist.txt&quot;;

	var oStream = oFs.OpenTextFile(sFileOut, 2, true, 0);
	var ostdin = oFs.GetStandardStream(0, false);
	var ostdout = oFs.GetStandardStream(1, false);
	//------------------------------------
	sdata = ostdin.ReadAll();

	//Дублирование вывода
	ostdout.Write(sdata);
	oStream.Write(sdata);
	oStream.Close();
}
</code></pre></div><p>Тестирование rar и zip архивов с помощью 7z, рекурсивно, с выбором начального каталога:<br /><strong>7ztest.cmd</strong><br /></p><div class="codebox"><pre><code>
@set @x=1/*
::
:: Тест архивов с помощью 7z с выбором начального каталога и дублированием вывода
::

 @echo off
 setlocal enableextensions
 rem chcp 1251&gt;nul

 for /f &quot;tokens=*&quot; %%a in (&#039;CScript /nologo /e:jscript &quot;%~f0&quot; /choosedir&#039;) do set dir1=%%a
 if /i &quot;%dir1%&quot; equ &quot;&quot; (exit /b 0)

 rem [проверка на слеш в конце строки, на случай если выбран диск]
 
 if &quot;%dir1:~-1%&quot; equ &quot;\&quot; (set dir1=%dir1:~0,-1%)
 set dir1=&quot;%dir1%&quot;

 echo Init directory %dir1%
 pause

::
:: Проверка архивов с помощью 7z
::
::--------------------------------------------
 rem &quot;C:\Program Files\7-Zip\7z&quot; t -an -air!%dir1%\*.rar -air!%dir1%\*.zip -air!%dir1%\*.7z

 &quot;C:\Program Files\7-Zip\7z&quot; t -an -air!%dir1%\*.rar -air!%dir1%\*.zip 2&gt;&amp;1|CScript /e:jscript &quot;%~f0&quot; /doit
 pause
 exit /b 0

::--------------------------------------------
*/

var oArgs = WScript.Arguments
if (oArgs.Length==0){WScript.Quit();}

var rearg = /doit/i; 
var f = rearg.test(String(oArgs.Item(0)))
if (f){redirect();}else{choosedir();}
WScript.Quit();

//
// Диалог выбора целевого каталога на JScript
//
function choosedir()
{
	var oShell = new ActiveXObject(&#039;Shell.Application&#039;);
	var oFolder = oShell.BrowseForFolder(0, &quot;Выбор папки&quot;, 1, &quot;&quot;);
	
	if (Number(isNaN(oFolder))==1){WScript.Echo(oFolder.Self.Path);}
	
}

//
// Дублирование вывода 7z в консоль и файл
//
function redirect()
{
	var re1 = /path|ok/i;
	var re2 = /error|warning/i;
	var f1 = false;
	var f2 = false;
	var sdata;
	var oFs = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);

	//---------------------------------------------------------
	sFilePath = WScript.ScriptFullName;
	sFileOut = sFilePath.substring(0, sFilePath.lastIndexOf(&#039;\\&#039;)+1) + &quot;myfile.txt&quot;;

	var oStream = oFs.OpenTextFile(sFileOut, 2, true, 0);
	oStream.WriteLine(&quot;testing archives&quot;);

	var ostdin = oFs.GetStandardStream(0, false);
	var ostdout = oFs.GetStandardStream(1, false);

	//---------------------------------------------------------
	while (!ostdin.AtEndOfStream)
	{
		sdata = ostdin.ReadLine();
		ostdout.WriteLine(sdata);

		if (f2){oStream.WriteLine(sdata);}
		f1 = re1.test(sdata);
		f2 = re2.test(sdata);
		if (f1||f2){oStream.WriteLine(sdata);}
	}
	oStream.Close();
}
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Poltergeyst]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=83</uri>
			</author>
			<updated>2022-05-06T18:55:47Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153089#p153089</id>
		</entry>
</feed>
