0

I am doing a python project, with the SikuliX feature. I want to make an Automatic Mail sending system, but I import the TO, CC/BCC, and so on.. trough a BAT file, which sends then its data to a txt, python imports the txt and then it uses to do the job. But my problem is that when I leave a variable in Batch empty, it Automatically fills it as 'ECHO is off.' How could I prevent this ? Here's the code:

@echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2,3 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho f0 "-----[]AutoMail System[]-----"
echo.
echo.
call :colorEcho 0f "Fill out the next part:"
echo.
pause
del userdata.txt

echo.
echo.
set /p to="TO: "
echo To: >> userdata.txt
echo %to% >> userdata.txt
set /p ccbcc="CC/BCC:  "
CC/ BCC: >> userdata.txt
%ccbcc% >> userdata.txt
set /p targy="Tárgy:  "
Targy: >> userdata.txt
%targy% >> userdata.txt
set /p szoveg=">->->  "
szoveg: >> userdata.txt
%szoveg% >> userdata.txt
echo.
PAUSE
echo.
echo Starting AutoFill
echo.
PAUSE
start C:\\Users\\gutiw\\Desktop\\Sikuli\\runsikulix.cmd -r C:\\Users\\gutiw\\Desktop\\Sikuli\\AUTOMATION\\AutoMail.sikuli
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

Thanks for helping!

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
nebulator0
  • 85
  • 8
  • what do you want to do instead? echo nothing? – Jean-François Fabre Dec 19 '17 at 16:55
  • 1
    I'm Windows batch file scripting there's no such thing as an empty variable, if there is no content then a variable is undefined. – Compo Dec 19 '17 at 16:57
  • I want to echo nothing, so it would be just empty but only where it says Echo is off – nebulator0 Dec 19 '17 at 16:57
  • @compo So is there a way to make a variable undefined only when there's a specific string in it? (like ECHO is off.) – nebulator0 Dec 19 '17 at 16:59
  • 1
    `echo.%to% >> userdata.txt` You are missing a lot of echo commands as well. – Squashman Dec 19 '17 at 16:59
  • 2
    You should use `Call` instead of `Start` for your `runsikulix.cmd` file. Also although you may require double back slashes for the parameters you certainly shouldn't need them for the path to that script, `Call "C:\Users\gutiw\Desktop\Sikuli\runsikulix.cmd"` or possibly `"%UserProfile%\Desktop\Sikuli\runsikulix.cmd"`. – Compo Dec 19 '17 at 17:06
  • @Squashman You saved me! The porblem was, that I haven't put the . between echo and the variable! Thanks a lot! – nebulator0 Dec 19 '17 at 17:07
  • @Compo There are other parts of the program too, where I use double quotes , but here it wasn't necessary, But the `Call` is not a bad idea ;) – nebulator0 Dec 19 '17 at 17:08
  • 1
    @ÁdámGutási, trying searching StackOverFlow first. I searched `echo is off` and found a couple dozen questions of people who already asked the same thing. – Squashman Dec 19 '17 at 17:10
  • Ádám, whilst I used double quotes as best practice, my example and comment referred to double back slashes. – Compo Dec 19 '17 at 17:11

2 Answers2

1

Use the command line:

>>userdata.txt echo/%to%

Now environment variable to can be not defined and ECHO does nevertheless not output current state of ECHO mode because of forward slash / in command line.

The output redirection is specified on this command line for safety at beginning to make it possible to output with referencing to also the string values 1 to 9 without getting a trailing space written into the file userdata.txt.

The command line echo.%to% >> userdata.txt has two small disadvantages:

  1. Although extremely unlikely, echo. can fail and do something completely different as expected, see DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for details.

  2. The space between %to% and the redirection operator >> is also written as trailing space into the output file userdata.txt.

Example to demonstrate the difference:

del userdata.txt 2>nul
set to=1
>>userdata.txt echo/%to%

Execution of a batch file with the three lines above results in really executing by Windows command interpreter:

del userdata.txt  2>nul
set to=1
echo/11>>userdata.txt

The file userdata.txt contains 1, carriage return and line-feed, i.e. three bytes with the hexadecimal values 31 0D 0A.

A batch file with the three lines

del userdata.txt 2>nul
set to=1
echo.%to% >>userdata.txt

results in really executing by Windows command interpreter

del userdata.txt  2>nul
set to=1
echo.1  1>>userdata.txt

and the file userdata.txt contains 1, a space character, carriage return and line-feed, i.e. four bytes with the hexadecimal values 31 20 0D 0A.

In this case it works also to specify the redirection on right side as usual without a space between environment variable reference %to% and redirection operator >>, i.e. use the command line:

echo/%to%>>userdata.txt

This works because of / after command ECHO. . could be also used if there would not be issue 1 with echo..

See also Why does ECHO command print some extra trailing space into the file?

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

So it looks like it's working now.
The problem was that I haven't used the . between echo and the variables.

So it looks like this after edited:

@echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2,3 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho f0 "-----[]AutoMail System[]-----"
echo.
echo.
call :colorEcho 0f "Fill out the next part"
echo.
pause
del userdata.txt

echo.
echo.
set /p to="TO: "
echo To: >> userdata.txt
echo.%to% >> userdata.txt
set /p ccbcc="CC/BCC:  "
echo.CC/ BCC: >> userdata.txt
echo.%ccbcc% >> userdata.txt
set /p targy="Tárgy:  "
echo.Targy: >> userdata.txt
echo.%targy% >> userdata.txt
set /p szoveg=">->->  "
echo.szoveg: >> userdata.txt
echo.%szoveg% >> userdata.txt
echo.
PAUSE
echo.
echo Starting AutoFill
echo.
PAUSE
start C:\\Users\\gutiw\\Desktop\\Sikuli\\runsikulix.cmd -r C:\\Users\\gutiw\\Desktop\\Sikuli\\AUTOMATION\\AutoMail.sikuli
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
Mofi
  • 46,139
  • 17
  • 80
  • 143
nebulator0
  • 85
  • 8