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]
Vue : 1719
Catégorie(s) : Fichier / Disque, Trucs & Astuces
Langages dispo pour ce code :
- VB 2005, VB 2008
- Delphi 5
- Delphi 5
- VB 2005, VB 2008, VB.NET 1.x, VB6, VBA
- C# 1.x, C# 2.x, C# 3.x



Langage : VB 2005 , VB 2008
Date ajout : 12/09/2008
Posté par gillardg [Liste]

''' <summary>

''' dirpath est le path à tester

''' si vide return true

''' </summary>

''' <param name="dirpath"></param>

''' <returns>boolean</returns>

''' <remarks></remarks>

Function nofileindir(ByVal dirpath As String) As Boolean
Dim x As Integer = 0 
Try
For Each file As String In My.Computer.FileSystem.GetFiles(dirpath, FileIO.SearchOption.SearchAllSubDirectories, "*.*") 
If x = 0 Then

If IO.File.Exists(file) Then

Return False

Else

Return True

End If

End If

x = x + 1

Next
Catch ex As Exception 
MsgBox(ex.Message)

End Try

If x = 0 Then

Return True

End If

End Function

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\");

Snippets en rapport avec : Fichier, Vide, Dir



Codes sources en rapport avec : Fichier, Vide, Dir

{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. ...

{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...

{C / C++ / C++.NET} LISTER LES FICHIERS D'UN REPERTOIRE + FILTRES
Programmé sous Linux. Compatible windows. Liste les fichiers d'un répertoire come indiqué dans le...

{C / C++ / C++.NET} PROTEGER UN DOSSIER ET LES FICHIER A L INTERIEUR
protégé un dossier et les fichier intérieur en renommant le dossier sous le nom de, au hasard ...

{Visual Basic, VB6, VB.NET, VB 2005} LISTER FICHIERS D'UN DOSSIER
ce p'tit code permet de lister les fichiers d'un dossier choisi en plaçant leur noms, leur type et l...