Solved

Using VBScript to capture YYMMDD format

Posted on 2008-06-11
10
1,023 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
Problem to refer to value 8 46
Batch file or script with password 22 42
vbscript code to take snapshot of current screen and save into a folder. 6 22
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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