如何获取当前月份的数值并将其放在VBScript / .vbs中的变量中

时间:2012-06-26 15:25:59

标签: variables date vbscript automation

我写了一个.vbs脚本,用当前日志文件向管理员发送电子邮件 以下是我到目前为止的情况:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\automatic_deployment\filename.txt",    ForReading)
fileName = objTextFile.ReadLine
Wscript.Echo fileName

Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
Dim month
ToAddress = "myaddress@myemail.com"
MessageSubject = "Deployment was successful"
MyTime = Now
MessageBody = "Successful deployment. Log file is attached." 
MessageAttachment = "C:\M\XYZ\201206\"&fileName&"_DEV_Log.txt"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

objTextFile.Close

如果你看到,有一个名为“MessageAttachment”的变量,其中附加了日志文件。在日志文件的目标部分中,有201206,表示年份和月份。该文件夹保存2012年6月的日志。那个月每个月递增一次。如你所见,它是硬编码的。 它到目前为止工作正常。但是,我想更进一步,让它更有活力。 我想创建一个变量并获取当前月份的当前值,并将其放在源目标的那一部分,如下所示:

month = aqDateTime.GetMonth(Date)
MessageAttachment = "C:\M\XYZ\2012"&month&"\"&fileName&"_DEV_Log.txt"

这会有用吗?任何帮助,将不胜感激。 谢谢!

2 个答案:

答案 0 :(得分:0)

你可以;

dim thisMonth: thisMonth = cstr(month(date))
if (len(thisMonth) = 1) then thisMonth = "0" & thisMonth

制作

"C:\M\XYZ\2012" & thisMonth & "\" & fileName & "_DEV_Log.txt"

等于

"C:\M\XYZ\201206\XXX_DEV_Log.txt"

(今年year(date)

答案 1 :(得分:0)

<%
current_month = DatePart("m",date) 

Response.Write current_month 
%>