将文件移动到其他文件夹并在电子邮件附件上发送文件夹URL

时间:2016-10-28 11:30:12

标签: batch-file vbscript

批处理文件:

@echo off
set source=D:\teste
set target=D:\trab

for /F "delims=" %%I in ('dir %source%\*.* /A:-D /O:D /B') do move %source%\%%I %target% & echo %%I %source%\%%I & goto :continue
:continue

set p/ AttachmentFullName=%target%\<<fileName.txt>>

cscript.exe //NoLogo "D:\partilhas\correo.vbs"  %AttachmentFullName%

timeout 20

VBScript文件:

Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields

Dim attachmentFullName
attachmentFullName = WScript.Arguments.Named("%AttachmentFullName%")

objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.SMTP.com"
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "NAME"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "PWS"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
objMail.From = "EMAIL@EMAIL.com"
objMail.To = "EMAIL@EMAIL.com"
objMail.Subject = "Email Subject Text"
objMail.AddAttachment attachmentFullName
objMail.TextBody = "The message of the email..."
objMail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing

我想要的是将文件从最旧到最新的文件夹逐个获取并发送一封电子邮件。作为文件号发送相同的邮件号并移动相同的次数。有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

您的问题对于您遇到的问题并不是很清楚,但可能是您的参数传递过来。

该行

attachmentFullName = WScript.Arguments.Named("%AttachmentFullName%")

named parameter %AttachmentFullName%的参数分配给变量attachmentFullName。但是,要实现这一点,您必须使用该名称的命名参数来实际调用VBScript。 VBScript中的命名参数的工作方式如下:

cscript.exe script.vbs /param:value

意味着在您的情况下,调用必须如下所示:

cscript.exe script.vbs /^%AttachmentFullName^%:value

实际获取以%字符开头和结尾的参数名称。

您更有可能希望将批处理变量%AttachmentFullName%的值作为unnamed argument传递给VBScript。要做到这一点,请更改声明

attachmentFullName = WScript.Arguments.Named("%AttachmentFullName%")

到此:

attachmentFullName = WScript.Arguments.Unnamed(0)

并像这样调用VBScript:

cscript.exe //NoLogo "D:\partilhas\correo.vbs" "%AttachmentFullName%"

请注意变量周围的双引号,因此当路径包含空格时,您的脚本不会中断。

至于首先为该批量变量赋值,目前您似乎在for循环中回显文件名并提示用户手动输入。

set p/ AttachmentFullName=%target%\<<fileName.txt>>

首先,上述语句不正确,会引发错误。但即使它确实有效,您也应该在循环中为变量分配路径,而不是要求用户交互:

for /F "delims=" %%I in ('dir %source%\*.* /A:-D /O:D /B') do (
    move "%source%\%%~I" "%target%"
    set "AttachmentFullName=%target%\%%~I"
    goto :continue
)

编辑:要添加所有复制的文件,您需要从循环中删除goto并附加到变量(需要delayed expansion)。在循环循环中的每个路径周围添加双引号,并且在将附件变量传递给VBScript时不要在引号变量周围放置双引号:

setlocal EnableDelayedExpansion
for /F "delims=" %%I in ('dir %source%\*.* /A:-D /O:D /B') do (
    move "%source%\%%~I" "%target%"
    set "Attachments=!Attachments! ^"%target%\%%~I^""
)

cscript.exe //NoLogo "D:\partilhas\correo.vbs" %Attachments%

然后让VBScript迭代(未命名)参数并将每个参数附加到邮件中:

For Each arg In WScript.Arguments.Unnamed
  objMail.AddAttachment arg
Next