Les Snippets

Connexion

Obtenir la taille d'un fichier en octets

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 25/11/2008 22:43:42 et initié par PCPT [Liste]
Date de mise à jour : 18/01/2009 01:36:34
Vue : 8814
Catégorie(s) : Fichier / Disque
Langages dispo pour ce code :
- VB 2005, VB 2008, VB.NET 1.x
- VB6, VBA
- VBScript
- C# 2.x, C# 3.x
- VB6, VBA
- Delphi 5
- Tcl



Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 25/11/2008
Posté par PCPT [Liste]
DateMAJ : 25/11/2008
    Function GetFileLength(ByVal sPathFile As StringAs Long
        Dim oFile As New System.IO.FileInfo(sPathFile)
        Dim lRet As Long
        If oFile.Exists Then lRet = oFile.Length Else lRet = -1
        oFile = Nothing
        Return lRet
    End Function
Remarque :
MessageBox.Show(GetFileLength("c:\pagefile.sys").ToString)
Langage : VB6 , VBA
Date ajout : 25/11/2008
Posté par PCPT [Liste]
DateMAJ : 25/11/2008
Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As StringAs Long
Function GetFileLength(ByVal sPathFile As String) As Long
    Const INVALID_FILE_ATTRIBUTES As Long  = &HFFFFFFFF
    If GetFileAttributes(sPathFile) = INVALID_FILE_ATTRIBUTES  Then
        GetFileLength = -1
    Else
        GetFileLength = FileLen(sPathFile)
    End If
End Function

Remarque :
parfois : n'autorise pas l'accès aux fichiers protégés en cours d'utilisation. retournera "-1" en ce cas, ou si le fichier n'existe pas
Langage : VBScript
Date ajout : 25/11/2008
Posté par PCPT [Liste]
DateMAJ : 25/11/2008
Function GetFileLength(sPathFile)
'    reader
    Dim oFSO 
    Set oFSO = CreateObject("Scripting.fileSystemObject")
    
'    retour
    If oFSO.FileExists(sPathFile) Then
        GetFileLength = oFSO.GetFile(sPathFile).Size
    Else
        GetFileLength = "-1"
    End If
    
'    nettoyage
    Set oFSO = Nothing
End Function

Langage : C# 2.x , C# 3.x
Date ajout : 03/12/2008
Posté par SwitchApocalyps [Liste]
public static long GetFileLength(string sPathFile)
{
  System.IO.FileInfo  oFile = new System.IO.FileInfo(sPathFile);
  if(oFile.Exists())
     return oFile.Length;
  else
     return -1;
}

Langage : VB6 , VBA
Date ajout : 18/01/2009
Posté par PCPT [Liste]
DateMAJ : 18/01/2009
Private Const GENERIC_READ    As Long &H80000000
Private Const FILE_SHARE_READ As Long &H1
Private Const OPEN_EXISTING   As Long 3&
'
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As StringByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes  As Any, ByVal dwCreationDisposition  As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As  Long
Private Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, lpFileSize As Currency) As Boolean
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As LongAs Long
'
Public Function GetFileLen(ByVal sPath As String) As Currency
    Dim hFile As Long, nSize As Currency
    hFile = CreateFile(sPath, GENERIC_READ, FILE_SHARE_READ,  ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
    Call GetFileSizeEx(hFile, nSize)
    Call CloseHandle(hFile)
    GetFileLen = nSize * 10000
End Function

Remarque :
adaptation depuis l'API-Guid
Langage : Delphi 5
Date ajout : 18/07/2009
Posté par f0xi [Liste]
function GetFileSize(const FileName: String): Int64;
var
  hFile : LongWord;
  lpFNFO : BY_HANDLE_FILE_INFORMATION;
begin
  result := -1;
  hFile := CreateFile(PChar(FileName), DWORD($80000000), $03, nil, $03, $80, 0);
  try
    if GetFileInformationByHandle(hFile, lpFNFO) then
    begin
      Int64Rec(result).Lo := lpFNFO.nFileSizeLow;
      Int64Rec(result).Hi := lpFNFO.nFileSizeHigh;
    end;
  finally
    CloseHandle(hFile);
  end;
end;
Langage : Tcl
Date ajout : 28/07/2009
Posté par gersoo [Liste]
set size [file size $file]


Snippets en rapport avec : Fichier, Taille, Octets, Poid, Filelen



Codes sources en rapport avec : Fichier, Taille, Octets, Poid, Filelen

{Visual Basic, VB6, VB.NET, VB 2005} TAILLE D'UN FICHIER
Comment connaître la taille d'un fichier en octet ? Mettez ce code dans un bouton par exemple :...

{Visual Basic, VB6, VB.NET, VB 2005} CRÉATION D'UN FICHIER AUTOEXTRACTIBLE
Cette source a pour but de montrer comment créer un fichier autoextractible contenant plusieurs fich...

{ASP / ASP.NET} GESTION DES EXTENSIONS SELON LE SIZE
Fonction qui vérifie un nombre et le traduit selon le poids. Ex.: 1115798 donnera 1.1 MB Permet d...

{JAVA / J2EE} EXERCICE SUR LE RANDOMACCESSFILE A TAILLE FIXE
Permet un affichage, un ajout et une recherche dans un RandomAccessFile a taille FIXE...

{ColdFusion} LIMITER LA TAILLE DU FICHIER À UPLOADER
Ce code permet de fixer une limite à la taille des fichiers que l'on veut uploader...

{C / C++ / C++.NET} DETERMINER LA TAILLE D'UN FICHIER
devrait fonctionner sur tout compilateur ...

{Visual Basic, VB6, VB.NET, VB 2005} RÉCUPÉRER LA TAILLE D'UN FICHIER À LA FAÇON DE L'EXPLORATEUR WINDOWS
La fonction TailleFichieroKoMoGo permet de récupérer la taille d'un fichier à la façon de l'Explorat...

{Visual Basic, VB6, VB.NET, VB 2005} UN PROGRAMME QUI AMÉLIORE LES PROPRIÉTÉS DE WINDOWS
J'ai fais un mélange des sources "DATE, TAILLE, TYPE, NOM COUR, NOM LONG, ATTRIBUTS,... d'un fichier...

{Visual Basic, VB6, VB.NET, VB 2005} TAILLE D'UN LECTEUR OU D'UN REPERTOIRE EN OCTETS
Ces lignes de code permetent de retourner la taille(en octet) d'un lecteur logique,ou d'un répertoir...

{Visual Basic, VB6, VB.NET, VB 2005} TESTER LA PRESENCE D'UN FICHIER
Comment puis-je tester la présence d'un fichier ? Mettez cette fonction dans un Module :...