PathFormatter.class.asp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <%
  2. Class PathFormatter
  3. Public Function Format( ByVal pathFormat, ByVal filename )
  4. Dim ext, name
  5. If IsEmpty( format ) Then
  6. format = "{yyyy}{mm}{dd}{hh}{ii}{ss}{rand:6}"
  7. End If
  8. Set invalidPattern = new RegExp
  9. invalidPattern.Pattern = "[\\\/\:\*\?\<\>\|""]"
  10. invalidPattern.Global = true
  11. filename = invalidPattern.Replace( filename, "" )
  12. ext = GetExt( filename )
  13. name = GetNameWithoutExt( filename )
  14. pathFormat = Replace( pathFormat, "{filename}", name )
  15. pathFormat = Replace( pathFormat, "{time}", TimeStamp() )
  16. pathFormat = Replace( pathFormat, "{yyyy}", Year(Now) )
  17. pathFormat = Replace( pathFormat, "{yy}", Year(Now) Mod 100 )
  18. pathFormat = Replace( pathFormat, "{mm}", LeadZero( Month(Now) ) )
  19. pathFormat = Replace( pathFormat, "{dd}", LeadZero( Day(Now) ) )
  20. pathFormat = Replace( pathFormat, "{hh}", LeadZero( Hour(Now) ) )
  21. pathFormat = Replace( pathFormat, "{ii}", LeadZero( Minute(Now) ) )
  22. pathFormat = Replace( pathFormat, "{ss}", LeadZero( Second(Now) ) )
  23. Set randPattern = new RegExp
  24. randPattern.Pattern = "{rand(\:?)(\d+)}"
  25. Set matches = randPattern.Execute(pathFormat)
  26. If matches.Count Then
  27. Set match = matches(0)
  28. digit = 6
  29. If match.SubMatches.Count > 1 Then
  30. digit = 0 + match.SubMatches(1)
  31. End If
  32. min = 1
  33. Do While digit > 0
  34. min = min * 10
  35. digit = digit - 1
  36. Loop
  37. max = min * 10
  38. pathFormat = randPattern.Replace( pathFormat, Rand( min, max ) )
  39. End If
  40. Format = pathFormat + ext
  41. End Function
  42. Private Function GetExt( file )
  43. GetExt = Right( file, Len(file) - InStrRev(file, ".") + 1 )
  44. End Function
  45. Private Function GetNameWithoutExt( file )
  46. GetNameWithoutExt = Left( file, InStrRev(file, ".") - 1 )
  47. End Function
  48. Private Function TimeStamp()
  49. TimeStamp = DateDiff("s", "1970-1-1 8:00:00", Now())
  50. End Function
  51. Private Function Rand( min, max )
  52. Randomize
  53. Rand = Int( (max - min + 1) * Rnd + min )
  54. End Function
  55. Private Function GetFormatedDate()
  56. Dim yyyy, mm, dd
  57. yyyy = Year(Date)
  58. mm = LeadZero(Month(Date))
  59. dd = LeadZero(Day(Date))
  60. GetFormatedDate = yyyy & mm & dd
  61. End Function
  62. Private Function LeadZero( number )
  63. If number < 10 Then
  64. LeadZero = "0" & number
  65. Else
  66. LeadZero = number
  67. End If
  68. End Function
  69. End Class
  70. %>