how do you echo text from a file using batch? - batch-file

okay, so I'm trying to make a batch file that just echos the text inside a file called "test.txt" and my code is
#echo off
cls
(
echo %0%
echo %1%
echo %2%
echo %3%
echo %4%
echo %5%
echo %6%
echo %7%
echo %8%
echo %9%
echo %10%
echo %11%
echo %12%
echo %13%
echo %14%
echo %15%
echo %16%
) <test.txt
pause >nul
but for some very strange reason I couldn't find any answers for anywhere, my output is
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
0
1
2
3
4
5
6
and I really don't understand why.

If you want to echo the text that's inside your test.txt file, then you need to add this line to your batch file as already said by #Blorgbeard.
type C:\..\test.txt
#pause
Adding this line to your batch file will display the text/contents of a file in the command prompt window.

Copy the numbers below into test.txt on your desktop.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.
Now copy and paste the code below into print.bat on your desktop.
#ECHO OFF
type "%UserProfile%\Desktop\test.txt"
#pause
now run print.bat
REM:: Big thanks to Rakitić, Blorgbeard & Hackoo.

With this batch file you can echo your test.txt file :
#echo off
Set "MyFile=test.txt"
for /f "delims=" %%a in ('Type "%MyFile%"') Do (
echo %%a )
pause

Related

Is there a way to write a variables name in a bat file using bat?

I'm asking this because I want to make a download file for a simple website IP finder I made. Here is the code so far:
#echo off
color 03
SET /P user=Enter the user you are using here:
echo.>>"C:\Users\%user%\desktop\WebIPFind.bat"
echo #echo off > WebIPFind.bat
echo color 03 >> WebIPFind.bat
echo :loop >> WebIPFind.bat
echo SET /A ip=%ip%+1 >> WebIPFind.bat
echo ECHO IP No. %ip% >> WebIPFind.bat
echo SET /P website=Enter a website URL: >> WebIPFind.bat
echo ping %website% -l 8 -w 1 -n 1 >> WebIPFind.bat
echo goto :loop >> WebIPFind.bat
but when I use this it just replaces the variables with blank spaces. Is there a way to make it so it writes the names of the variables? (I don't need the user variable to be written because that is used to select where to write the bat file)
Your modified sample, like T3RROR explained
#echo off
color 03
SET /P user=Enter the user you are using here:
(
echo(
echo #echo off
echo color 03
echo :loop
echo SET /A ip=ip+1
echo ECHO IP No. %%ip%%
echo SET /P website=Enter a website URL:
echo ping %%website%% -l 8 -w 1 -n 1
echo goto :loop
) >>"C:\Users\%user%\desktop\WebIPFind.bat"

How to Set FINDSTR value to variable in looping [duplicate]

This question already has answers here:
Defining and using a variable in batch file
(4 answers)
Closed 3 years ago.
so i have a problem with set a variable with FINDSTR value in it. I have to set the variable so i can output the value to text file that i wanted
i already tried to set the variable in for looping every findstr value that i get, but the output didnt like i wish too
set /p tanggal="Masukan Bulan/Tahun: "
echo.
set /p namaService="Masukan services path: "
setlocal enabledelayedexpansion
echo Services,tanggal,hit >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
FOR /L %%A IN (1,1,31) DO (
if %%A LEQ 9 (
set "jumlahHit = FINDSTR /R /N "0%%A/%tanggal%.*%namaService%" access* | FIND /C ":""
echo !jumlahHit! >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
) else (
set "jumlahHit = FINDSTR /R /N "%%A/%tanggal%.*%namaService%" access* | FIND /C ":""
echo !jumlahHit! >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
)
)
echo Total : >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
FINDSTR /R /N "%tanggal%.*%namaService%" access* | FIND /C ":" >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
I expect the output to be some value with findstr result but the output in the file say echo is off
Services,tanggal,hit
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
Total :
152720
is that any other way to set the findstr result in looping?
You have two problems:
Variable Names
Variable names are exactly as you type them:
set variable = string
This sets variable{space} to the value {space}string. So to print that variable, you'd need to have
echo %variable %
This is why your lines are full of ECHO is off.
You can't set variable values from inline commands
An example of what I mean:
set variable=dir
echo %variable%
:: output is "dir"
See this question on how to set value of a variable to the output of a command.

How to read text from command prompt using a .bat

I am trying to get into windows batch files and programming in general, I messed around with an example I found online and made a batch file that creates another batch file (for the sake of learning)
But I now want to enable typing into a command prompt, I have a crude solution to this where I make a batch file, then create another batch file and start that file using the orignal .bat (don't worry, code is below, will make sense.)
Basically if you type 1, I want to do something in the batch, I know that I need to first do an if, but it's trying to get the text entered in the command line that I'm struggling with.... Any help would be great!
#echo off
if exist CommandTest.bat (
echo File already exists...
pause
) else (
echo CommandTest.bat does not exist. Creating the file...
timeout /t 3 /NOBREAK>nul
echo Please wait while your file loads...
echo #echo off >CommandTest.bat
echo color 0a >>CommandTest.bat
echo echo Line number 1 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 2 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 3 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 4 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
timeout /t 2 /NOBREAK>nul
start CommandTest.bat
timeout /t 5 /NOBREAK>nul
)
I am basically trying to redesign it to be user interactive...
According to your comment, choice is the right choice for you:
#echo off
echo 1 - do something
echo 2 - do something else
echo 3 - do nothing
choice /c 123 /m "take your choice "
if %errorlevel% == 1 echo let's do something
if %errorlevel% == 2 goto :other
if %errorlevel% == 3 echo let's do nothing
pause
goto :eof
:other
echo let's do something else
echo something else ...
pause

How to echo output redirection mark (>>) literally?

I'm creating a bat file that will write me a script that will be called by another bat.
What I wrote:
echo #echo off>>myscript-NTRE_TN_F
echo open 127.0.0.1>>myscript-NTRE_TN_F
echo user USER PASS>>myscript-NTRE_TN_F
echo prompt n>>myscript-NTRE_TN_F
echo ascii>>myscript-NTRE_TN_F
echo lcd C:\>>myscript-NTRE_TN_F
SET MAX_FILES=200
SET /S FILE_COUNT=0
for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
IF !FILE_COUNT! LSS %MAX_FILES% (
echo mget %%a>>myscript-NTRE_TN_F
echo echo %%a>>C:\Downloaded_Files.txt>>myscript-NTRE_TN_F
)
SET /A FILE_COUNT+=1
)
echo bye>>myscript-NTRE_TN_F
My issue is in the echo echo %%a part.
My output now is:
#echo off
open 127.0.0.1
user USER PASS
prompt n
ascii
lcd C:\
mget FILE1
echo FILE1
mget FILE2
echo FILE2
bye
I would like an OUTPUT like this:
#echo off
open 127.0.0.1
user USER PASS
prompt n
ascii
lcd C:\
mget FILE1
echo FILE1>>C:\Downloaded_Files.txt"
mget FILE2
echo FILE2>>C:\Downloaded_Files.txt
bye
I found something similar in the topic here:
Why gives my script an other output with the echo command?
I don't know how to manage the variable.
Thanks in advance for your help.
(example)
echo echo %%a^>^>C:\Downloaded_Files.txt>>myscript-NTRE_TN_F
The caret ^ signifies that the following character be reproduced literally.
I suggest using this batch code:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ScriptFile=myscript-NTRE_TN_F"
>"%ScriptFile%" echo #echo off
>>"%ScriptFile%" echo open 127.0.0.1
>>"%ScriptFile%" echo user USER PASS
>>"%ScriptFile%" echo prompt n
>>"%ScriptFile%" echo ascii
>>"%ScriptFile%" echo lcd C:\
SET "MAX_FILES=200"
SET "FILE_COUNT=0"
for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
IF !FILE_COUNT! GEQ %MAX_FILES% goto Finish
>>"%ScriptFile%" echo mget %%a
set "ScriptLine=>>C:\Downloaded_Files.txt echo %%a"
>>"%ScriptFile%" echo !ScriptLine!
SET /A FILE_COUNT+=1
)
:Finish
>>"%ScriptFile%" echo bye
endlocal
The redirection operator can be also put at beginning of a line which makes it easier to read what is written into the file.
Inside the FOR loop the IF condition is changed to exit the loop on reaching value of MAX_FILES.
And the line to output is first assigned to a variable enclosed in double quotes to get >> interpreted as string and the value of this variable is output using delayed expansion with redirection to file.
Please read also answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why using double quotes around variable=value.
See also the Microsoft article Using command redirection operators
Another even better solution is:
#echo on
setlocal EnableExtensions EnableDelayedExpansion
set "ScriptFile=myscript-NTRE_TN_F"
(
echo #echo off
echo open 127.0.0.1
echo user USER PASS
echo prompt n
echo ascii
echo lcd C:\
)>"%ScriptFile%"
SET "MAX_FILES=200"
SET "FILE_COUNT=0"
for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
IF !FILE_COUNT! GEQ %MAX_FILES% goto Finish
echo mget %%a
set "ScriptLine=>>C:\Downloaded_Files.txt echo %%a"
echo !ScriptLine!
SET /A FILE_COUNT+=1
)>>"%ScriptFile%"
:Finish
echo bye>>"%ScriptFile%"
endlocal
All lines output to STDOUT within the two command blocks are redirected into the script file to create.
By the way: SET /S FILE_COUNT=0 resulted on execution in error message
The syntax of the command is incorrect.
The command SET does not support an option /S, just /A for an arithmetic expression and /P for prompting user for value if command extensions are enabled in current command process environment.

How should I print this in a .bat file? [duplicate]

This question already has answers here:
How do escape echo " for storing in a file?
(2 answers)
Closed 6 years ago.
echo ping localhost -n 4 > nul >>TEST.bat
When I print this in a .bat file, for example, the "> nul" disappears. How can i just print "> nul" as a string way?
To esacpe > or < or | just add the carret ^
#echo off
Color 0E
(
echo cls
echo #echo off
echo Color 0B
echo echo Please Wait a while ....
echo ping localhost -n 4 ^> nul
)>TEST.bat
cls
echo.
echo Type any key to read the contents of your new batch file
pause>nul
echo.
Type TEST.bat
pause>nul
cls
echo.
echo Type any key to execute your new batch file ...
pause>nul
TEST.bat

Resources