0

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.

  • 2
    [this](https://stackoverflow.com/a/56774429/2152082) should be enlightening. – Stephan Jul 01 '19 at 08:04
  • 2
    You need to either use, `!EC1!`, `!EC2!`, `!EC3!` and `!EC4!` not `%EC1%`, `%EC2%`, `%EC3%` and `%EC4%`, because they're both being defined and used within the same block, _(in this case an `If` block)_, or you could change the order of your `If`/`Else`, i.e. `If %cnt% == 0 (Echo No reports to email & Pause & GoTo :EOF)` first, then simply un-nest the `if NOT %cnt%` block. – Compo Jul 01 '19 at 08:54
  • Thanks Stephan! According to the link mine does set it correctly, as they print correct when I add `set EC` afterwards, but when I then use `echo 1. %EC1%` I only get a `1.`, not the variable. What am I doing wrong? – Donné Kruger Jul 01 '19 at 09:07
  • Thank you Compo!!! That solved my problem!!! ... And answered my previous question to Stephen! – Donné Kruger Jul 01 '19 at 09:09
  • 1
    Assuming the `L` in `for %%f in (C:\LReports\C*.csv) do (` is a typo, you could replace the 6 A..F loops in a nested `for %%L in (A B C D E F) do for %%f in (C:\Reports\%%L*.csv) do set /a "cnt+=1"&set R%%L="%%f"` –  Jul 01 '19 at 13:48

0 Answers0