How can I save the out put of a batch file to a text file and the output should be displayed on console too.
For Eg:
#ECHO OFF
ECHO Hello World
#pause
It will show Hello World on console
#ECHO OFF
ECHO Hello World >text.txt
#pause
It will save Hello World to text.txt
How can I make it both happen together?
Thanks in advance
Use 'FOR' command if you want: (yea this one only execute 1 command)
For /f "tokens=*" %%a in ('ECHO Hello world') do (
echo %%a
echo %%a >text.txt
)
But the solution below only useful if you want to send both output to console and file a command that show a lots of texts like 'SET' command.
So what about you create an external batch file then use it in your main file?
Example: Code for 'printBoth.bat'
#echo off
setlocal ENABLEDELAYEDEXPANSION
set string=
:loop
set string=!string!%1
shift
if not "%1"=="" goto loop
For /f "tokens=*" %%a in ('!string!') do (
echo %%a
echo %%a >text.txt
)
Now if you want to print to both console and file, just type this code: call printBoth.bat [type command in here]
If you want to get the outputs of commands:
if exist "file.txt" del file.txt /f /q
for /f "delims=" %%k in ('command here') do (
echo %%k
echo %%k>>file.txt
)
Note the >>. Don't use >. > would remove all the text in the file, then write the first output-ed line in the command, which is not good if there were multiple lines that will be "echo-ed" by FOR. >> creates a new line instead of replacing the line like >.
DELIMS= works like TOKENS=*, but DELIMS= won't include the executed command.
I added if exist "file.txt" del file.txt /f /q, in order to not append the new output-ed lines. You can remove that if you want to append the lines to the file.
For customized ECHO outputs,
#echo off
echo TEXTHERE & echo TEXTHERE >>file.txt
echo TEXTHERE2 & echo TEXTHERE >>file.txt
rem ...and so on
<command1> & <command2> means "do <command1>, then do <command2>"
If you don't want to append, use > instead of >>.
Related
Is there a way I can use the type command to replace a specific line in a batch file with the content of a text file?
For example, let's say I have a batch like this:
#echo off
echo hello world
::replace
pause>nul
exit
And I want to replace the ::replace line with the content of a text file for example:
echo this
echo was
echo replaced
Is that possible? if not is there another way i can do this?
This example replaces the line if it matches ::replace exactly, not contains. The first example replaces the line in the main script itself:
#echo off
set "infile=%~0"
for /f "delims=" %%i in ('type "%infile%" ^| find /v /n "" ^& break^>%infile%') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*]=!"
if "!line!" == "::replace" (
set line=
type replace_file.txt
)>>!infile!
echo(!line!>>!infile!
endlocal
)
rem sample replace section here.
#echo off
echo hello world
::replace
pause>nul
to have it replace the lines in another batch file, then add the name of the file where it says "ADD FILE NAME HERE":
#echo off
set "infile=ADD FILE NAME HERE"
for /f "delims=" %%i in ('type "%infile%" ^| find /v /n "" ^& break^>%infile%') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*]=!"
if "!line!" == "::replace" (
set line=
type replace_file.txt
)>>!infile!
echo(!line!>>!infile!
endlocal
)
a word of caution though, this is replacing the script itself by breaking the original and re-creating it while replacing what you ask it to. If you make a mistake and break the code, it will break your script, so always make a copy of the script/s you want to replace code in. you can do that automatically by adding a line in the script to copy the file before the for loop. Something like:
copy "%infile%" "%infile%.bck"
I want to append the results after I remove the .txt files and the the CIS153 directory from my code.
The problem is that it is not appending the correct results.
I found out how to append the results to a text file I am just not sure where I should place that code for my script.
SETLOCAL ENABLEDELAYEDEXPANSION
SET CURRENT_DIR=%CD%
SET DIR_NAME=%CURRENT_DIR%CIS153
SET COUNTER=1
MKDIR %DIR_NAME% ECHO output.txt
FOR F DELIMS= %%X IN (MyData.txt) DO (
ECHO %%X CUsersAdministratorDesktopCIS153myFile!COUNTER!.txt
SET A COUNTER=!COUNTER!+1
)
DEL CUsersAdministratorDesktopCIS153.txt | echo
C:\Users\Administrator\Desktop\CIS153 > output.txt
RD S CUsersAdministratorDesktopCIS153 | echo
C:\Users\Administrator\Desktop\CIS153 > output.txt
I expect this code to output in the output.txt file.
" C:\Users\Administrator\Desktop>DEL
C:\Users\Administrator\Desktop\CIS153\*.txt
C:\Users\Administrator\Desktop>RD /S "C:\Users\Administrator\Desktop\CIS153"
C:\Users\Administrator\Desktop\CIS153, Are you sure (Y/N)? "
Instead the output im receiving in the output.txt file is this
"C:\Users\Administrator\Desktop\CIS153
C:\Users\Administrator\Desktop\CIS153"
As others have commented, your fundamentals are incorrect.
Here is some reading you can do regarding redirecting output.
If you run a .bat from the command line and redirect the output to a text file then you can replicate what you are trying to achieve. Example:
#echo off
:: Everything you put here won't display in the output file
setlocal enabledelayedexpansion
for /l %%i in (1,1,3) do set /a myVar%%i=!random!
#echo on
:: Everything you put here will display in the output file
for /l %%i in (1,1,3) do echo. !myVar%%i!
Run on the command line via test.bat > output.txt would leave an output.txt of:
C:\Test>for /L %i in (1 1 3) do echo. !myVar%i!
C:\Test>echo. !myVar1!
11828
C:\Test>echo. !myVar2!
30270
C:\Test>echo. !myVar3!
19262
I have the following batch file which outputs limited info. How do I suppress all output from this batch file? What I'd like to see is after I enter the name of the batch file and hit enter, the next thing that shows up on screen is "C:>".
#ECHO OFF
SETLOCAL EnableDelayedExpansion
(for /f "tokens=1 delims=;" %%A in (C:\ALL.txt) do (
#echo %%A | find /i "\"
if errorlevel 1 (
DEL "D:\!mypath!%%A" >> C:\ALL-OUT.txt 2>&1
) ELSE (
set mypath=%%A
)))
#endlocal
I think it all boils down to this line which I use to test whether %%A contains backslash:
#echo %%A | find /i "\"
I tried adding " 1>null" to the end of the line. It worked to suppress all output. However, it also created a file named "null".
Since "For /f" parses files line by line, I wonder if there is a way to incorporate the echo command in "For /f"?
The NUL device is referenced with nul, not null. >null creates a file named null.
for your second question: you can redirect the output of a command block with a single redirection (in fact, this is way faster)
#ECHO OFF
SETLOCAL EnableDelayedExpansion
(
for /f "delims=;" %%A in (C:\ALL.txt) do (
echo %%A | find "\" >nul
if errorlevel 1 (
DEL "D:\!mypath!%%A"
) ELSE (
set mypath=%%A
)
)
) > C:\ALL-OUT.txt 2>&1
endlocal
I have a text file which contains a list of URLs separated by comma.
e.g. URL1,URL2,URL3 etc.
I have a batch script which just launches the url in the text file.
for /f "delims=," %%a in (ListOfURLs.txt) do (start /wait iexplore.exe %%a)
Currently, it only launches the 1st URL and stops. It is not reading the subsequent URLs.
Help needed.
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%u IN (listofurls.txt) DO (
FOR %%i IN (%%u) DO ECHO START iexplore %%i
)
the ECHO is to show what is intended. To execute, remove the ECHO keyword.
I have a first.properties file and I am reading that file, skipping the first 5 lines and creating a new file with the remaining lines of first.properties. This is working fine. But I am getting spaces for each line in the newly created file. I want to remove these spaces.
I have used the following piece of code:
#echo off
set new=0
setlocal ENABLEDELAYEDEXPANSION
for /F "skip=5" %%L in (first.properties) do (
echo %%L>>NewProjectsInfo.properties
)
endlocal
pause
first.properties is as follows:
name=abcd
number=bcde
address=cdef
mobile=efgh
worklocation=ghij
Please help me out to remove the spaces.
Try this. There can be issues when numbers directly precede the >> characters.
You are not using ENABLEDELAYEDEXPANSION so I removed it too.
#echo off
set new=0
setlocal
for /F "skip=5 delims=" %%L in (first.properties) do (
>>NewProjectsInfo.properties echo %%L
)
endlocal
pause
There is a space after NewProjectsInfo.properties in your code. if that is removed, it works as you expect.
It's sad, but looks like, batch interprets command as entire line & separates out the redirection part. In bash, for example, the command terminates at > symbol itself.
e.g. echo hello >somefile.txt world will put hello world in somefile.txt.
#ECHO OFF
SETLOCAL
(
FOR /f "skip=5 delims=" %%i IN (rltsf.txt) DO CALL :unpad %%i
)>output.txt
FC rltsf.txt output.txt
GOTO :eof
:unpad
ECHO %*
GOTO :eof
This approach seemst to work, but I fancy it may be a little sensitive to line-content. Easier to test if we have representative data...