Les Snippets

Connexion

OPTENIR DANS UNE STRING LA TAILLE D'UN FICHIER EN KO, MO, GO...

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 01/12/2007 22:30:47 et initié par renocmoa [Liste]
Date de mise à jour : 26/02/2009 19:59:52
Vue : 5176
Catégorie(s) : API, Fichier / Disque, Chaîne de caractères
Langages dispo pour ce code :
- VB 2005
- Delphi 5
- VB6, VBA
- C# 1.x, C# 2.x, C# 3.x
- VB6, VBA



Langage : VB 2005
Date ajout : 01/12/2007
Posté par renocmoa [Liste]
DateMAJ : 04/12/2007
<System.Runtime.InteropServices.DllImport("shlwapi")> _
Public Function StrFormatByteSizeA(ByVal dw As UInteger, _
  ByVal pszBuf As System.Text.StringBuilder, _
  ByVal cchBuf As Integer) As System.IntPtr
End Function

Public Function TailleFichier(ByVal TailleEnOctets As UInteger) As String  Dim mem As New System.Text.StringBuilder(256)
  If StrFormatByteSizeA(TailleEnOctets, mem, mem.Capacity) <> System.IntPtr.Zero Then
    Return mem.ToString()
  Else
    Return Nothing
  End If
End Function  
Remarque :
petite utilisation d'une api permetant d'optenir la taille d'un fichier sous forme de texte ex :
  532 -> 532 bytes
      1340 -> 1.3KB
     23506 -> 23.5KB
   2400016 -> 2.4MB
2400000000 -> 2.4GB
Langage : Delphi 5
Date ajout : 02/12/2007
Posté par japee [Liste]
DateMAJ : 02/12/2007
function StrFormatByteSize(dw: DWORD; szBuf: PChar; uiBufSize: UINT): PChar; stdcall;
  external 'shlwapi.dll' name 'StrFormatByteSizeA';
 
function FormatByteSize(Value: DWORD): string;
var
  Buff: array[0..255] of Char;
begin
  StrFormatByteSize(Value, Buff, SizeOf(Buff));
  Result := Buff;
end;
 
// exemple d'utilisation :
var 
  FmtSize: string;
  Value: Longword;
 
FmtSize := FormatByteSize(Value);
 

Remarque :
La librairie shlwapi.dll doit se trouver dans le répertoire de WINDOWS.
Elle est installée par Internet Explorer.
Langage : VB6 , VBA
Date ajout : 05/11/2008
Posté par jrivet [Liste]
DateMAJ : 05/11/2008
Private Declare Function StrFormatByteSize Lib "shlwapi" Alias "StrFormatByteSizeA" (ByVal dw As Long, ByVal pszBuf As String, ByVal cchBuf As Long) As Long
'retourne la taille d'un fichier se trouvant en sPath
Public Function TailleFichier(ByVal sPath As String) As String
Dim StrOut As String
Dim lSize As Long
    'On vérifie si le fichier existe
    If Dir(sPath) <> vbNullString Then
        'on récupère sa taille
        lSize = FileLen(sPath)
        StrOut = Space(64)
        'on la formate avec StrFormatByteSize
        Call StrFormatByteSize(lSize, StrOut, Len(StrOut) - 1)
        TailleFichier = Trim$(StrOut)
    End If
End Function
Remarque :
Traduction en VB6, j'ai tout de fois ajouter une petite variante, il faut passer en paramètre à la fonction le chemin d'un fichier.
NB : fonction limitée à des fichiers de 2Go MAX
Langage : C# 1.x , C# 2.x , C# 3.x
Date ajout : 25/02/2009
Posté par Charles Racaud [Liste]
DateMAJ : 26/02/2009
// using System.Text;
// using System.Runtime.InteropServices;

[DllImport("shlwapi")]
private static extern string StrFormatByteSize64(long qdw, StringBuilder pszBuf, uint cchBuf);
private static string StrFormatByteSize64(long qdw) {
  StringBuilder StrSize = new StringBuilder(64);
    StrFormatByteSize64(qdw, StrSize, 64U);
  return StrSize.ToString();
}

// Exemples
StrFormatByteSize64(532L); // "532 bytes"
StrFormatByteSize64(1340L); // "1,30 KB"
StrFormatByteSize64(23506L); // "22,9 KB"
StrFormatByteSize64(2400016L); // "2,28 MB"
StrFormatByteSize64(2400000000L); // "2,23 GB"
StrFormatByteSize64(5466554984654L); // "4,97 TB"
StrFormatByteSize64(11874650654198465L); // "10,5 PB"
StrFormatByteSize64(7266167416426541069L); // "6,30 EB"
StrFormatByteSize64(long.MaxValue); // "7,99 EB"
Langage : VB6 , VBA
Date ajout : 18/03/2009
Posté par Charles Racaud [Liste]

Private Type LARGE_INTEGER
  lowpart As Long
  highpart As Long
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As AnyByVal Length As Long)
Private Declare Function StrFormatByteSize64 Lib "shlwapi" Alias "StrFormatByteSizeW" (ByVal dw As CurrencyByVal pszBuf As Long, cchBuf As LongAs Long

' Attention : Un Currency est codé sur 8 octets mais à 4 chiffres après la virgule.
'             Il faut donc diviser par 10000 pour avoir la bonne valeur.
'             StrFormatByteSize64__C(654) retournera 6,23 MB (FAUX)
'             StrFormatByteSize64__C(654 / 10000) retournera 654 bytes
Private Function StrFormatByteSize64__C(dw As CurrencyAs String
  Dim StrSize As String
  StrSize = VBA.String$(64&, Chr$(0))
  Call StrFormatByteSize64(dw, StrPtr(StrSize), 64&)
  StrFormatByteSize64__C = VBA.Left$(StrSize, InStr(StrSize, Chr$(0)) - 1)
End Function

Private Function StrFormatByteSize64__LI(dw As LARGE_INTEGER) As String
  Dim dw__C As Currency
  Call CopyMemory(dw__C, dw, LenB(dw))
  StrFormatByteSize64__LI = StrFormatByteSize64__C(dw__C)
End Function

Private Function StrFormatByteSize64__LL(ByVal nFileSizeLow As LongOptional ByVal nFileSizeHigh As Long = 0As String
  Dim dw As LARGE_INTEGER
  dw.lowpart = nFileSizeLow
  dw.highpart = nFileSizeHigh
  StrFormatByteSize64__LL = StrFormatByteSize64__LI(dw)
End Function

Remarque :
Maximum de 7,99 EB et avec la gestion unicode.

Snippets en rapport avec : Fichier, Api, Taille fichier



Codes sources en rapport avec : Fichier, Api, Taille fichier

{C / C++ / C++.NET} COPIE AVEC SHFILEOPERATION
Bonjour Voici un exemple de l'utilisation de la fonction SHFileOperation de l'api windows. La ...

{C / C++ / C++.NET} TXT SUPPRIMER LIGNES DOUBLONS (WIN32)
Demo pour cette question du forum: http://www.cppfrance.com/forum.v2.aspx?ID=1234830 Exe qui sup...

{PDA / PocketPC} ECRITURE D'UN FICHIER ANSI PAR LES API
Ce source écrit des données au format ANSI dans un fichier à l'aide des API système. Pourquoi ce ...

{Visual Basic, VB6, VB.NET, VB 2005} FERMER UN FICHIER OUVERT PAR UNE AUTRE APPLICATION
Il vous est deja arrivé de vouloir supprimer un fichier mais de ne pas pouvoir car une application ...

{Delphi} OBJET POUR LA PROJECTION DE FICHIER EN MÉMOIRE (FILE MAPPING)
L’unité MappedFileStream permet d’utiliser la technique de projection de fichier en mémoire de Windo...

{JAVA / J2EE} API POUR FAIRE DES PDF A PARTIE DE FICHIER TEXT
juste il faut compiler la classe sous eclipse par exemple puis: java -classpath C:\path\to\pdf;C:\p...

{Delphi} CRÉATION DE FICHIERS TEMPORAIRES DANS LE DOSSIER TEMPORAIRE COURANT.(API)
Ayant eu besoin de travailler avec des fichiers temporaires, je suis tombé par hasard sur 2 fonction...

{Assembleur} FAIRE FICHIER ISO (MASM32)
Windows 2000 ou supérieur requis. Fait un fichier "F1.iso" depuis un CD ou DVD. N'écrit le iso q...

{Delphi} VERROUILER UN FICHIER
Voici une fonction qui permet de verrouiller un fichier. Et une procedure qui permet de déverrouil...

{Visual Basic, VB6, VB.NET, VB 2005} JOUER DES FICHIERS MP3 (SANS OCX)
Voici une façon de procéder pour lire des MP3 sans OCX (Attention : il faut avoir des codecs p...