

InterviewSolution
Saved Bookmarks
1. |
Solve : loggin console output of batch to text file without using redirect? |
Answer» <html><body><p>Hi,<br/>Can any one <a href="https://interviewquestions.tuteehub.com/tag/plz-1718368" style="font-weight:bold;" target="_blank" title="Click to know more about PLZ">PLZ</a> help me for the following case:<br/>I want to save batch <a href="https://interviewquestions.tuteehub.com/tag/file-11330" style="font-weight:bold;" target="_blank" title="Click to know more about FILE">FILE</a> console output with out using a redirect ie if we <a href="https://interviewquestions.tuteehub.com/tag/use-241643" style="font-weight:bold;" target="_blank" title="Click to know more about USE">USE</a> redirect command ">" the we have to execte batch in following way:<br/>C:\mybatch.bat >C:\batchlog.txt<br/>I need it to be in same batch file only, so that by only <a href="https://interviewquestions.tuteehub.com/tag/passing-1148520" style="font-weight:bold;" target="_blank" title="Click to know more about PASSING">PASSING</a> argument as -log="path to save batch'c log" that batch execute <br/>as well as simultaneously record its console output in text file.!<br/><br/>Thanks in advance ..!<br/><br/>ArpanBatch code does not support named arguments. However you might tag each command in your batch file with %2 which if <strong>-log=</strong> is present would redirect the output, and if not present, there would be no redirected output:<br/><br/><strong>batchfile.bat</strong><br/> Code: <a>[Select]</a>command1 %2<br/>command2<br/>command3 %2<br/><br/>if you run the batch file as <strong>batchfile -log=">>path to save batch'c log"</strong>, the output from commands 1 and 3 would be redirected. If you run the batch file as <strong>batchfile</strong> with no parameters, then none of the command output would be redirected.<br/><br/> <br/><br/><strong>Note:</strong> This method is not best practice. Readability would be increased if you simply used positional arguments on the command line. Not everyone might recognize that <strong>-log=">>path to save batch'c log" </strong>resolves to two parameters.</p></body></html> | |