0

I am trying to write a batch file that will pull a file from a directory and email it. That is easy, the problem I am having is that the file that needs to be emailed has either an increasing number or a time/date stamp. Obviously due to the inconsistencies of the time date issue I can change the file names to be numbers that increase. My problem is how to I designate the file either that is the newest in the directory, or the file with the largest number in the file name. I have been searching for a while now and not found anything that has helped.

@echo off
setlocal

set Port=465
set SSL=True
set From="email@blah.com"
set To="email@blah.com"
set Subject="Subject"
set Body="Body"
set SMTPServer="smtp.gmail.com"
set User="username"
set Pass="password"
set fileattach="\\networklocation\filename.jpg"

if "%~7" NEQ "" (
set From="%~1"
set To="%~2"
set Subject="%~3"
set Body="%~4"
set SMTPServer="%~5"
set User="%~6"
set Pass="%~7"
set fileattach="%~8"
)

set "vbsfile=%temp%\email-bat.vbs"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From     = %From%
echo >>"%vbsfile%" objEmail.To       = %To%
echo >>"%vbsfile%" objEmail.Subject  = %Subject%
echo >>"%vbsfile%" objEmail.Textbody = %body%
if exist %fileattach% echo >>"%vbsfile%" objEmail.AddAttachment %fileattach%
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = %SMTPServer%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = %port%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = %user%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendpassword")     = %pass%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = %SSL%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 30
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send

cscript.exe /nologo "%vbsfile%"

echo email sent (if variables were correct)

Over the years this site has helped me a lot and I just wanted to say thank you to the community.

  • You can use the `/o` option of the `dir` command to sort the files by date (newest or oldest first). Then just use the first or last file listed. example: `for /f "delims=" %%A in ('dir /a-d /b /od') do set "newest=%%~fA"` – David Ruhmann Mar 18 '14 at 18:08
  • Thank you David. I guess I am not nearly as smart as I thought I was. I can see how the /o option can arrange the files. I am just lost as to how to incorporate that into the code to make it functional. Again thank you for your offer of help. – user3434325 Mar 18 '14 at 19:13

2 Answers2

0

Here is a function that I wrote to do this. It's similar to David's code but uses /o-d to take the first file found then exit the loop. This will speed it up if there are a lot of files in the directory. It also lets you easily specify if you want latest created, accessed or written.

Call :getlatestfile "C:\files" "w" latest
echo %latest%
exit /b         


:getlatestfile <path> <c/a/w> <return>
setlocal
for /f %%b in ('dir "%~1" /b /o-d /t%~2') do (
set "latest=%%b" & goto :out)
:out
endlocal & set "%~3=%latest%"
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
  • Thanks Matt, I seem to be having trouble incorporating that into whats already there. Am I correct in guessing that would be folder location of the files to search for? I am not sure what to insert as . Please forgive by complete ignorance. – user3434325 Mar 19 '14 at 02:12
  • can be anything. In this example, I used "latest" as the return variable. – Matt Williamson Mar 19 '14 at 10:29
0

Use the line below in place of set fileattach="\\networklocation\filename.jpg"

for /f "delims=" %%a in ('dir "\\networklocation\*.jpg" /b /od') do set fileattach="\\networklocation\%%a"

It selects the newest file (if there are thousands of .jpg files in the folder then expect a short delay, or ask for method B :) )

Change \\networklocation in two places.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • There is an execution delay even with about 20 or so files in the folder. I have no problem with that as I don't expect this to be called that frequently. I very much appreciate your help, and thank you for your time and contribution. – user3434325 Mar 19 '14 at 16:08
  • Thanks. Just to clarify that the batch file does take a short time by itself. If there are many files then it could take an extra few seconds, as the loop takes every filename in turn, and remembers the newest. I'm glad it helps. – foxidrive Mar 20 '14 at 02:46
  • I did some further digging today and found that it doesn't actually do what it's supposed to. I am not sure why, but for some reason it just sends the same image over and over again despite there being a newer image to send. – user3434325 Mar 21 '14 at 16:47
  • I found the solution which was that the batch file was being executed before the file was written. I had to add a TIMEOUT /t 30 /nobreak in order to add enough of a sleep to allow for the file to be written and searchable. With that addition before the outlining of the message I was able to get it to work. Thanks again for your help – user3434325 Mar 21 '14 at 20:30