-1

我有一个 vbscript,它将文件夹内容作为附件发送到我的电子邮件,但问题是我无法指定 windows 文件夹的路径,因为不同计算机的 windows 路径不同。

在我的代码中,以下作品

Const PATH = "C:\windows\Folder1\"

但由于不同机器的路径不同。我尝试关注但没有成功

Const PATH = "%windows%\Folder1\"

这是完整的 vbscript 代码

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM


Set objMessage = CreateObject("CDO.Message")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFolder
Dim oFile
Dim oFiles
Const PATH = "%windows%\Folder\" 'This method not working!!!!! 
Set oFolder = fso.GetFolder(PATH)
Set oFiles= oFolder.files
objMessage.Subject = "This is the email subject"
objMessage.From = "mailSender@MyMail.com"
objMessage.To = ""
objMessage.TextBody = "This is the body of the email. I’m fairly unoriginal"
For Each oFile in oFolder.files
objMessage.AddAttachment PATH & oFile.name
Next
'==This section will provide the configuration information for the remote SMTP server.
'==End remote SMTP server configuration section==

objMessage.Send

当远程 SMTP 服务器的配置信息代码完美运行时。

我将如何在此脚本中指定窗口、程序文件、桌面(特殊文件夹)?

4

3 回答 3

1
>> WScript.Echo CreateObject("WScript.Shell").ExpandEnvironmentStrings("%windir%")
>>
C:\WINDOWS

>> WScript.Echo CreateObject("WScript.Shell").SpecialFolders("Desktop")
>>
C:\Documents and Settings\eh\Desktop

更新:

示例用法:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

'Your problem in a nutshell
'Const PATH = "c:\windows\system"  ' fails on systems with %windir% <> c:\windows
'Const PATH = "%windir%\system"  ' fails with "Path not found" (FSO does not expand env vars)

Dim goWS   : Set goWS = CreateObject("WScript.Shell")
' PATH can't be a Const, because Consts can be initialized with literals only
' I use the prefix "cs" to indicate "constant string - keep your fingers off!"
Dim csPATH  : csPATH   = goWS.ExpandEnvironmentStrings("%windir%\system")
Dim csDESKT : csDESKT  = goWS.SpecialFolders("desktop")
WScript.Echo "# of files in system folder:", goFS.GetFolder(csPATH).Files.Count
WScript.Echo "# of files in desktop:", goFS.GetFolder(csDESKT).Files.Count

输出:

cscript specfolders.vbs
# of files in system folder: 27
# of files in desktop: 49
于 2013-10-17T10:36:46.050 回答
0

由于 Windows 安全体系结构,它不是您尝试的好习惯。我将从 SpecialDirectories 类开始:http: //msdn.microsoft.com/en-us/library/Microsoft.VisualBasic.FileIO.SpecialDirectories.aspx

于 2013-10-17T10:17:37.880 回答
-1

如果您的目标是发送带有附件的电子邮件?我将使用以下示例:

Public Shared Function SendMail(strFrom As String, strTo As String, strSubject As String, strMsg As String) As Boolean
Try
    ' Create the mail message
    Dim objMailMsg As New MailMessage(strFrom, strTo)

    objMailMsg.BodyEncoding = Encoding.UTF8
    objMailMsg.Subject = strSubject
    objMailMsg.Body = strMsg
    Dim at As New Attachment(Server.MapPath("~/Uploaded/txt.doc"))
    objMailMsg.Attachments.Add(at)
    objMailMsg.Priority = MailPriority.High
    objMailMsg.IsBodyHtml = True

    'prepare to send mail via SMTP transport
    Dim objSMTPClient As New SmtpClient()
    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
    objSMTPClient.Send(objMailMsg)
    Return True
Catch ex As Exception
    Throw ex
End Try

结束功能

或者

如果要使用文件夹位置来附加文件。首先,我不会使用 c:\windows\folder1 作为文件的位置。由于此文件夹包含您/客户的所有系统文件,您可能会遇到安全问题。

插入以下代码:您的代码

\\ Const PATH = "%windows%\Folder\" 'This method not working!!!!! 
\\ Set oFolder = fso.GetFolder(PATH)

使用以下

string PATH = My.Computer.FileSystem.SpecialDirectories.MyDocuments 

返回字符串“C:\Users\Owner\Documents”。在这里您可以在上面的代码中添加新文件夹。使用这样的连接 & "\" & "Folder1"

希望这会有所帮助...

于 2013-10-17T12:37:31.457 回答