在VBScript中指定Windows文件夹路径

时间:2013-10-17 10:13:15

标签: email vbscript email-attachments cdo.message

我有一个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服务器的配置信息代码完美运行时。

我将如何在此脚本中指定windows,programfiles,desktop(特殊文件夹)?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

由于Windows安全架构,这不是一个很好的做法,因为你正在尝试。我将从SpecialDirectories Class开始:http://msdn.microsoft.com/en-us/library/Microsoft.VisualBasic.FileIO.SpecialDirectories.aspx

答案 2 :(得分:-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”。在这里你可以在上面的代码中添加新文件夹。使用这样的连接&amp; “\”&amp; “Folder1中”

希望这有用......