0

每天凌晨 1 点到凌晨 1:15 之间,有两个文件会上传到 D:\CW-Data\edw 位置。我的要求是在收到两个文件时自动向我们发送带有文件名的电子邮件。我尝试编写一个通过到达的最后一个文件的脚本,但我没有得到预期的结果。请帮帮我:我的脚本是

set srcDir=D:\Mitul\Quantum AWR Report_23Apr_3am_2pm
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
echo %lastmod%
4

2 回答 2

1

这是用于发送电子邮件的通用脚本。

https://groups.google.com/forum/?fromgroups=#!msg/alt.msdos.batch.nt/l_8K11YzS0A/WfbVBoJe-l8J的原始线程

:: Allows ssl and port 465



:: email.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Tom Lavedas, Mod 2.14 to eliminate temporary VBS script file
:: Works with IE5+ (and maybe earlier versions as well)
 @echo off
  setlocal

 :Use command line arguments, if supplied
  if [%1] EQU [] goto :Continue
    set "Arg=%1"
    set "Arg=%Arg::==%"
    set "Arg=%Arg:/=%"
    set "%Arg%"
    shift
    goto :Use 
 :Continue

:: defaults 
  if not defined From       set "From=userid@gmail.com"
  if not defined To         set "To=you@somemail.com"
  if not defined Subj       set Subj="email test   %date% %time%"
  if not defined Body       set Body="Did it work? %date% %time%"
  if not defined Serv       set "Serv=smtp.gmail.com"
  if not defined Auth       set "Auth=userid@gmail.com"
  if not defined Pass       set "Pass=password"
  if not defined fileattach set "fileattach="
  if not defined Port       set "Port=465"
  if not defined SSL        set "SSL=True"
  if not defined Timeout    set "Timeout=25"

  call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass% %fileattach%
  if %errorlevel% NEQ 0 echo Error: %ErrorLevel%
  if %0==%~f0 pause
  exit /b %ErrorLevel%

 :send

  set "cdoSchema=http://schemas.microsoft.com/cdo/configuration"
  set arguments="%~1+%~2+%~3+%~4+%~5+%~6+%~7+%~f8"

  set VBS=resizeTo 1,1:moveTo 1, 3000:
  set VBS=%VBS%set ws=createobject("wscript.shell"):
  set VBS=%VBS%with createobject("scripting.filesystemobject")
  set VBS=%VBS%Execute ws.ExpandEnvironmentStrings(
  set VBS=%VBS%.GetStandardStream(0).readALL):end with:
  set "VBS=about:<script type=text/vbs>%VBS%:close</script>"

  set "Match=VBScript start"
  for /f "delims=[]" %%N in (
    'find /n "### %Match% ###" ^<"%~f0"'
  ) do set "N=%%N"

  for /f "tokens=1,2 delims=#" %%A in (
    'more +%N% "%~f0" ^| mshta.exe "%VBS%"'
  ) do @echo %%B & exit /b %%A
  exit /b 1 % Error in previous statement %

' ### VBScript start ###
 set StdOut=.GetStandardStream(1)
 args = Split(%arguments%, "+")
 with CreateObject("CDO.Message")
   .From     = args(0)
   .To       = args(1)
   .Subject  = args(2)
   .Textbody = args(3)
  if args(7) <> "" then .AddAttachment args(7)
   with .Configuration.Fields
     .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
     .Item ("%cdoSchema%/smtpserver")       = args(4)
     .Item ("%cdoSchema%/smtpserverport")   = %Port%
     .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
     .Item ("%cdoSchema%/sendusername")     = args(5)
     .Item ("%cdoSchema%/sendpassword")     = args(6)
     .Item ("%cdoSchema%/smtpusessl")       = %SSL%
     .Item ("%cdoSchema%/smtpconnectiontimeout") = %Timeout%
     .Update 
   end with ' Configuration.Fields
   On Error Resume Next
   .Send
 end with ' CDO.Message
 if Err.Number = 0 then
   sRes = "0#Mail sent without error"
 else
   sRes = Hex(Err.Number) + "#" + Err.Description
 end if
 stdout.writeline sRes
' ### VBScript end ### - Must be the end of the batch file
于 2013-05-01T16:10:14.553 回答
0
@echo off
setlocal
set srcDir=D:\CW-Data\edw
set file1=whateverthenameofthefirstfileis
set file2=whateverthenameofthesecondfileis
:loop
set /a count=0
for /f %%a in ('dir /b "%srcdir%\%file1%" "%srcdir%\%file2%" 2^>NUL') do set /a count+=1
IF NOT %count%==2 timeout /t 5 >nul&GOTO loop 
echo send the email

这将等待两个文件到达。如果缺少任何一个,请超时 5 秒,然后重试。

没有足够的详细信息来提供更多信息。


附录:

如果您的 windows 版本不包含timeout,请尝试

IF NOT %count%==2 ping -n 5 127.0.0.1 >nul&GOTO loop 
于 2013-05-01T15:41:02.197 回答