Guest User

Directory management

a guest
Sep 4th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5.  
  6.  
  7.  
  8.  
  9. int main ( int argc, char *argv[] )
  10. {
  11.   DIR *pd; // Puntero a fichero directorio
  12.   struct dirent *dirdatos; // Información sobre una entrada en el directorio
  13.   struct stat    statdatos; // Información sobre el fichero
  14.   int numf;  /* Nº de entradas en el directorio */
  15.   int total; /* Tamaño total del los archivos  */
  16.   char nombreycamino [200]; /* Nombre completo directorio y archivo */
  17.  
  18.  
  19.   if ( argc != 2 )
  20.      {
  21.      fprintf(stderr,"Nº de parámetros erróneo\n");
  22.      fprintf(stderr,"Uso %s <Directorio> \n",argv[0]);
  23.      return EXIT_FAILURE ;
  24.      }
  25.  
  26.   pd = opendir ( argv[1] );
  27.  
  28.  if ( pd == NULL )
  29.     {
  30.     perror ( argv[1] );
  31.     exit (1 );
  32.     }
  33.   numf = 0;
  34.   total = 0;
  35.   dirdatos = readdir ( pd );
  36.   while ( dirdatos != NULL  )
  37.    {
  38.    numf++;
  39.    sprintf(nombreycamino,"%s\\%s",argv[1],dirdatos->d_name);
  40.    // Información sobre el fichero
  41.    if ( stat ( nombreycamino, & statdatos ) == 0)
  42.       {
  43.        printf ( "%2d> %-20.20s \t Tamaño:%10d \n",
  44.              numf,
  45.              dirdatos->d_name,
  46.              statdatos.st_size );
  47.       total += statdatos.st_size;
  48.       }
  49.    dirdatos = readdir (pd );
  50.   }
  51.  
  52.  printf (" %d bytes en %d archivos.\n", total, numf );
  53.  closedir ( pd );
  54.  return EXIT_SUCCESS;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment