Question

I have a vbscript ,which sends the folder contents as attachments to my email but the problem is i am unable to specify the path of windows folder because the windows path is different for different computers.

In my code following works

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

but since path is different for different machines. i tried following but no success

Const PATH = "%windows%\Folder1\"

Here is the full vbscript code

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

when the configuration information for the remote SMTP server the code works perfectly.

how will i specify the windows,programfiles,desktop(special folders) in this script??

Was it helpful?

Solution

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

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

UPDATE:

sample usage:

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

output:

cscript specfolders.vbs
# of files in system folder: 27
# of files in desktop: 49

OTHER TIPS

Due to windows security architecture its not a good practice to do as you are trying. I would start from SpecialDirectories Class : http://msdn.microsoft.com/en-us/library/Microsoft.VisualBasic.FileIO.SpecialDirectories.aspx

If your objective is to send email with attachment? I will use the following example :

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

End Function

Or

If you want to use folder location to attach the file. Firstly I will not use c:\windows\folder1 as a location for files. As this folder contains all your/clients system files and you might run into security issues.

Insert the following code : Your code

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

Use the following

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

Returns the string "C:\Users\Owner\Documents". here you can add new folder in above code. use concatenation like this & "\" & "Folder1"

Hope this is helpful...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top