Redirecting Output from within Batch file
I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.
I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?
Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.
windows batch-file cmd batch-processing
add a comment |
I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.
I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?
Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.
windows batch-file cmd batch-processing
add a comment |
I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.
I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?
Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.
windows batch-file cmd batch-processing
I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.
I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?
Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.
windows batch-file cmd batch-processing
windows batch-file cmd batch-processing
edited Mar 8 '18 at 11:40
PLNech
2,0871435
2,0871435
asked Dec 10 '13 at 1:09
user3085030user3085030
376133
376133
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.
@echo off
command1 >output.txt
command2 >>output.txt
...
commandN >>output.txt
A better way - easier to write, and faster because the file is opened and positioned only once.
@echo off
>output.txt (
command1
command2
...
commandN
)
Another good and fast way that only opens and positions the file once
@echo off
call :sub >output.txt
exit /b
:sub
command1
command2
...
commandN
1
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
3
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
1
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.
– dbenham
Dec 7 '16 at 17:08
3
@ThariqNugrohotomo ->output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
2
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Executecmd /?
orhelp cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.
– dbenham
Jan 14 '18 at 23:22
|
show 6 more comments
if you want both out and err streams redirected
dir >> a.txt 2>&1
16
+1. It's also worth pointing out that using>>
will append toa.txt
. To overwritea.txt
instead, use>
. stackoverflow.com/q/4458231/1098302
– Aaron
Apr 19 '17 at 19:15
add a comment |
I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)
To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:
@echo off
> filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
echo Your Name Here
echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
REM do some stuff here
echo Your Name Here
echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
)
Will create a file like what you see in this screenshot of the file in the target directory
That will contain this output:
Your Name Here
Beginning Date/Time: 2016-09-16_141048.log
Your Name Here
Ending Date/Time: 2016-09-16_141048.log
Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
1
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
add a comment |
@echo off
>output.txt (
echo Checking your system infor, Please wating...
systeminfo | findstr /c:"Host Name"
systeminfo | findstr /c:"Domain"
ipconfig /all | find "Physical Address"
ipconfig | find "IPv4"
ipconfig | find "Default Gateway"
)
@pause
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
add a comment |
echo some output >"your logfile"
or
(
echo some output
echo more output
)>"Your logfile"
should fill the bill.
If you want to APPEND
the output, use >>
instead of >
. >
will start a new logfile.
add a comment |
There is a cool little program you can use to redirect the output to a file and the console
some_command ^| TEE.BAT [ -a ] filename
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
add a comment |
@echo OFF
[your command] >> [Your log file name].txt
I used the command above in my batch file and it works. In the log file, it shows the results of my command.
add a comment |
Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:
if not "%1"=="STDOUT_TO_FILE" %0 STDOUT_TO_FILE %* >log.txt 2>&1
shift /1
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20484151%2fredirecting-output-from-within-batch-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.
@echo off
command1 >output.txt
command2 >>output.txt
...
commandN >>output.txt
A better way - easier to write, and faster because the file is opened and positioned only once.
@echo off
>output.txt (
command1
command2
...
commandN
)
Another good and fast way that only opens and positions the file once
@echo off
call :sub >output.txt
exit /b
:sub
command1
command2
...
commandN
1
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
3
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
1
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.
– dbenham
Dec 7 '16 at 17:08
3
@ThariqNugrohotomo ->output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
2
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Executecmd /?
orhelp cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.
– dbenham
Jan 14 '18 at 23:22
|
show 6 more comments
The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.
@echo off
command1 >output.txt
command2 >>output.txt
...
commandN >>output.txt
A better way - easier to write, and faster because the file is opened and positioned only once.
@echo off
>output.txt (
command1
command2
...
commandN
)
Another good and fast way that only opens and positions the file once
@echo off
call :sub >output.txt
exit /b
:sub
command1
command2
...
commandN
1
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
3
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
1
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.
– dbenham
Dec 7 '16 at 17:08
3
@ThariqNugrohotomo ->output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
2
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Executecmd /?
orhelp cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.
– dbenham
Jan 14 '18 at 23:22
|
show 6 more comments
The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.
@echo off
command1 >output.txt
command2 >>output.txt
...
commandN >>output.txt
A better way - easier to write, and faster because the file is opened and positioned only once.
@echo off
>output.txt (
command1
command2
...
commandN
)
Another good and fast way that only opens and positions the file once
@echo off
call :sub >output.txt
exit /b
:sub
command1
command2
...
commandN
The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.
@echo off
command1 >output.txt
command2 >>output.txt
...
commandN >>output.txt
A better way - easier to write, and faster because the file is opened and positioned only once.
@echo off
>output.txt (
command1
command2
...
commandN
)
Another good and fast way that only opens and positions the file once
@echo off
call :sub >output.txt
exit /b
:sub
command1
command2
...
commandN
edited Dec 13 '18 at 15:40
answered Dec 10 '13 at 1:17
dbenhamdbenham
101k20180284
101k20180284
1
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
3
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
1
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.
– dbenham
Dec 7 '16 at 17:08
3
@ThariqNugrohotomo ->output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
2
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Executecmd /?
orhelp cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.
– dbenham
Jan 14 '18 at 23:22
|
show 6 more comments
1
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
3
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
1
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.
– dbenham
Dec 7 '16 at 17:08
3
@ThariqNugrohotomo ->output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
2
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Executecmd /?
orhelp cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.
– dbenham
Jan 14 '18 at 23:22
1
1
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
Love the solutions where I can set it for the remainder of the file
– Sam
Feb 2 '16 at 22:36
3
3
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
– Jannes
Dec 7 '16 at 16:41
1
1
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,
%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.– dbenham
Dec 7 '16 at 17:08
@Jannes - True, but you can always get info about the running batch script if you add a modifier. For example,
%~f0
always gives the full path to the batch script, even when inside a CALLed :subroutine.– dbenham
Dec 7 '16 at 17:08
3
3
@ThariqNugrohotomo -
>output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
@ThariqNugrohotomo -
>output.txt 2>&1
– dbenham
Mar 21 '17 at 10:11
2
2
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute
cmd /?
or help cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.– dbenham
Jan 14 '18 at 23:22
@Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute
cmd /?
or help cmd
from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.– dbenham
Jan 14 '18 at 23:22
|
show 6 more comments
if you want both out and err streams redirected
dir >> a.txt 2>&1
16
+1. It's also worth pointing out that using>>
will append toa.txt
. To overwritea.txt
instead, use>
. stackoverflow.com/q/4458231/1098302
– Aaron
Apr 19 '17 at 19:15
add a comment |
if you want both out and err streams redirected
dir >> a.txt 2>&1
16
+1. It's also worth pointing out that using>>
will append toa.txt
. To overwritea.txt
instead, use>
. stackoverflow.com/q/4458231/1098302
– Aaron
Apr 19 '17 at 19:15
add a comment |
if you want both out and err streams redirected
dir >> a.txt 2>&1
if you want both out and err streams redirected
dir >> a.txt 2>&1
answered Mar 12 '15 at 17:59
Kalpesh SoniKalpesh Soni
3,34713239
3,34713239
16
+1. It's also worth pointing out that using>>
will append toa.txt
. To overwritea.txt
instead, use>
. stackoverflow.com/q/4458231/1098302
– Aaron
Apr 19 '17 at 19:15
add a comment |
16
+1. It's also worth pointing out that using>>
will append toa.txt
. To overwritea.txt
instead, use>
. stackoverflow.com/q/4458231/1098302
– Aaron
Apr 19 '17 at 19:15
16
16
+1. It's also worth pointing out that using
>>
will append to a.txt
. To overwrite a.txt
instead, use >
. stackoverflow.com/q/4458231/1098302– Aaron
Apr 19 '17 at 19:15
+1. It's also worth pointing out that using
>>
will append to a.txt
. To overwrite a.txt
instead, use >
. stackoverflow.com/q/4458231/1098302– Aaron
Apr 19 '17 at 19:15
add a comment |
I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)
To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:
@echo off
> filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
echo Your Name Here
echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
REM do some stuff here
echo Your Name Here
echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
)
Will create a file like what you see in this screenshot of the file in the target directory
That will contain this output:
Your Name Here
Beginning Date/Time: 2016-09-16_141048.log
Your Name Here
Ending Date/Time: 2016-09-16_141048.log
Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
1
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
add a comment |
I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)
To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:
@echo off
> filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
echo Your Name Here
echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
REM do some stuff here
echo Your Name Here
echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
)
Will create a file like what you see in this screenshot of the file in the target directory
That will contain this output:
Your Name Here
Beginning Date/Time: 2016-09-16_141048.log
Your Name Here
Ending Date/Time: 2016-09-16_141048.log
Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
1
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
add a comment |
I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)
To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:
@echo off
> filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
echo Your Name Here
echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
REM do some stuff here
echo Your Name Here
echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
)
Will create a file like what you see in this screenshot of the file in the target directory
That will contain this output:
Your Name Here
Beginning Date/Time: 2016-09-16_141048.log
Your Name Here
Ending Date/Time: 2016-09-16_141048.log
Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.
I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)
To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:
@echo off
> filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
echo Your Name Here
echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
REM do some stuff here
echo Your Name Here
echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
)
Will create a file like what you see in this screenshot of the file in the target directory
That will contain this output:
Your Name Here
Beginning Date/Time: 2016-09-16_141048.log
Your Name Here
Ending Date/Time: 2016-09-16_141048.log
Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.
answered Sep 16 '16 at 18:20
AnthonyAnthony
15617
15617
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
1
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
add a comment |
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
1
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
– Moondra
Jan 14 '18 at 18:50
1
1
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
@Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.
– Anthony
Apr 17 '18 at 18:32
add a comment |
@echo off
>output.txt (
echo Checking your system infor, Please wating...
systeminfo | findstr /c:"Host Name"
systeminfo | findstr /c:"Domain"
ipconfig /all | find "Physical Address"
ipconfig | find "IPv4"
ipconfig | find "Default Gateway"
)
@pause
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
add a comment |
@echo off
>output.txt (
echo Checking your system infor, Please wating...
systeminfo | findstr /c:"Host Name"
systeminfo | findstr /c:"Domain"
ipconfig /all | find "Physical Address"
ipconfig | find "IPv4"
ipconfig | find "Default Gateway"
)
@pause
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
add a comment |
@echo off
>output.txt (
echo Checking your system infor, Please wating...
systeminfo | findstr /c:"Host Name"
systeminfo | findstr /c:"Domain"
ipconfig /all | find "Physical Address"
ipconfig | find "IPv4"
ipconfig | find "Default Gateway"
)
@pause
@echo off
>output.txt (
echo Checking your system infor, Please wating...
systeminfo | findstr /c:"Host Name"
systeminfo | findstr /c:"Domain"
ipconfig /all | find "Physical Address"
ipconfig | find "IPv4"
ipconfig | find "Default Gateway"
)
@pause
edited Feb 20 '15 at 20:35
Wesley Bland
6,74833249
6,74833249
answered Feb 20 '15 at 20:07
saifsaif
8111
8111
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
add a comment |
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
I think that this is the most elegant way. Thanks Wesley!
– Kiswanij
Aug 23 '18 at 4:37
add a comment |
echo some output >"your logfile"
or
(
echo some output
echo more output
)>"Your logfile"
should fill the bill.
If you want to APPEND
the output, use >>
instead of >
. >
will start a new logfile.
add a comment |
echo some output >"your logfile"
or
(
echo some output
echo more output
)>"Your logfile"
should fill the bill.
If you want to APPEND
the output, use >>
instead of >
. >
will start a new logfile.
add a comment |
echo some output >"your logfile"
or
(
echo some output
echo more output
)>"Your logfile"
should fill the bill.
If you want to APPEND
the output, use >>
instead of >
. >
will start a new logfile.
echo some output >"your logfile"
or
(
echo some output
echo more output
)>"Your logfile"
should fill the bill.
If you want to APPEND
the output, use >>
instead of >
. >
will start a new logfile.
answered Dec 10 '13 at 1:15
MagooMagoo
60.2k54068
60.2k54068
add a comment |
add a comment |
There is a cool little program you can use to redirect the output to a file and the console
some_command ^| TEE.BAT [ -a ] filename
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
add a comment |
There is a cool little program you can use to redirect the output to a file and the console
some_command ^| TEE.BAT [ -a ] filename
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
add a comment |
There is a cool little program you can use to redirect the output to a file and the console
some_command ^| TEE.BAT [ -a ] filename
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
There is a cool little program you can use to redirect the output to a file and the console
some_command ^| TEE.BAT [ -a ] filename
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Keep variables local
SETLOCAL
:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
SET Append=1
SHIFT
)
IF [%1]== GOTO Syntax
IF NOT [%2]== GOTO Syntax
:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
SET Counter=
GOTO Syntax
)
:: A valid filename seems to have been specified
SET File=%1
:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
SET File=
GOTO Syntax
)
:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y
:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
:: Done
ENDLOCAL
GOTO:EOF
:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF
:Syntax
ECHO.
ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
IF NOT "%OS%"=="Windows_NT" GOTO Skip
ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
:Skip
ECHO.
ECHO Where: "some_command" is the command whose output should be redirected
ECHO "filename" is the file the output should be redirected to
ECHO -a appends the output of the command to the file,
ECHO rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron
edited Dec 11 '17 at 17:14
T.S.
9,769103253
9,769103253
answered Apr 24 '15 at 12:06
AquaAlexAquaAlex
275413
275413
add a comment |
add a comment |
@echo OFF
[your command] >> [Your log file name].txt
I used the command above in my batch file and it works. In the log file, it shows the results of my command.
add a comment |
@echo OFF
[your command] >> [Your log file name].txt
I used the command above in my batch file and it works. In the log file, it shows the results of my command.
add a comment |
@echo OFF
[your command] >> [Your log file name].txt
I used the command above in my batch file and it works. In the log file, it shows the results of my command.
@echo OFF
[your command] >> [Your log file name].txt
I used the command above in my batch file and it works. In the log file, it shows the results of my command.
edited Jul 18 '18 at 2:42
Pang
6,9011664101
6,9011664101
answered Jul 18 '18 at 2:36
Indra PermanaIndra Permana
215
215
add a comment |
add a comment |
Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:
if not "%1"=="STDOUT_TO_FILE" %0 STDOUT_TO_FILE %* >log.txt 2>&1
shift /1
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
add a comment |
Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:
if not "%1"=="STDOUT_TO_FILE" %0 STDOUT_TO_FILE %* >log.txt 2>&1
shift /1
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
add a comment |
Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:
if not "%1"=="STDOUT_TO_FILE" %0 STDOUT_TO_FILE %* >log.txt 2>&1
shift /1
Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:
if not "%1"=="STDOUT_TO_FILE" %0 STDOUT_TO_FILE %* >log.txt 2>&1
shift /1
answered Nov 1 '18 at 15:46
ilgitanoilgitano
6113
6113
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
add a comment |
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
– CCarlos
Nov 23 '18 at 19:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20484151%2fredirecting-output-from-within-batch-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown