Click to See Complete Forum and Search --> : convert filenames to a text date in a hyperlink


blodefood
10-27-2003, 12:45 PM
I have this code that displays filenames in a folder according to the first character of the filename.

I need to parse the filename W20031024-ID01.xls or M20031024-ID01.xls so that the text in the hyperlink displays as a date. Note that I can change the file naming conventions if that would make it easier.

e.g. October 24, 2003

I trust I have commented the code adequately.

Would appreciate some assistance. Thanks.

<%
dim strWeekly, strMonthly
strPath = "D://Wwwroot/test/newfolder/ID01/"

dim oFS, oFile, oFolder, oFolderContents, slot, varname

'slot corrects a path discrepency on the testing server
'it will be removed once it goes on the live server

slot="ID01/"
varname=left(oFile,1)

Set oFS = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFS.getFolder(strPath)


For Each oFile In oFolder.files
If Left(oFile.Name, 1) = "W" Then
strWeekly = strWeekly & _
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=""" & slot & oFile.Name & """>" & _
oFile.Name & "</a><br>"
ElseIf Left(oFile.Name, 1) = "M" Then
strMonthly = strMonthly & _
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=""" & slot & oFile.Name & """>" & _
oFile.Name & "</a><br>"
End If
Next

'after this is complete we will parse the filenames to English

%>

And this in the body of the document to display filenames in their appropriate columns in the table.

<td><% = strWeekly%></td>
<td><% = strMonthly%></td>

rdoekes
10-28-2003, 05:28 AM
The below code works only if all dates are in the format
yyyymmdd.
So if November 1, 2003 is written as 2003111 then this will not work (it has to be 20031101)

For Each oFile In oFolder.files
strFileName = oFile.Name 'filename in variable
strFileName = Mid(strFileName, 2, 8) 'take out the datepart
dtmDate = FormatDateTime( _
Mid(strFileName, 1, 4) & "/" & _
Mid(strFileName, 5, 2) & "/" & _
Mid(strFileName, 7, 2), _
1) 'format the date part
If Left(oFile.Name, 1) = "W" Then
strWeekly = strWeekly & _
" <a href=""" & slot & oFile.Name & """>" & _
dtmDate & "</a><br>"
ElseIf Left(oFile.Name, 1) = "M" Then
strMonthly = strMonthly & _
" <a href=""" & slot & oFile.Name & """>" & _
dtmDate & "</a><br>"
End If
Next

blodefood
10-28-2003, 10:43 AM
Thanks ever so much!

I managed to adapt the code this way so that I would get the month name. It looks a bit skewjy, but it works!

blodefood :D

________
<%
dim strWeekly, strMonthly
dim strFileName, dtmDate
dim oFS, oFile, oFolder, oFolderContents, slot, varname
dim Monthnamey, Dayy, Yeary
'corrects path discrepency on test server
slot="ID01/"
'for now to display the ID number
slot2="ID01"
'the first char of the filename
varname=left(oFile,1)

'the path will be replaced with a parameter from the login screen
'once the functions in notes below are created and working properly
strPath = "D://Wwwroot/test/newfolder/ID01/"

'Create the filesystem object
Set oFS = CreateObject("Scripting.FileSystemObject")
'get the files named in strpath
Set oFolder = oFS.getFolder(strPath)

For Each oFile In oFolder.files
' below are descriptive but do not use reserved words
Monthnamey = Monthname(Mid(oFile.Name, 6, 2))
Dayy = Mid(oFile.Name, 8, 2)
Yeary = Mid(oFile.Name, 2, 4)

'the filename starts with M or W followed by the date a hyphen and ID number
'format the date from yyyymmdd start at 2 and get 4 to make year
'start at 5 and get 2 to make month and use the name of the month
'start at 7 and get 2 to make day

'sort the files by first char, and display in hyperlink
If Left(oFile.Name, 1) = "W" Then
strWeekly = strWeekly & _
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=""" & slot & oFile.Name & """>" & _
Monthnamey & " " & Dayy & " " & Yeary & "</a><br>"
ElseIf Left(oFile.Name, 1) = "M" Then
strMonthly = strMonthly & _
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=""" & slot & oFile.Name & """>" & _
Monthnamey & " " & Dayy & " " & Yeary & "</a><br>"
End If
Next
%>

and in the body --

<td><% = strWeekly%></td>
<td><% = strMonthly%></td>