Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
fkjhr4e8d
- Posts: 8
- Joined: 05 Apr 2022 05:02
#1
Post
by fkjhr4e8d » 05 Apr 2022 05:19
I have a batchfile that will assemble a command line passed on passed parameters.
Code: Select all
REM Parameters:
REM To (required)
REM To (Optional) - NA to indicate empty
REM Subject (required)
REM Message (required)
REM AttachFile1 (optional) - NA to indicate empty
REM AttachFile2 (optional) - NA to indicate empty
SET EmailTo1=%~1
SET EmailTo2=%~2
SET EmailSubject=%~3
SET EmailMessage=%~4
SET EmailFile1=%~5
SET EmailFile2=%~6
if "%EmailTo1%"=="" GOTO MissingParams
if "%EmailSubject%"=="" GOTO MissingParams
if "%EmailMessage%"=="" GOTO MissingParams
REM Check that EmailFile2 is not null. This guarantees that all other parameters are populated
if "%EmailFile2%"=="" GOTO MissingParams
SET tmp-File=d:\temp\sendemail.log
IF NOT EXIST d:\temp\NUL SET tmp-File=c:\temp\sendemail.log
SET ExecBase="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l %tmp-File% -t %EmailTo1% -u "%EmailSubject%" -m "%EmailMessage%"
SET TempStr=
if NOT "%EmailTo2%"=="NA" (
SET TempStr=%ExecBase% -t %EmailTo2%
SET ExecBase=%TempStr%
)
if NOT "%EmailFile1%"=="NA" (
SET TempStr=%ExecBase% -a "%EmailFile1%"
SET ExecBase=%TempStr%
)
if NOT "%EmailFile2%"=="NA" (
SET TempStr=%ExecBase% -a "%EmailFile2%"
SET ExecBase=%TempStr%
)
ECHO %BaseFile%
GOTO :End
I call the code as follows:
Code: Select all
SendFailOver.cmd email@gmail.com email@outlook.com "This is a subject" "this is a message" "C:\Temp\File1.txt" "C:\temp\file2.txt"
The selective concatenation is not taking place.
I tried various approaches but never getting the expected result.
Below is the output I get from executing the code
SET ExecBase="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l d:\temp\sendemail.log -t
email@gmail.com -u "This is a subject" -m "this is a message"
SET TempStr=
if NOT "email@outlook.com" == "NA" (
SET TempStr="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l d:\temp\sendemail.log -t email@gmail.com -u "This is a subject" -m "this is a message" -t email@outlook.com
SET ExecBase=
)
if NOT "C:\Temp\File1.txt" == "NA" (
SET TempStr= -a "C:\Temp\File1.txt"
SET ExecBase="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l d:\temp\sendemail.log -t email@gmail.com -u "This is a subject" -m "this is a message" -t email@outlook.com
)
if NOT "C:\temp\file2.txt" == "NA" (
SET TempStr="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l d:\temp\sendemail.log -t email@gmail.com -u "This is a subject" -m "this is a message" -t email@outlook.com -a "C:\temp\file2.txt"
SET ExecBase= -a "C:\Temp\File1.txt"
)
Thanks
-
fkjhr4e8d
- Posts: 8
- Joined: 05 Apr 2022 05:02
#2
Post
by fkjhr4e8d » 05 Apr 2022 07:23
The problem was related to the evaluation of the variables.
resolved the issue and using
! rather than
%. It controls when a variable is expanded (at the beginning or when it is used). the modified code reads as follows
Code: Select all
SETLOCAL EnableDelayedExpansion
REM Parameters:
REM To (required)
REM To (Optional) - NA to indicate empty
REM Subject (required)
REM Message (required)
REM AttachFile1 (optional) - NA to indicate empty
REM AttachFile2 (optional) - NA to indicate empty
SET EmailTo1=%~1
SET EmailTo2=%~2
SET EmailSubject=%~3
SET EmailMessage=%~4
SET EmailFile1=%~5
SET EmailFile2=%~6
if "%EmailTo1%"=="" GOTO MissingParams
if "%EmailSubject%"=="" GOTO MissingParams
if "%EmailMessage%"=="" GOTO MissingParams
REM Check that EmailFile2 is not null. This guarantees that all other parameters are populated
if "%EmailFile2%"=="" GOTO MissingParams
SET tmp-File=d:\temp\sendemail.log
IF NOT EXIST d:\temp\NUL SET tmp-File=c:\temp\sendemail.log
SET ExecBase="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l %tmp-File% -t %EmailTo1% -u "%EmailSubject%" -m "%EmailMessage%"
if NOT "%EmailTo2%"=="NA" SET ExecBase=!ExecBase! -t %EmailTo2%
if NOT "%EmailFile1%"=="NA" SET ExecBase=!ExecBase! -a "%EmailFile1%"
if NOT "%EmailFile2%"=="NA" SET ExecBase=!ExecBase! -a "%EmailFile2%"
ECHO !ExecBase!
GOTO :End
-
AR Coding
- Posts: 53
- Joined: 02 May 2021 21:16
#3
Post
by AR Coding » 05 Apr 2022 20:04
Hi.
I dont have this "sendEmail.exe" file and i dont know how to use it. But from a simple google search it appears that you are missing the
'-f' option. So im guessing you should add a
somewhere in the line.
-
fkjhr4e8d
- Posts: 8
- Joined: 05 Apr 2022 05:02
#4
Post
by fkjhr4e8d » 06 Apr 2022 00:41
Hi,
I have been using sendemail for over 10 years and it is a nifty free utility that is great when you want a process to send an email. Today you normally need to combine it with a utility that handles the encryption aspects of the communication. I use STunnel for that. -f is an issue only if the provider complains and from my experience google, outlook and others I use don't.
Back to the original topic.
The solution was to enable delayed evaluation of the variables. Without that functionality the variable would be expanded when processed. As it is changing the evaluation needs to be when it is being used. Useing
! rather than
% is another code change
Code: Select all
setLOCAL EnableDelayedExpansion
REM Parameters:
REM To (required)
REM To (Optional) - NA to indicate empty
REM Subject (required)
REM Message (required)
REM AttachFile1 (optional) - NA to indicate empty
REM AttachFile2 (optional) - NA to indicate empty
set EmailTo1=%~1
set EmailTo2=%~2
set EmailSubject=%~3
set EmailMessage=%~4
set EmailFile1=%~5
set EmailFile2=%~6
set InvalidEntry=%~7
if NOT "%InvalidEntry%"=="" GOTO MissingParams
if "%EmailTo1%"=="" GOTO MissingParams
if "%EmailSubject%"=="" GOTO MissingParams
if "%EmailMessage%"=="" GOTO MissingParams
REM Check that EmailFile2 is not null. This guarantees that all other parameters are populated
if "%EmailFile2%"=="" GOTO MissingParams
set tmp-File=d:\temp\sendemail.log
IF NOT EXIST d:\temp\NUL set tmp-File=c:\temp\sendemail.log
set ExecBase="C:\Program Files (x86)\sendEmail\sendEmail.exe" -l %tmp-File% -t %EmailTo1% -u "%EmailSubject%" -m "%EmailMessage%"
if NOT "%EmailTo2%"=="NA" set ExecBase=!ExecBase! -t %EmailTo2%
if NOT "%EmailFile1%"=="NA" set ExecBase=!ExecBase! -a "%EmailFile1%"
if NOT "%EmailFile2%"=="NA" set ExecBase=!ExecBase! -a "%EmailFile2%"
-
fkjhr4e8d
- Posts: 8
- Joined: 05 Apr 2022 05:02
#5
Post
by fkjhr4e8d » 06 Apr 2022 05:55
I know I was not going senile
I just realised that the approval by the admin resulted in the flow not appearing to be logical.