Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date convert 1

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
I need a way to convert the date portion of some file names.

The file names are in this format.

5212007-052SecurityAudit.xls

I am at a loss as to how to take the date and convert it to something like this 2007-05-21 or 70070521.

Any suggestions?? Please..





Thanks

John Fuhrman
Titan Global Services
 
This might work...the pattern may need some work if you want it to make sure the date is valid, but otherwise...

Code:
Option Explicit

Dim arrTemp : arrTemp = Array("5212007-052SecurityAudit.xls", _
							  "122007-052SecurityAudit.xls", _
							  "12212007-052SecurityAudit.xls", _
							  "10212007-052SecurityAudit.xls")

Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "([1][0-2]|[1-9])([1-3][0-9]|[1-9])(\d{4})"
RegEx.Global = True
RegEx.IgnoreCase = True

Dim strTemp1, intMonth, intDay, intYear
For Each strTemp1 In arrTemp
	intMonth = RegEx.Execute(strTemp1)(0).SubMatches(0)
	If Len(intMonth) = 1 Then intMonth = "0" & intMonth
	intDay = RegEx.Execute(strTemp1)(0).SubMatches(1)
	If Len(intDay) = 1 Then intDay = "0" & intDay
	intYear = RegEx.Execute(strTemp1)(0).SubMatches(2)
	WScript.Echo intYear & "-" & intMonth & "-" & intDay
	WScript.Echo intYear & intMonth & intDay
	WScript.Echo ""
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Perfect! and a star for you!!!

Code:
'Option Explicit

'Dim arrTemp : arrTemp = Array("5212007-052SecurityAudit.xls", _
'                              "122007-052SecurityAudit.xls", _
'                              "12212007-052SecurityAudit.xls", _
'                              "10212007-052SecurityAudit.xls")

Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "([1][0-2]|[1-9])([1-3][0-9]|[1-9])(\d{4})"
RegEx.Global = True
RegEx.IgnoreCase = True

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService. _
    ExecQuery("Select * from CIM_DataFile where Path = '\\001\\'")

For Each objFile in colFiles
'    Wscript.Echo objFile.Name 
'Next
Dim fso
Set fso = CreateObject("scripting.filesystemobject")
Dim arrTemp : arrTemp = Array(fso.GetBaseName(objFile.Name))

Dim strTemp1, intMonth, intDay, intYear
For Each strTemp1 In arrTemp
    intMonth = RegEx.Execute(strTemp1)(0).SubMatches(0)
    If Len(intMonth) = 1 Then intMonth = "0" & intMonth
    intDay = RegEx.Execute(strTemp1)(0).SubMatches(1)
    If Len(intDay) = 1 Then intDay = "0" & intDay
    intYear = RegEx.Execute(strTemp1)(0).SubMatches(2)
'    WScript.Echo intYear & "-" & intMonth & "-" & intDay
'    WScript.Echo intYear & intMonth & intDay
    arrFileName = Split(strTemp1,"-")
    WScript.Echo  intYear & "-" & intMonth & "-" & intDay & " - " & arrFileName(1)
'    WScript.Echo ""
    Next
next

I love the regex. I just haven't figured out how to use it properly yet.

Thanks!!!!!!!!!!

Thanks

John Fuhrman
Titan Global Services
 
Thanks and you're welcome!

Regular Expression scared me at first, but the hardest part is coming up with a pattern...still learning and I think they're awesome in certain situations.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top