Declare PtrSafe Function OemToChar Lib "user32" Alias "OemToCharA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
Function oem2ansi(in_string) As String
Dim Out_String As String * 60000
Dim t As String
t = OemToChar(in_string, Out_String)
oem2ansi = Out_String
End Function
Liste = Trim(CreateObject("wscript.shell").exec(TempString).stdout.readall)
Liste = oem2ansi(Liste)
' Liste = ReadText_UTF8(Liste)
Liste = Trim(CreateObject("wscript.shell").exec(TempString).stdout.readall)
OemToChar Liste, Liste
Function ReadTextAutoDetect(text As String) As String
Dim fileContent() As Byte, fileSize&, utf8Index&, charCode&, decodedText$, currentByte As Byte
Dim tempLong1&, tempLong2&, tempLong3&, tempLong4 As Long
Dim isUTF8 As Boolean
' Convertir le texte en tableau d'octets
fileContent = StrConv(text, vbFromUnicode)
fileSize = UBound(fileContent) + 1
' Détection de l'encodage par présence du BOM UTF-8
If fileSize >= 3 And fileContent(0) = &HEF And fileContent(1) = &HBB And fileContent(2) = &HBF Then
isUTF8 = True
utf8Index = 3 ' Ignorer le BOM UTF-8
Else
' Estimation de l'encodage si pas de BOM
isUTF8 = False
For utf8Index = 0 To fileSize - 1
If (fileContent(utf8Index) And &H80) Then ' Octet supérieur à 127 trouvé
isUTF8 = True ' Probablement UTF-8
Exit For
End If
Next
utf8Index = 0 ' Réinitialiser l'index
End If
decodedText = "" ' Initialiser la chaîne de résultat
' Décodage en fonction de l'encodage détecté
Do While utf8Index < fileSize
currentByte = fileContent(utf8Index)
If isUTF8 Then
' Décodage UTF-8
Select Case True
Case (currentByte And &H80) = 0
charCode = currentByte
utf8Index = utf8Index + 1
Case (currentByte And &HE0) = &HC0
If utf8Index + 1 < fileSize Then
tempLong1 = (currentByte And &H1F) * &H40
tempLong2 = fileContent(utf8Index + 1) And &H3F
charCode = tempLong1 + tempLong2
utf8Index = utf8Index + 2
Else
Exit Do
End If
Case (currentByte And &HF0) = &HE0
If utf8Index + 2 < fileSize Then
tempLong1 = (currentByte And &HF) * &H1000
tempLong2 = (fileContent(utf8Index + 1) And &H3F) * &H40
tempLong3 = fileContent(utf8Index + 2) And &H3F
charCode = tempLong1 + tempLong2 + tempLong3
utf8Index = utf8Index + 3
Else
Exit Do
End If
Case (currentByte And &HF8) = &HF0
If utf8Index + 3 < fileSize Then
tempLong1 = (currentByte And &H7) * &H40000
tempLong2 = (fileContent(utf8Index + 1) And &H3F) * &H1000
tempLong3 = (fileContent(utf8Index + 2) And &H3F) * &H40
tempLong4 = fileContent(utf8Index + 3) And &H3F
charCode = tempLong1 + tempLong2 + tempLong3 + tempLong4
utf8Index = utf8Index + 4
Else
Exit Do
End If
Case Else
utf8Index = utf8Index + 1
GoTo NextChar
End Select
Else
' Décodage OEM-850
charCode = currentByte
utf8Index = utf8Index + 1
End If
decodedText = decodedText & ChrW(charCode) ' Ajouter le caractère décodé
NextChar:
Loop
ReadTextAutoDetect = decodedText ' Retourne le texte décodé
End Function
Private Declare PtrSafe Function OemToChar Lib "user32" Alias "OemToCharA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
' La variable Liste récupère le texte du stdout
Liste = Trim(CreateObject("wscript.shell").exec(TempString).stdout.readall)
OemToChar Liste, Liste
' Liste = ReadTextAutoDetect(Liste)
Private Sub CommandButton1_Click() 'bouton pour choisir le dossier parent
Dim fldr As Object
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Choisir un dossier à lister"
.AllowMultiSelect = False
If .Show = -1 Then TxtbFolder = .SelectedItems(1)
End With
End Sub