Solved

Using VBScript to capture YYMMDD format

Posted on 2008-06-11
10
1,031 Views
Last Modified: 2010-07-27
Dear Experts,

I've a file format named YYMMDD.MSG in a server, whereby
YYMMDD = today date (auto generated log file).
Eg. 080612 (12th June 2008)

I need to use VBscript to grab this file and copy to another server.

How do i capture the file name in the format YYMMDD.MSG using VBScript?

I tried to format the date, but not successful.

Hope you can assist, thanks!;)

rgds,
kelvin
0
Comment
Question by:kunghui80
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 5

Accepted Solution

by:
rstomar earned 100 total points
ID: 21765917
CStr(Year(Now())) & CStr(Month(Now())) & CStr(Day(Now()))   will give you a string in yyyymmdd format.
Use CStr(Year(Now()) mod 100)  for yy
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 150 total points
ID: 21765920
Hi,

This string will give you the file name.

strFileName = Right(Year(Date), 2) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & ".MSG"

Regards,

Rob.
0
 
LVL 2

Author Comment

by:kunghui80
ID: 21766012
hi rstomar,

thanks for the format. I notice there's leading "0" problem.
The result will show 8612, instead of 080612, any ideal how could avoid this?

I've manual done like this, but wish to see if any better solutions
MyFileNameA="C:\" & "0" & CStr(Year(Now()) mod 100) & "0" & CStr(Month(Now())) & CStr(Day(Now())) & ".MSG"
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:kunghui80
ID: 21766020
hi RobSampson,

Im testing on this now, will get back to you soon.

rgds,
kelvin
0
 
LVL 2

Author Comment

by:kunghui80
ID: 21766175
I got a problem. For scenario:
Today is 1st May 2008 file generated will be (080501.MSG)

My script will always try to get yesterday file, using the script, im getting (080500.MSG)
strFileName = Right(Year(Date), 2) & Right("0" & Month(Date), 2) & Right("0" & Day(Date)-1, 2) & ".MSG"

0
 
LVL 22

Expert Comment

by:danaseaman
ID: 21766178
Try this:
Option Explicit
 
Private Sub Form_Load()
   Debug.Print fYYMMDD(Now)
End Sub
 
Function fYYMMDD(ByVal dat As Date) As String
   fYYMMDD = Mid(CStr(Year(dat)), 3) & Right("0" & CStr(Month(dat)), 2) & Right("0" & CStr(Day(dat)), 2) & ".MSG"
End Function

Open in new window

0
 
LVL 2

Author Comment

by:kunghui80
ID: 21766184
If today is 1st May 2008, i'll need to get 080430.MSG file, anyway to capture the day-1?
0
 
LVL 22

Assisted Solution

by:danaseaman
danaseaman earned 50 total points
ID: 21766197
For yesterdays date try this:
Option Explicit
 
Private Sub Form_Load()
   Debug.Print fYYMMDD(Now)
End Sub
 
Function fYYMMDD(ByVal dat As Date) As String
   dat = dat - 1
   fYYMMDD = Mid(CStr(Year(dat)), 3) & Right("0" & CStr(Month(dat)), 2) & Right("0" & CStr(Day(dat)), 2) & ".MSG"
End Function

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 150 total points
ID: 21766539
This will do it.

Regards,

Rob.
dteYesterday = DateAdd("d", -1, Date)
strFileName = Right(Year(dteYesterday), 2) & Right("0" & Month(dteYesterday), 2) & Right("0" & Day(dteYesterday), 2) & ".MSG"

Open in new window

0
 
LVL 2

Author Comment

by:kunghui80
ID: 21767588
Thank you for your all assistance, it helps lots.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question