Batch Script design help - need advice

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Batch Script design help - need advice

#1 Post by SIMMS7400 » 12 Oct 2017 01:42

Good Morning Everyone -

I just wanted to pop in to get your opinion on a batch script design and what you think would be best.

I'm not sure how familiar you are with Oracle products, but there is a utility (FR batch) that comes with the product that allows Financial Report bursting based on parameters set in the application. However, this particular client also receives a Financial Report from another non-Oracle system. Therefore, I need to create a batch script that search for a particular report, and then emails that report to a person or persons.

Reports are in *.pdf.

For instance : GL_Report_001.pdf needs to be picked up and emailed to StoreManager001@client.com and so on and so forth.

Does anyone have an recommendation on how to set up a batch script efficiently for this? It seems as though I'll need to set up many if statements. Or perhaps I can manage the Store and email specifics in a text file and then read that into the batch via loop?

I'm not sure, so I was hoping to get some advice from you guys.

Thank you!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch Script design help - need advice

#2 Post by SIMMS7400 » 12 Oct 2017 16:57

Hi Team -

So I figured out I'm going to manage this information in a csv file.

Column 1 will be the list of emails seperated by ";" and columns 2 & 3 will be the specific report name.

Columns will not change and columns 2 and 3 will always be reports.

Hardcoded, the following code is working like a charm:

Code: Select all

:EMAIL
SET "SW_MAIL_EXE=%UTILPATH%SwithMail\SwithMail.exe"
SET "SW_FROM_EMAIL=srvc-oracle@client.com"
SET "SW_PW=Password123!"
SET "SW_SERVER=west.XXXXX29.serverdata.net"
SET "SW_PORT=587"
SET SW_SUB="Client : Finance Report Distribution"
SET SW_BODY=%UTILPATH%SwithMail\Body_Text\FR_DIST_TXT.txt

SET "SW_TO_EMAIL=jdoe@client.com;jsmith@client.com"

CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%" /server "%SW_SERVER%" /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub %SW_SUB% /btxt "%SW_BODY%"^
   /a "%RPT_PATH%\Oracle\Oracle_Report_001.pdf|%RPT_PATH%\Epicor\Epicor_Report_001.pdf"

GOTO :EOF


However, when I introduce (2) for loops to dynamically extract the information and pass to the email utility, I loose the 2nd email from !LIST! and the exclmation mark does not display.

Ultimately, I'd like the script to operate like this:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "tokens=1 delims=," %%A in ( Test.csv ) DO (
   SET "LIST=%%A"
   FOR /F "tokens=2-3 delims=," %%B in ( Test.csv ) DO (
      SET "RPT1=%%B"
      SET "RPT2=%%C"
   )
CALL :EMAIL !LIST! !RPT1! !RPT2!
)

GOTO :EOF

:EMAIL

SET "SW_MAIL_EXE=%UTILPATH%SwithMail\SwithMail.exe"
SET "SW_FROM_EMAIL=srvc-oracle@client.com"
SET "SW_PW=Password123!"
SET "SW_SERVER=west.XXXXX29.serverdata.net"
SET "SW_PORT=587"
SET SW_SUB="Client : Finance Report Distribution"
SET SW_BODY=%UTILPATH%SwithMail\Body_Text\FR_DIST_TXT.txt

SET "SW_TO_EMAIL=%1"

CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%" /server "%SW_SERVER%" /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub %SW_SUB% /btxt "%SW_BODY%"^
   /a "%RPT_PATH%\Oracle\%2.pdf|%RPT_PATH%\Epicor\%3.pdf"

GOTO :EOF



Here is a sample of what the source file will be:

Code: Select all

jdoe@client.com;jsmith@client.com,Oracle_Report_001,Epicor_Report_001


I'm sure it's very simple but can someone help me adjust so the entire list is passed to the function and the ! mark works?

Thank you!

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch Script design help - need advice

#3 Post by Compo » 13 Oct 2017 06:09

Considering you've been provided with a lot of code on these forums, I'm disappointed that you appear not to have grasped the basics yet

Code: Select all

@Echo Off
For /F "UseBackQ Tokens=1-3 Delims=," %%A In ("Test.csv"
) Do Call :eMail "%%A" "%%B" "%%C"
Pause
GoTo :EOF

:eMail

Set "UTILPATH=C:\Utilities"
Set "RPT_PATH=C:\Reports"

Set "SW_MAIL_EXE=%UTILPATH%\SwithMail\SwithMail.exe"
Set "SW_FROM_EMAIL=srvc-oracle@client.com"
Set "SW_PW=Password123!"
Set "SW_SERVER=west.XXXXX29.serverdata.net"
Set "SW_PORT=587"
Set "SW_TO_EMAIL=%~1"
Set "SW_SUB=Client : Finance Report Distribution"
Set "SW_BODY=%UTILPATH%\SwithMail\Body_Text\FR_DIST_TXT.txt"

Call "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%"^
 /server "%SW_SERVER%" /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%"^
 /btxt "%SW_BODY%" /a "%RPT_PATH%\Oracle\%~2.pdf|%RPT_PATH%\Epicor\%~3.pdf"
GoTo :EOF
I have added the code to Set both %UTILPATH% and %RPT_PATH% as they weren't showing in your example. If you have set them earlier in the code then you can safely delete lines 8-11 inclusive, first making sure that yours do not end with trailing backslashes.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch Script design help - need advice

#4 Post by SIMMS7400 » 13 Oct 2017 12:45

Hi Compo -

Thank you so much!! That worked like a charm!

However - I have some new requirements. There will be more column inclusive of reports, as shown below. Store Managers will roll into a district manager.

Here is my source file:

Code: Select all

DistrictManager1@client.com,Epicor_Report_001,Epicor_Report_002,Epicor_Report_003,Epicor_Report_004,Epicor_Report_005
StoreManager1@client.com,Epicor_Report_001,,,,
StoreManager2@client.com,Epicor_Report_002,,,,
StoreManager3@client.com,Epicor_Report_003,,,,
StoreManager4@client.com,Epicor_Report_004,,,,
StoreManager5@client.com,Epicor_Report_005,,,,
DistrictManager2@client.com,Epicor_Report_006,Epicor_Report_007,Epicor_Report_008,Epicor_Report_009,Epicor_Report_010
StoreManager11@client.com,Epicor_Report_006,,,,
StoreManager22@client.com,Epicor_Report_007,,,,
StoreManager33@client.com,Epicor_Report_008,,,,
StoreManager44@client.com,Epicor_Report_009,,,,
StoreManager55@client.com,Epicor_Report_010,,,,


There could however be more than 5 reports, but not much more. Regardless, keeping it dynamic would be key.

Is there a way to capture this requirement dynamically and pass to the email utility?

Also, what I'm going to is pushd to the folder containing the attachments, so I dont need to provide a path. However, I will need to append ".pdf" onto the end of each file name. Is this possible?

So I'm thnking something like this:

Code: Select all

@ECHO OFF

FOR /F "USEBACKQ Tokens=1-15 Delims=," %%A IN ("Test2.csv") DO (

   CALL :EMAIL "%%A" "%%B|%%C|%%D|%%E|%%F|%%G|%%H|%%I|%%J|%%K|"
)
pause
GOTO :EOF

:EMAIL

SET TEST=%2

ECHO %T:|=.pdf|%

SET "RPT_PATH=C:\Hyperion_Batch\Files\Fin_Rpts\

SET "SW_MAIL_EXE=%UTILPATH%SwithMail\SwithMail.exe"
SET "SW_FROM_EMAIL=srvc-oracle@client.com"
SET "SW_PW=Password123!"
SET "SW_SERVER=west.XXXXX29.serverdata.net"
SET "SW_PORT=587"
SET SW_SUB="Client : Finance Report Distribution"
SET SW_BODY=%UTILPATH%SwithMail\Body_Text\FR_DIST_TXT.txt

SET "SW_TO_EMAIL=%~1"
SET "SW_ATTACH=%2"

PUSHD "%RPT_PATH%"
CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%"^
 /server "%SW_SERVER%" /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%"^
 /btxt "%SW_BODY%" /a "%SW_ATTACH%"
POPD
GOTO :EOF


However, for the columns not populated, it's trying to attach |.pdf| etc. Is there a way to remove that 'excess'?

Thank you!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch Script design help - need advice

#5 Post by Squashman » 13 Oct 2017 13:34

This will never work. Apparently you forgot the pipe is a special character.

Code: Select all

ECHO %T:|=.pdf|%

I assume you mean to use the SET command and you would have to use quotes to preserve the variable.

Code: Select all

SET "TEST=%TEST:|=.pdf|%"

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch Script design help - need advice

#6 Post by Squashman » 13 Oct 2017 14:11

Is this what you are trying to do with your PDF attachments.

Code: Select all

@ECHO OFF

FOR /F "USEBACKQ Tokens=1-15 Delims=," %%A IN ("test2.csv") DO (
   CALL :EMAIL "%%A" "%%B|%%C|%%D|%%E|%%F|%%G|%%H|%%I|%%J|%%K|"
)
pause
GOTO :EOF

:EMAIL
SET "SW_TO_EMAIL=%~1"
SET "sw_attach=%~2"
:loop
IF "%sw_attach:~-2%"=="||" (
   SET "sw_attach=%sw_attach:~0,-1%"
   GOTO loop
)
set "sw_attach=%sw_attach:|=.pdf|%"
echo "%sw_attach%"

Output

Code: Select all

"Epicor_Report_001.pdf|Epicor_Report_002.pdf|Epicor_Report_003.pdf|Epicor_Report_004.pdf|Epicor_Report_005.pdf|"
"Epicor_Report_001.pdf|"
"Epicor_Report_002.pdf|"
"Epicor_Report_003.pdf|"
"Epicor_Report_004.pdf|"
"Epicor_Report_005.pdf|"
"Epicor_Report_006.pdf|Epicor_Report_007.pdf|Epicor_Report_008.pdf|Epicor_Report_009.pdf|Epicor_Report_010.pdf|"
"Epicor_Report_006.pdf|"
"Epicor_Report_007.pdf|"
"Epicor_Report_008.pdf|"
"Epicor_Report_009.pdf|"
"Epicor_Report_010.pdf|"

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch Script design help - need advice

#7 Post by SIMMS7400 » 13 Oct 2017 14:31

Hi Squash -

Wow thank you!!!!

So that's almost it! For the reports where there's only one, I don't need the "|". Is that dooable to remove it from those instances?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch Script design help - need advice

#8 Post by Squashman » 13 Oct 2017 14:45

SIMMS7400 wrote:Hi Squash -

Wow thank you!!!!

So that's almost it! For the reports where there's only one, I don't need the "|". Is that dooable to remove it from those instances?

Seriously????
You do not know how to remove the last character from a string. I actually have it in the code I am using above.

Here is another option for removing the duplicate pipes.

Code: Select all

:EMAIL
setlocal enableDelayedExpansion
SET "sw_attach=%~2"
set "pipes=||||||||"
for /L %%G in (8,-1,2) do for /f %%S in ("!pipes:~0,%%G!") do set "sw_attach=!sw_attach:%%S=|!"
set "sw_attach=%sw_attach:|=.pdf|%"
echo "%sw_attach%"

Gets the same output with your trailing pipe but you should be able to figure out how to remove it.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch Script design help - need advice

#9 Post by SIMMS7400 » 13 Oct 2017 19:25

Hi Squash -

I'm sorry - you're 100% right. I do know how to remove the last chracter. I read your post too quick and misinterpreted it.

It's working as expected - I will post the final code tomorrow.

Thank you so much!!!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch Script design help - need advice

#10 Post by SIMMS7400 » 16 Oct 2017 07:05

Hi Squash -

Attached is my final script and it's working flawlessly.

I was wondering one thing - if there is ever a need to handle more than 26 tokens, what is the best way to do that?

Code: Select all

@ECHO OFF
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: FIN_RPT_BURSTING.cmd                                       --::
::                                                                           --::
::-- Description: Bursts Epicor Financial Reports                            --::
::--                                                       --::
::                                                         --::
::--  Calls:      _env.cmd                                                   --::
::--  Called By:  Not Applicable                                             --::
::                                                                --::
::-- Parameters:  Call _env.cmd to get variable to determine:                --::
::                                                                --::
::--              1. Path variables                                          --::
::--              2. User ID(s)                                              --::
::--              3. Logon(s)                                                --::
::--              4. Password(s)                                    --::
::--              5. Server(s)                                         --::
::--              6. Application(s)                                      --::
::--           7. Database(s)                                       --::
::--           8. etc                                        --::
::                                                         --::
::-- Author:     Name (Client)                                          --::
::-- Date:         08/17/2017                                                  --::
::                                                         --::
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Set Working Directory as Script Path --::

CD /D %~dp0

::-- Call Environment File --::

CALL _env.cmd

::-- Prep Process Output Variables --::

CALL :PROC_PREP

::-- Set Process Output Variables --::

SET "PLOGPATH=%A%%B%%C%_Logs\"
SET "PERRORPATH=%A%%B%%C%_Errors\"

::-- Establish INTRAPATH variables & directories --::

SET "INTRAPATH=%LOGPATH%%PLOGPATH%"
SET "ERRORINTRAPATH=%ERRORPATH%%PERRORPATH%"

::-- Parse DOS TIME variable in savable format --::

FOR /F "tokens=1-2 delims=/:" %%A IN ("%TIME%")   DO ( SET "TIMESTAMP=%%A%%B")
FOR /F "tokens=* delims= " %%C IN ("%TIMESTAMP%") DO ( SET "TIMESTAMP=%%C")
SET "DATETIME=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%TIMESTAMP%"

::-- Establish Log & Error files --::

SET "LOGFILE=%INTRAPATH%%DATETIME%_%~n0.log"
SET "ERRORFILE=%ERRORINTRAPATH%%DATETIME%_%~n0.err"

:::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Intra-script variable declaration - Begin --::
:::::::::::::::::::::::::::::::::::::::::::::::::::

SET "ERR="
SET /A "LCNT=0"
SET "CNTRL_FILE=FR_Burst_List.csv"

::-- PBCS_URL & Target_PATH set in '_env.cmd' --::
::-- If Target_PATH needs to be changed, modify it below and NOT in _env.cmd file --::

SET "PBCS_URL=%PBCS_URL_LMIT%"
SET "Target_PATH=%BACKUPPATH%%ENV%"

SET "PING_DELAY=5"
:::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Intra-script variable declaration - End   --::
:::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Call function to extract domain from %PBCS_URL% --::

CALL :PROC_PREP

::-- Begin Script Processing --::

>>%LOGFILE% (
ECHO ********************************************************
ECHO %~n0 Starting at %TIME%                               
ECHO ********************************************************
ECHO.
ECHO ********************************************************
ECHO Validate Financial Report Existence                                         
ECHO ********************************************************
ECHO.
)

CALL :RPT_VAL

IF DEFINED ERR GOTO AbnormalExit

>>%LOGFILE% (
ECHO ********************************************************
ECHO Execute Financial Report Burst Process                                         
ECHO ********************************************************
ECHO.
)

FOR /F "USEBACKQ Tokens=1-15 Delims=," %%A IN ("%CNTRL_FILE%") DO (
   CALL :EMAIL "%%A" "%%B|%%C|%%D|%%E|%%F|%%G|%%H|%%I|%%J|%%K|"
)
ECHO.>>%LOGFILE%

>>%LOGFILE% (
ECHO ********************************************************
ECHO Execute Financial Report Archive Process                                         
ECHO ********************************************************
ECHO.
)

CALL :ARCHIVE_FR

IF DEFINED ERR GOTO AbnormalExit

:NormalExit
CLS
>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO %~n0 - Completed Successfully                           
ECHO ********************************************************

ECHO ********************************************************
ECHO Normal Exit - %~nx0                                   
ECHO ********************************************************
DATE /t                                                     
TIME /t                                                     
)

CALL :ARCHIVE
EXIT /B 0

:AbnormalExit
FOR %%L IN ( "%LOGFILE%" "%ERRORFILE%" ) DO (
   >>%%L (
   ECHO.
   ECHO ********************************************************
   ECHO %~n0 results in Abnormal Exit                           
   ECHO ********************************************************
   ECHO Please check log file for errors
   ECHO.
   ECHO ********************************************************
   ECHO Abnormal Exit - %~nx0                                   
   ECHO ********************************************************
   DATE /t                                                     
   TIME /t                                                     
   )
)

CALL :ARCHIVE
EXIT /B 1

:::::::::::::::::::::::::::::::::::::::::::::::::
::-- F U N C T I O N S   B E L O W   H E R E --::
:::::::::::::::::::::::::::::::::::::::::::::::::

:ARCHIVE

::-- Archive Log Files in \YYYYDDMM Folder --::
       
FOR %%F IN ("%INTRAPATH%*") DO (
   IF NOT EXIST "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )
IF EXIST "%%~F" MOVE "%%~F" "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)

::-- Archive Error Files in \YYYYDDMM Folder --::
::-- RD then used to remove empty error subdirectories --::

FOR %%F IN ("%ERRORINTRAPATH%*") DO (
   IF NOT EXIST "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )
IF EXIST "%%~F" MOVE "%%~F" "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)
RD "%ERRORINTRAPATH:~0,-1%">nul 2>&1

GOTO :EOF

:ARCHIVE_FR

IF NOT EXIST "%ARCHIVEPATH%\FR_RPTS\%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
   MKDIR "%ARCHIVEPATH%\FR_RPTS\%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)

IF EXIST "%RPT_PATH%*" MOVE /Y "%RPT_PATH%*" "%ARCHIVEPATH%\FR_RPTS\%date:~-4,4%_%date:~-10,2%%date:~-7,2%" && (
   ECHO Financial Report Archive Process  : Successful >>%LOGFILE%
   ) || (
   ECHO Financial Report Archive Process  : Failed >>%LOGFILE%
   SET "ERR=T"
)

GOTO :EOF
   
:EMAIL
SET /A "LCNT+=1"

SET "SW_SUB=Client : Finance Report Distribution"
SET "SW_BODY=%UTILPATH%SwithMail\Body_Text\FR_DIST_TXT.txt"
SET "SW_TO_EMAIL=%~1"
SET "SW_ATTACH=%~2"

:LOOP
IF "%SW_ATTACH:~-2%"=="||" (
   SET "SW_ATTACH=%SW_ATTACH:~0,-1%"
   GOTO LOOP
)
SET "SW_ATTACH=%SW_ATTACH:|=.pdf|%"

PUSHD "%RPT_PATH%"

CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%"^
   /server "%SW_SERVER%" /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%"^
   /btxt "%SW_BODY%" /a "%SW_ATTACH:~0,-1%" && (
   
      ECHO Execute Control File ^- Line %LCNT% : Successful >>%LOGFILE%
      ) || (
      ECHO Execute Control File ^- Line %LCNT% : Failed >>%LOGFILE%
      SET "ERR=T"
)

POPD
GOTO :EOF

:PROC_PREP

::-- This function is leveraged twice during this script execution --::
::-- The actions are as follows:
   ::-- Parses script name and sets each string as a variable  --::
      ::-- i.e. <STRING>_<STRING>_<STRING>_<STRING> = <VARIABLE>_<VARIABLE>_<VARIABLE>_<VARIABLE> --::
      ::-- The second string is ALWAYS the environment abbreviation --::
      ::-- Net new scripts should adhere to the following naming convention : PBCS_<ENVIRONMENT>_<YOURCHOICE>.cmd --::
      ::-- Script name must be a minimum 3 strings long and strings 3+ can be set to whatever the developer chooses --::
   ::-- Extracts the domain from %PBCS_URL% and sets it as a variable --::
      ::-- i.e. https://planning-<DOMAIN>.pbcs.us6.oraclecloud.com --::
   ::-- Establishes relevant process subdirectories i.e. Log, Error, Export, Backup etc. --::
      ::-- i.e. C:\Hyperion_Batch\Logs\PBCS_TEST_Backup_Logs --::
      
::-- As this script is leveraged twice, 'if' statements are established in each command below --::
::-- This prevents script failure as there are some variables that do not get defined until the 2nd pass --::
::-- For instance, %PBCS_URL% is set only AFTER the first call to this function --::
::-- Therefore, the domain parsing piece code will not be executed during the first call since the variable is not yet defined --::

::-- Parse Script Name --::
FOR /F "tokens=1-2* delims=_" %%A IN ("%~n0") DO (
   IF ["%%A"] NEQ [""] SET "A=%%A"
   IF ["%%B"] NEQ [""] SET "B=_%%B" & SET "ENV=%%B"
   IF ["%%C"] NEQ [""] SET "C=_%%C"
)

::-- Parse Domain String --::
IF ["%PBCS_URL%"] NEQ [""] FOR /F "delims=." %%A in ("%PBCS_URL%") DO SET "DOMAIN=%%A"
   SET "DOMAIN=%DOMAIN:-=" & SET "DOMAIN=%"
   SET "PBCS_DOMAIN=%DOMAIN%"

::-- Establish process directories --::
IF ["%INTRAPATH%"] NEQ [""] FOR %%F IN ( "%INTRAPATH%" "%ERRORINTRAPATH%" "%~1" ) DO (
   IF ["%%~F"] NEQ [""] IF NOT EXIST "%%~F" MKDIR "%%~F"
)

GOTO :EOF

:RPT_VAL

SET "SW_SUB=Warning! Finance Report Distribution Error"
SET "SW_BODY=%UTILPATH%SwithMail\Body_Text\FR_RPT_VAL.txt"
SET "SW_TO_EMAIL=%SW_ERR_DIST%"

>nul 2>nul dir /a-d "%RPT_PATH%*.*" && ( ECHO Financial Reports exist >nul 2>nul  ) || (

   CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%"   /server "%SW_SERVER%"^
   /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%"   /btxt "%SW_BODY%" /Priority "High"
   
   ECHO Warning! No Financial Reports exist - unable to proceed >>%LOGFILE%
   SET "ERR=T"
)

GOTO :EOF

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch Script design help - need advice

#11 Post by Squashman » 16 Oct 2017 07:54

SIMMS7400 wrote:I was wondering one thing - if there is ever a need to handle more than 26 tokens, what is the best way to do that?

Search the forums. Has been talked about extensively over the years.
Also lots of information out on the Internet about it.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch Script design help - need advice

#12 Post by SIMMS7400 » 18 Oct 2017 09:35

Well - it was easier than I thought. Technically, I only needed (2) tokens to begin with.

Here are my edits:

Code: Select all

FOR /F "USEBACKQ Tokens=1* Delims=," %%A IN ("%CNTRL_FILE%") DO (

   CALL :EMAIL "%%A" "%%B|"
)


Code: Select all

:EMAIL
SET "RPT=%~2"
SET /A "LCNT+=1"

SET "SW_SUB=Bojangles : Finance Report Distribution"
SET "SW_BODY=%UTILPATH%SwithMail\Body_Text\FR_DIST_TXT.txt"
SET "SW_TO_EMAIL=%~1"
SET "SW_ATTACH=%RPT:,=|%"

:LOOP
IF "%SW_ATTACH:~-2%"=="||" (
   SET "SW_ATTACH=%SW_ATTACH:~0,-1%"
   GOTO LOOP
)
SET "SW_ATTACH=%SW_ATTACH:|=.pdf|%"

PUSHD "%RPT_PATH%"

echo CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%"^
   /server "%SW_SERVER%" /p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%"^
   /btxt "%SW_BODY%" /a "%SW_ATTACH:~0,-1%" && (
   
      ECHO Execute Control File ^- Line %LCNT% : Successful >>%LOGFILE%
      pause
      ) || (
      ECHO Execute Control File ^- Line %LCNT% : Failed >>%LOGFILE%
      SET "ERR=T"
)

POPD
GOTO :EOF


Everything checks out - I can have essentially have as many report attachments as I want.

Is that the method you would have chosen? Thanks Squash!

Post Reply