2013-10-17 33 views
-1

我有一個vbscript,它將文件夾內容作爲附件發送到我的電子郵件,但問題是我無法指定windows文件夾的路徑,因爲不同計算機的windows路徑不同。在VBScript中指定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 = "[email protected]" 
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服務器的配置信息的代碼完美的作品。

我將如何在此腳本中指定窗口,程序文件,桌面(特殊文件夾)?

回答

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

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

UPDATE:

示例用法:

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 
+0

那麼我的腳本應該是什麼樣子。對不起,我是vb腳本的初學者,請說明你的代碼需要放在哪裏? –

+0

@AidenJones - 查看更新。 –

+0

感謝您的答覆。我感謝您的示例用法,你瞭解我,但正如我所提到的我是一個初學者,我試圖適應你的代碼,但我每次都會收到錯誤,例如「expect fso」。請編輯我的代碼並提及它在你的回覆。提醒:我的腳本工作正常,將文件夾的內容附加到電子郵件,但「窗口,程序文件等」的路徑顯然是問題,因爲它改變了不同的pc.seriously它會解決我的挫折周!謝謝。 ;) –

-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:\用戶\用戶\文檔」。在這裏你可以在上面的代碼中添加新文件夾。使用級聯這樣&「\」 &「Folder1中」

希望這是有益...

+0

這不是VBScript! –

+0

你的意思是字符串PATH ?.它是一個幫助提交者理解邏輯的僞代碼。在vb.net它將PATH作爲字符串... – Jag

+0

我的意思是你的* VB.NET *代碼; OT要求提供* VBScript *建議。 –

相關問題