Programación General > C/C++

 Como Creo Una Lista De Archivos De Una Carpeta?

(1/2) > >>

plaf:
eso, como puedo meterme a una carpeta y agarrar una lista de los archivos q tiene? algo asi como un "dir" pero guardando el output. no importa como quede, despues se arregla...
la idea es leer todos los archivos (1 a 1) *.algo de una carpeta, pero estos no siguen un patron en el nombre, son cualquier cosa

Ruben3d:
Hola.

Te pego un pograma sacado de MSDN, cuya misión es listar todo el contenido de un directorio, entrando recursivamente en los subdirectorios. No creo que tengas que modificarlo mucho para adaptarlo a tus necesidades.


--- Código: Text --- #include <windows.h>#include <stdio.h> void ScanDir(char *dirname, int indent){    BOOL            fFinished;    HANDLE          hList;    TCHAR           szDir[MAX_PATH+1];    TCHAR           szSubDir[MAX_PATH+1];    WIN32_FIND_DATA FileData;     // Get the proper directory path    sprintf(szDir, "%s\\*", dirname);     // Get the first file    hList = FindFirstFile(szDir, &FileData);    if (hList == INVALID_HANDLE_VALUE)    {         printf("No files found\n\n");    }    else    {        // Traverse through the directory structure        fFinished = FALSE;        while (!fFinished)        {            // Check the object is a directory or not            if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)            {                if ((strcmp(FileData.cFileName, ".") != 0) &&(strcmp(FileData.cFileName, "..") != 0))                {                    printf("%*s%s\\\n", indent, "",                      FileData.cFileName);                     // Get the full path for sub directory                    sprintf(szSubDir, "%s\\%s", dirname,                      FileData.cFileName);                     ScanDir(szSubDir, indent + 4);                }            }            else                printf("%*s%s\n", indent, "", FileData.cFileName);              if (!FindNextFile(hList, &FileData))            {                if (GetLastError() == ERROR_NO_MORE_FILES)                {                    fFinished = TRUE;                }            }        }    }     FindClose(hList);} void main(int argc, char *argv[]){    char *pszInputPath;    char pwd[2] = ".";     if (argc < 2)    {        printf("Argument not supplied - using current directory.\n");        pszInputPath = pwd;    }    else    {        pszInputPath = argv[1];        printf("Input Path: %s\n\n", pszInputPath);    }     ScanDir(pszInputPath, 0);     printf("\ndone.\n");}    

Un saludo.

Ruben3d

QliX=D!:
Si te intereza hacerlo sin la api de windows existen las funciones diropen (U opendir?) para moverte dentro del listado..

slds.

Ruben3d:
Hola.

La función que mencionas es opendir(), pero es específica de sistemas basados en unix.

Un saludo.

Ruben3d

plaf:
gracias a los 2, ya me funciono :)
busque el opendir() en msdn y... chachan! encontre el ejemplo q puso ruben3d :P

por si alguien tb quiere hacer esto, le regalo el codigo... no es la gran cosa, es un vil copypastel recortado, pero igual funca :)


--- Código: Text ---#include <windows.h>#include <stdio.h>#include <stdlib.h> int termina(char *palabra, char *fin){   int i,p,f;   p=strlen(palabra);   f=strlen(fin);   for(i=1;i<=f;i++) if(palabra[p-i] != fin[f-i]) return 0;   return 1;} void ScanDir(){   BOOL            fFinished;   HANDLE          hList;   WIN32_FIND_DATA FileData;    hList = FindFirstFile(".\\*", &FileData);   fFinished = FALSE;      while (!fFinished)   {       // Check the object is a directory or not       if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && termina(FileData.cFileName,".cpp"))           printf("%s\n",FileData.cFileName);        if (!FindNextFile(hList, &FileData))       {           if (GetLastError() == ERROR_NO_MORE_FILES)           {               fFinished = TRUE;           }       }   }    FindClose(hList);} int main(int argc, char *argv[]){   ScanDir();    printf("\ndone.\n");   system("pause");} 

Navegación

[0] Índice de Mensajes

[#] Página Siguiente

Ir a la versión completa