I have a batch file where I need to read lines from a txt file (email_config.txt) which contains email settings that I then use in the next line to send an email with a list of attachments.
Within the for loop I have a counter which I use in the variable name (eg EC1 = email addresss, EC2 = email username, EC3 = email password, EC4 = server). The counter works correctly, but I am not able to get the variable to set.
I found a possible solution to assigning each line to a variable, and it worked, but I have since changed something in my code and now it doesn't.
@echo off
set cnt=0
for %%f in (C:\Reports\A*.csv) do (
set /a cnt += 1
set RA="%%f"
)
for %%f in (C:\Reports\B*.csv) do (
set /a cnt += 1
set RB="%%f"
)
for %%f in (C:\LReports\C*.csv) do (
set /a cnt += 1
set RC="%%f"
)
for %%f in (C:\Reports\D*.csv) do (
set /a cnt += 1
set RD="%%f"
)
for %%f in (C:\Reports\E*.csv) do (
set /a cnt += 1
set RE="%%f"
)
for %%f in (C:\Reports\F*.csv) do (
set /a cnt += 1
set RF="%%f"
)
if NOT %cnt% == 0 (
copy C:\Reports\*.CSV C:\Reports\Backup\*.csv
echo ... Emailing %cnt% reports
setlocal EnableDelayedExpansion
set N=0
for /f "tokens=2 delims==" %%x in (C:\Reports\Task\email_config.txt) do (
set /a N+=1
set "EC!N!=%%x"
echo !N! %%x
)
C:\Reports\SendEmail\sendEmail.exe -f %EC1% -t me@myemail.co.za -s %EC4% -xu %EC2% -xp %EC3% -u "Reports" -m "Attached are the available reports" -a %RA% %RB% %RC% %RD% %RE% %RF%
del /f C:\Reports\*.csv
)
if %cnt% == 0 (
echo No reports to email
)
echo Emailing reports complete
pause
exit
My script is reading the email_config.txt correctly, but it is not assigning it to a variable.
I also tried including an additional if inside the loop and assigning to given variables but this didnt work either eg
if !N! == 1 ( set "EC1=%%x" )
I don't know if I have typed something wrong, but would greatly appreciate some help, or a different solution entirely.