Les Snippets

Connexion

vérifier si un dossier est vide

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 12/09/2008 18:01:40 et initié par gillardg [Liste]
Date de mise à jour : 17/12/2008 16:09:54
Vue : 7343
Catégorie(s) : Fichier / Disque, Trucs & Astuces
Langages dispo pour ce code :
- Delphi 5
- Delphi 5
- VB 2005, VB 2008, VB.NET 1.x, VB6, VBA
- C# 1.x, C# 2.x, C# 3.x
- VB 2005, VB 2008



Langage : Delphi 5
Date ajout : 22/09/2008
Posté par f0xi [Liste]
function IncludeSearchSDS(const Path: string): string;
begin
  result := path;
  if IsPathDelimiter(Result, Length(Result)) then
    Result := Result + '*.*'
  else
    Result := Result + PathDelim + '*.*';
end;
function IsEmptyDirectory(const Path: String): boolean;
var
  SR : TSearchRec;
  C  : integer;
begin
  result := true;
  C      := 0;
  if DirectoryExists(Path) then
    if SysUtils.FindFirst(IncludeSearchSDS(Path), faAnyFile, SR) = 0 then
    try
      repeat
        if (SR.Name <> '.') and (SR.Name <> '..') then
          inc(C);
      until (SysUtils.FindNext(SR) <> 0) or (C > 0);
      result := C = 0;
    finally
      SysUtils.FindClose(SR);
    end;
end;
Langage : Delphi 5
Date ajout : 24/09/2008
Posté par cirec [Liste]
Function PathIsDirectoryEmpty(pszPath: PChar):Bool; stdcall; External 'ShLwApi.dll' name 'PathIsDirectoryEmptyA';

a partir de Turbo Delphi 2006 plus la peine de déclarer la fonction 
il suffit d'ajouter l'unité 'ShLwApi' à la clause "Uses"

Langage : VB6 , VB.NET 1.x , VB 2005 , VBA , VB 2008
Date ajout : 27/09/2008
Posté par PCPT [Liste]
'déclaration API en VB6 et  VBA !! => LONG
Private Declare Function PathIsDirectoryEmpty Lib "shlwapi.dll" Alias "PathIsDirectoryEmptyA" (ByVal pszPath As StringAs Long
'déclaration API en  VB.NET !! => INTEGER
Private Declare Function PathIsDirectoryEmpty Lib "shlwapi.dll" Alias "PathIsDirectoryEmptyA" (ByVal pszPath As StringAs Integer

'même fonction  pour les différentes versions de VB, choisir la bonne  déclaration
Function IsDirectoryEmpty(ByVal sDirectory As String) As Boolean
'   le chemin peut (ou non) se terminer par un dernier  "\"
    IsDirectoryEmpty = (PathIsDirectoryEmpty(sDirectory) >  0)
End Function

Remarque :
retourne égalemet false si le dossier n'existe pas ou est incorrect, attend donc un argument valide
Langage : C# 1.x , C# 2.x , C# 3.x
Date ajout : 28/09/2008
Posté par Willi [Liste]
[DllImport("shlwapi.dll", CharSet = CharSet.Auto)] 
internal extern static bool PathIsDirectoryEmpty(string pszPath);
public bool DirectoryIsEmpty(string path) 
{
return PathIsDirectoryEmpty(path); 
}

Remarque :
Ajouter la directive
using System.Runtime.InteropServices;

Exemple:
bool bDirIsEmpty=DirectoryIsEmpty(@"c:\temp\");
Langage : VB 2005 , VB 2008
Date ajout : 16/12/2008
Posté par sturtrid [Liste]
DateMAJ : 17/12/2008
  Private Function IsFileLess(ByVal Path As String, Optional ByVal searchPattern As String = "*", Optional ByVal searchOption As System.IO.SearchOption = System.IO.SearchOption.AllDirectories) As Boolean
  ' True si pas de fichier et/ou des dossiers vides (paramètres par défaut)
  ' False sur Exceptions (Voir remarque)
    Try
      'If Not (System.IO.Directory.Exists(Path)) Then Return True
       Return (System.IO.Directory.GetFiles(Path, searchPattern, searchOption).Length = 0)
    Catch ex As Exception
       MessageBox.Show(ex.Message)
    End Try
  End Function

  Private Function IsEntryLess(ByVal Path As String, Optional ByVal searchPattern As String = Nothing) As Boolean
  ' True si pas de fichier et pas de dossier (paramètres par défaut)
  ' False sur Exceptions (Voir remarque)
    Try
      'If Not (System.IO.Directory.Exists(Path)) Then Return True
      Return (System.IO.Directory.GetFileSystemEntries(Path, searchPattern).Length = 0)
    Catch ex As Exception
      MessageBox.Show(ex.Message)
    End Try
  End Function

Remarque :
Pour obtenir True au lieu de False lorsqu'une "DirectoryNotFoundException" est levée, décommentez la ligne de code de la fonction choisie.

Snippets en rapport avec : Fichier, Vide, Dir



Codes sources en rapport avec : Fichier, Vide, Dir

{PHP} LISTE REPERTOIRE (DIR EN PHP)
Liste le contenue d'un répertoire sur serveur web et affiche icones date et taille approprié Mais ...

{Visual Basic, VB6, VB.NET, VB 2005} DIRLISTING - LISTER UN DOSSIER ET SES SOUS DOSSIERS TRES RAPIDEMENT
Cette classe vous offre un moyen simple et rapide de lister le contenu d'un repertoire. Elle est in...

{Visual Basic, VB6, VB.NET, VB 2005} SUPPRIMER LIGNES VIDES FICHIER TEXTE
Ce script supprime les lignes vides dans un fichier texte, vous pouvez parcourir les répertoires de ...

{Visual Basic, VB6, VB.NET, VB 2005} LISTEZ VOS FICHIER FACILEMENT ET RAPIDEMENT
Je pars du même constat que Jack a fait concernant sa source "REMPLACER DIR PAR UNE CLASSE DIR2 (AVA...

{C / C++ / C++.NET} [C/WIN32] EFFACER FICHIERS/RÉPERTOIRES VIDES
Le code n'apporte rien de vraiment nouveau: Utilisation de FindFirstFile et FindNextFile, recherche...

{Visual Basic, VB6, VB.NET, VB 2005} SUPPRIMER LES LIGNES VIDES DANS UN FICHIER TEXTE
Le module va supprimer les lignes vides du fichier en entrée. Le string passé en paramètre (fichier...

{Visual Basic, VB6, VB.NET, VB 2005} VÉRIFIER LA PRÉSENCE D'UN FICHIER (SIMPLE À COMPRENDRE)
Objectif : Savoir si le nom d'un fichier existe. ...

{Visual Basic, VB6, VB.NET, VB 2005} JPG + ZIP = JPG ET ZIP
Arf : ma première source (publique) en .Net ! Sur une idée de jx53 sur le forum de VBFrance, voil...

{Visual Basic, VB6, VB.NET, VB 2005} LECTURE DE FICHIER *.OFX
Lit le fichier et renvoit toutes les infos dans une structure. Tester sur les fichiers OFX de Dex...

{Delphi} ENREGISTREMENT DES DONNÉES DANS L'EXE
Alors rien de bien nouveau, comment écrire dans un Exe ? La méthode a déjà été montrée / démontrée ...