3

Given a windows .cmd file abc.cmd

abc.cmd > output.log

The above command line operation would save the output of the execution to output.log file.

Are there options for running this command so as to create multiple copies of this log. That is, I want one copy created in one location, and another in a different location.

Please do not ask my to run a copy command. I am looking for command line "options".

4
  • 1
    copy output.log \path\to\new\location\copy1.log Commented Oct 24, 2014 at 19:51
  • 1
    > is a redirection operator, not a command, and does not have any options. Commented Oct 24, 2014 at 19:55
  • @admdrew There are several options for using redirection as could be found here: ss64.com/nt/syntax-redirection.html. I am just not entirely sure if multiple redirection is not one of them. Commented Oct 24, 2014 at 20:01
  • 1
    Possible duplicate of how to redirect a output of a command to two files Commented Jan 15, 2016 at 11:01

4 Answers 4

4

Any Windows implementation of the unix tee command would work perfectly. There are free options out there. I like to use the GNU utilities for Windows, which includes tee.exe.

Usage is simple:

abc.cmd | tee out1.txt >out2.txt

Or, you could write your own implementation of tee using Windows Scripting Host (WSH) - no exe to download! triggeradeadcat attempted to do so, but that implementation is flawed because it uses line based input/output instead of character based. It will not work well if the command has interactive prompts and responses on the same line.

Below is a handy JScript implementation that I have used in many projects. I use hybrid JScript/batch techniques so that the utility can be called directly without having to specify CSCRIPT.

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::--- Batch section within JScript comment that calls the internal JScript ----
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b

----- End of JScript comment, beginning of normal JScript  ------------------*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var mode=2;
if (WScript.Arguments.Count()==2) {mode=8;}
var out = fso.OpenTextFile(WScript.Arguments(0),mode,true);
var chr;
while( !WScript.StdIn.AtEndOfStream ) {
  chr=WScript.StdIn.Read(1);
  WScript.StdOut.Write(chr);
  out.Write(chr);
}

Again, the usage is simple:

abc.cmd | tee out1.txt >out2.txt
Sign up to request clarification or add additional context in comments.

Comments

1

No there is no such command line options present as already commented by others but you can run the command multiple times redirecting them to different files like below

date > D:\Testing\test.log & date > D:\Testing\test1.log

So in your case it would be like

abc.cmd > output.log & abc.cmd > output1.log

2 Comments

Can you guarantee that each run of abc.cmd yields the same result? This just seems like a bad idea to me. Perhaps this can be used in some circumstances, but in general it is no good. Also, I see no advantage of this over simply using COPY to replicate the output file.
Expanding upon dbenham's comment here - this would be disastrous for commands where large volumes of files are being copied, or if there's a command that simply takes a long time to execute, etc.
0

From the site you posted:

command > fileA > fileB - Redirect output to separate files.

Is that not what you're looking for?

5 Comments

Looking to create copies instead of splitting the output.
This will never work. You will end up getting only fileB and not two copies.
Interesting. This is something I've never tried before so your question and Rahul's answer were useful to me as well.
That sends stderror to file B, not file A to file B. You need to find a utility called Tee. It's a standard unix command and many ports to windows.
@triggeradeadcat, is there a windows equivalent to the 'tee' command?
0
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile(wscript.Arguments(0), vbtrue)
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            outp.writeline Line
            file.writeline Line
        Loop

Haven't tested.

dir |cscript tee.vbs filename1.txt > filename2.txt

1 Comment

Good idea, but you should not use line based IO - it does not work well with programs that prompt for input. Also, you can use hybrid scripting to make it easier to call. See my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.