Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.6 KiB

  1. Attribute VB_Name = "FilesAndDirs"
  2. Option Explicit
  3. Public Function FileNameFromPath( _
  4. ByVal i_strPath As String _
  5. ) As String
  6. FileNameFromPath = Mid$(i_strPath, InStrRev(i_strPath, "\") + 1)
  7. End Function
  8. Public Function DirNameFromPath( _
  9. ByVal i_strPath As String _
  10. ) As String
  11. Dim intPos As Long
  12. DirNameFromPath = ""
  13. intPos = InStrRev(i_strPath, "\")
  14. If (intPos > 0) Then
  15. DirNameFromPath = Mid$(i_strPath, 1, intPos)
  16. End If
  17. End Function
  18. Public Function FileNameFromURI( _
  19. ByVal i_strURI As String _
  20. ) As String
  21. Dim intPos As Long
  22. intPos = InStrRev(i_strURI, "/")
  23. If (intPos = 0) Then
  24. ' Sometimes the authors write the URI like "distrib.chm::\distrib.hhc"
  25. ' instead of "distrib.chm::/distrib.hhc"
  26. intPos = InStrRev(i_strURI, "\")
  27. End If
  28. FileNameFromURI = Mid$(i_strURI, intPos + 1)
  29. End Function
  30. Public Function FileExtension( _
  31. ByVal i_strFileName As String _
  32. ) As String
  33. Dim strFileName As String
  34. Dim intStart As Long
  35. strFileName = FileNameFromPath(i_strFileName)
  36. intStart = InStrRev(strFileName, ".")
  37. If (intStart <> 0) Then
  38. FileExtension = Mid$(strFileName, intStart)
  39. End If
  40. End Function
  41. Public Function FileNameWithoutExtension( _
  42. ByVal i_strFileName As String _
  43. ) As String
  44. Dim strFileName As String
  45. Dim intStart As Long
  46. strFileName = FileNameFromPath(i_strFileName)
  47. intStart = InStrRev(strFileName, ".")
  48. If (intStart <> 0) Then
  49. FileNameWithoutExtension = Mid$(strFileName, 1, intStart - 1)
  50. Else
  51. FileNameWithoutExtension = strFileName
  52. End If
  53. End Function
  54. Public Function FileRead( _
  55. ByVal i_strPath As String _
  56. ) As String
  57. On Error GoTo LEnd
  58. FileRead = ""
  59. Dim FSO As Scripting.FileSystemObject
  60. Dim TStream As Scripting.TextStream
  61. Set FSO = New Scripting.FileSystemObject
  62. Set TStream = FSO.OpenTextFile(i_strPath)
  63. FileRead = TStream.ReadAll
  64. LEnd:
  65. End Function
  66. Public Function FileExists( _
  67. ByVal i_strPath As String _
  68. ) As Boolean
  69. On Error GoTo LErrorHandler
  70. If (Dir(i_strPath) <> "") Then
  71. FileExists = True
  72. Else
  73. FileExists = False
  74. End If
  75. Exit Function
  76. LErrorHandler:
  77. FileExists = False
  78. End Function
  79. Public Function FileWrite( _
  80. ByVal i_strPath As String, _
  81. ByVal i_strContents As String, _
  82. Optional ByVal i_blnAppend As Boolean = False, _
  83. Optional ByVal i_blnUnicode As Boolean = False _
  84. ) As Boolean
  85. On Error Resume Next
  86. Dim intError As Long
  87. Err.Clear
  88. FileWrite = False
  89. Dim FSO As Scripting.FileSystemObject
  90. Dim TStream As Scripting.TextStream
  91. Set FSO = New Scripting.FileSystemObject
  92. If (Not i_blnAppend) Then
  93. If (FSO.FileExists(i_strPath)) Then
  94. FSO.DeleteFile i_strPath
  95. End If
  96. End If
  97. Set TStream = FSO.OpenTextFile(i_strPath, ForAppending)
  98. intError = Err.Number
  99. Err.Clear
  100. If (intError = 53) Then ' File not found
  101. Set TStream = FSO.CreateTextFile(i_strPath, True, i_blnUnicode)
  102. ElseIf (intError <> 0) Then
  103. GoTo LEnd
  104. End If
  105. TStream.Write i_strContents
  106. intError = Err.Number
  107. Err.Clear
  108. If (intError <> 0) Then
  109. GoTo LEnd
  110. End If
  111. FileWrite = True
  112. LEnd:
  113. End Function
  114. Public Function TempFile() As String
  115. Dim FSO As Scripting.FileSystemObject
  116. Set FSO = New Scripting.FileSystemObject
  117. TempFile = Environ$("TEMP") & "\" & FSO.GetTempName
  118. End Function