Leaked source code of windows server 2003
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.

209 lines
4.7 KiB

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