Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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%"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top