How to echo output redirection mark (>>) literally? - batch-file

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.

Related

How can I modify the variable result from a FOR-LOOP and output the modified variable?

I want to run a Windows batch FOR loop on a text file and strip spaces from the resulting variable, and then output each modified variable into text file.
I think I will need to use enabledelayedexpansion for this, but I cannot get the proper syntax
Here is an example to reproduce:
(
echo Here is my first line
echo Here is my second line
echo Here is my third line
)>"textfile.txt"
FOR /F "delims=" %%G IN ('TYPE textfile.txt') DO (
(
ECHO #ECHO OFF
ECHO ECHO PLEASE WAIT
ECHO ECHO.
ECHO "%%windir%%\system32\cscript" "%%windir%%\system32\script.vbs" -\\REMOTESERVER\%%G
)>%%G.txt
)
This code works as expected and outputs 3 files:
Here is my first line.txt
Here is my second line.txt
Here is my third line.txt
Here is my first line.txt contains:
#ECHO OFF
ECHO PLEASE WAIT
ECHO.
"%windir%\system32\cscript" "%windir%\system32\script.vbs" \\REMOTESERVER\Here is my first line
Here is my second line.txt contains:
#ECHO OFF
ECHO PLEASE WAIT
ECHO.
"%windir%\system32\cscript" "%windir%\system32\script.vbs" \\REMOTESERVER\Here is my second line
and so on.
I now want to strip the spaces. I want the files to be named:
Hereismyfirstline.txt
Hereismysecondline.txt
Hereismythirdline.txt
and I want them to contain:
#ECHO OFF
ECHO PLEASE WAIT
ECHO.
"%windir%\system32\cscript" "%windir%\system32\script.vbs" \\REMOTESERVER\Hereismyfirstline
and so on.
To accomplish this, I think that I need to use enabledelayedexpansion and add:
SET variable=%%G
SET variable=!variable: =!
But I am unsure how to add that to the code to the FOR loop and convert my % variables to !
#echo off
setlocal enabledelayedexpansion
(
echo Here is my first line
echo Here is my second line
echo Here is my third line
)>"textfile.txt"
FOR /F "delims=" %%G IN ('TYPE textfile.txt') DO (
set "var=%%G"
set "var=!var: =!"
(
ECHO #ECHO OFF
ECHO ECHO PLEASE WAIT
ECHO ECHO.
ECHO "%%windir%%\system32\cscript" "%%windir%%\system32\script.vbs" -\\REMOTESERVER\!var!
)>!var!.txt
)

Why am I getting "ECHO is OFF/ON"?

Simple question: why is ECHO is OFF/ON being printed in a .txt file instead of the users' input? I tried various solutions - none of them worked. Any and all help will be appreciated.
Code
#echo off
:start
del "test.txt"
cls
set /p test_test = "> "
echo %test_test% >> test.txt
for /f "delims=" %%a in (test.txt) do (
set file = %%a
)
echo %file%
pause
The ECHO command with no parameters outputs the status of ECHO, which in this case you've set to off. Because your %test_test% variable is empty, (due to you setting a variable named %test_test %), the ECHO command is being entered with no parameters.
#echo off
:start
cls
set /p "test_test= > "
(echo %test_test%)>test.txt
for /f "delims=" %%a in (test.txt) do (
set "file=%%a"
)
echo %file%
pause
As you may have noted you werre also setting a variable named %file % too.

Batch file out put to both console and text

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 >>.

Test for and remove substring in user input

I want to create a batch file that echoes text that it receives as part of a user-inputted command.
#echo off
Echo execute a command
Set /p command=
if "%command%"=="echo 'some text'" goto echo
::my aim is to make "echo" ignored by System and Only read "'some text'"
:: like "set text_to_echo='some text'
:echo
echo %text_to_echo%
Asking the user for the command first and then asking the user what to echo, like below, is not an option
#echo /p
set /p text=
if "%text%=="echo"
:echo
set /p tex1="Enter text to echo"
echo "%tex1%"
You could use this:
#echo off
Echo execute a command
Set /p "command="
if NOT "%command%"=="%command:echo =%" goto echo
pause
:echo
SET "text_to_echo=%command:~5%"
echo.%text_to_echo%
pause
Note that I also fixed your if, and put a pause after your if so you know whether it goes to :echo or not.
You want to split your Input into a first word (token) and the rest. You can do this with a for:
#echo off
Echo execute a command
Set /p "commandstring=# "
for /f "tokens=1,* delims= " %%a in ("%commandstring%") do (
set "command=%%a"
set "params=%%b"
)
if /i "%command%"=="echo" goto :_echo
...
:_echo
echo %params%
REM also possible (at least for "echo", but I guess, that's just an example):
%command% %params%

Batch command to take only first line from input

I'm looking for a DOS batch program that takes a file:
First input line
Second input line
Third input line...
And outputs "First input line"
you can just get the first line like this
set /p firstline=<file
echo %firstline%
Assuming you mean the Windows cmd interpreter (I'd be surprised if you really were still using DOS), the following script will do what you want:
#echo off
setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (infile.txt) do (
if !first!==1 echo %%i
set first=0
)
endlocal
With an input file of infile.txt as:
line 1
line 2
line 3
this will output:
line 1
This will still process all the lines, it just won't print those beyond line 1. If you want to actually stop processing, use something like:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in (infile.txt) do (
echo %%i
goto :endfor
)
:endfor
endlocal
Or you could just go get your hands on Cygwin or GnuWin32 and use the head program. That's what I'd do. But, if that's not an option (some workplaces don't allow it), you can create a similar cmd file in Windows itself as follows (winhead.cmd):
#echo off
setlocal enableextensions enabledelayedexpansion
if x%1x==xx goto :usage
if x%2x==xx goto :usage
set /a "linenum = 0"
for /f "usebackq delims=" %%i in (%1) do (
if !linenum! geq %2 goto :break1
echo %%i
set /a "linenum = linenum + 1"
)
:break1
endlocal
goto :finish
:usage
echo.winhead ^<file^> ^<numlines^>
echo. ^<file^>
echo. is the file to process
echo. (surround with double quotes if it contains spaces).
echo. ^<numlines^>
echo. is the number of lines to print from file start.
goto :finish
:finish
endlocal
why not use the more +1 command via a pipe?
e.g.
type something | more +1

Resources