|
Answer» Hi,
I would like to automate a fairly simply email task using a command-line email program that I found, and a "for" loop. Here are the details....
I have a folder "students", and inside of it there are subfolders whose name is the first part of my students' email address. If the subfolder is "johnsmith", then the student's email address is "[emailprotected]".
Furthermore, each subfolder contains exactly one document -- a word document that needs to be emailed to the student. The text of each email will be identical.
An example of the cmd line code that needs to be executed on the subfolder "johnsmith" would be:
Code: [Select]sendEmail -f "[emailprotected]" -t "[emailprotected]" -m "Your graded paper is attached!" -a "<name of file inside subfolder johnsmith>"
So basically the "johnsmith" part of the to email and the name of the attached file would be dynamically grabbed inside the loop before assembling and executing the command like the one above.
Any help WRITING the needed DOS would be greatly appreciated!
THANKS, gmSomething like this should work. Since variables are normally EVALUATED once, before a loop is executed, delayed EXPANSION is called for (variables have ! instead of %)
Just cooked up from memory, not tried. May need small amount of tinkering, tailoring, etc.
Remove ECHO (or duplicate line and remove it from that) when happy - this avoids sending wrong messages and/or becoming a spammer!
Does sendEmail need full path to the -a file?
Quote setlocal enabledelayedexpansion
REM run in students directory
for /f "delims==" %%S in ('dir /b /ad') do ( set student=%%S for /f "delims==" %%D in ('dir /b !student!\*.doc') do set docname=%%D ECHO sendEmail -f "[emailprotected]" -t "[emailprotected]" -m "Your graded paper is attached!" -a "<\!student!\!docname!>" )
Thanks contrex! Exactly what I needed.Here is the tested version... some changes were required.
Quote @echo off setlocal enabledelayedexpansion
REM save and run in "students" directory
REM For each individual student folder, for /f "delims==" %%S in ('dir /b /ad') do (
REM change to that folder and ... cd %%S
REM for each (of one!) file in that folder, REM get full path (drive letter, path, name and extension) for /f "delims==" %%D in ('dir /b *.doc') do set file=%%~dpnxD
REM go back up 1 level to "students" directory REM ready for next time around the loop cd..
REM form command line for sendEmail REM Exclamation mark (special character in batch language) in message body "escaped" with caret ECHO sendEmail -f "[emailprotected]" -t "%%[emailprotected]" -m "Your graded paper is attached^!" -a "!file!" )
Quote S:\Test\students>mailgrades.bat sendEmail -f "[emailprotected]" -t "[emailprotected]" -m "Your graded paper is attached!" -a "S:\Test\students\BillJones\Bill Jones Graded Paper.doc" sendEmail -f "[emailprotected]" -t "[emailprotected]" -m "Your graded paper is attached!" -a "S:\Test\students\JaneBrown\Jane Brown Graded Paper.doc" sendEmail -f "[emailprotected]" -t "[emailprotected]" -m "Your graded paper is attached!" -a "S:\Test\students\JohnSmith\John Smith Graded Paper.doc" sendEmail -f "[emailprotected]" -t "[emailprotected]" -m "Your graded paper is attached!" -a "S:\Test\students\MaryWhite\Mary White Graded Paper.doc"
|