我有一个初学者的问题。我有一台收集数据的远程计算机(业余的元理论)。数据每天存储在文本文件中。每个文件都以其日期命名:28_02-2014.txt,01_03_2014.txt等。
任何人都可以帮我创建一个批处理文件,它会以某种方式通过电子邮件自动发送这样的文本文件。即使是从所有txt文件创建zip文件的选项也可以。我可以每月删除一次文件夹,所以即使发送一个包含31个文本文件的zip文件也不会影响带宽(可能每月300kb)。
到目前为止我的问题:
Windows 7
拜托,有人可以帮我吗?无论哪种解决方案更简单:从特定文件夹发送最新文件或将整个文件夹压缩到文件中并发送。
答案 0 :(得分:2)
使用WMIC的日期例程的XP Pro及更高版本:
在此行set fileattach="d:\myfolder\%datestamp%.txt"
中更改文件夹,并将日期戳设置为dd_mm-yyyy
格式,以便文件名适用于您。
文本文件将被附加但不会压缩。
在set语句块中更改server settings
和passwords
以适合您,并对其进行测试。
如果适合您,您可以按日计划安排批处理文件。
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%DD%_%MM%-%YYYY%"
:: email.bat :::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
:: use these settings to send from a gmail account
:: set port=465 and set SSL=True
:: use these settings for standard email SMTP port and no encryption
:: set port=25 and set SSL=False
:: Change these following items to use the same variables all the time
:: or use the command line to pass all the variables
set Port=25
set SSL=False
set From="myemail@myemailserver.com"
set To="recipient@server.com"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="mailservername.myemailserver.com"
set User="username"
set Pass="password"
set fileattach="d:\myfolder\%datestamp%.txt"
:: This section sets the command line arguments
:: use this format: CALL email.bat "myname@gmail.com" "RecipientEmailAddress@server.com" "Subject line" "Email Body in one line" "smtp.gmail.com" "myname@gmail.com" "password" "d:\folder\filename to attach.txt"
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)
del "%vbsfile%" 2>nul
goto :EOF