Solved

Using VBScript to capture YYMMDD format

Posted on 2008-06-11
10
1,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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