Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Directories continued

shaolin007shaolin007 Posts: 1,018Member
[green]
OK, I got the basics down. The code below is preliminary and simple but I will streamline it when it is fully functional so bear with me.
[/green]
[code]
/*

Artist & Album sorter

Description: Search CDDVD drive for artist+album directories and
write this information to a text file.

*/
#include
#include
#include



int main ()
{
WIN32_FIND_DATAA Drive;
HANDLE file_handle;
FILE *mfile;
BOOL x;
char disc_num[4]={0};

printf("Enter the disc number please (up to 999 for now): ");
fgets(disc_num, 4, stdin);

system ("mkdir C:\Sort");

mfile=fopen("C:\Sort\Music.txt", "w+");

if (mfile==NULL)
{
printf("
Unable to open or create file!");
system("pause");
return 0;
}

file_handle=FindFirstFile("E:\*.*", &Drive);

if ( file_handle==0)
{
printf("
Unable to find directory or file!

");
system("pause");
return 0;
}

fputs(Drive.cFileName, mfile);
fputs("
--------------------", mfile);

do
{
x=FindNextFile(file_handle, &Drive);
if (x!= false)
{
fputc('
', mfile);
fputs(Drive.cFileName, mfile);
fputs("
--------------------", mfile);
}

}
while(x==true);

printf("
Disc number: %s
", disc_num);
system ("pause");
return 0;
}
[/code]
[green]
What I need suggestions on is how to do the search through the directories using a loop construct. I got it to print out all of the directories in root with no problem as shown below.
[code]
Aerosmith
--------------------
Bad Company
--------------------
Beach Boys
--------------------
Billy Joel
--------------------
Bon Jovi
--------------------
Boston
--------------------
Creedence Clearwater Revival
--------------------
Doobie Brothers
--------------------
Drivin n' Cryin
--------------------
Eric Clapton
--------------------
Genesis
--------------------
Gloria Estefan
--------------------
Heart
--------------------
Lynyrd Skynyrd
--------------------
Megadeth
--------------------
Nirvana
--------------------
[/code]
I would like to loop through the first directory, for instance Aerosmith, and all of it's subdirectories and then move on to the next directory in root, like Bad Company. What would be the best way to go about doing this? Any suggestions or critiques on my code welcomed too. Thanks for any help.
[/green]

Comments

  • stephlstephl Posts: 422Member
    : [green]
    : OK, I got the basics down. The code below is preliminary and simple but I will streamline it when it is fully functional so bear with me.
    : [/green]
    : [code]
    : /*
    :
    : Artist & Album sorter
    :
    : Description: Search CDDVD drive for artist+album directories and
    : write this information to a text file.
    :
    : */
    : #include
    : #include
    : #include
    :
    :
    :
    : int main ()
    : {
    : WIN32_FIND_DATAA Drive;
    : HANDLE file_handle;
    : FILE *mfile;
    : BOOL x;
    : char disc_num[4]={0};
    :
    : printf("Enter the disc number please (up to 999 for now): ");
    : fgets(disc_num, 4, stdin);
    :
    : system ("mkdir C:\Sort");
    :
    : mfile=fopen("C:\Sort\Music.txt", "w+");
    :
    : if (mfile==NULL)
    : {
    : printf("
    Unable to open or create file!");
    : system("pause");
    : return 0;
    : }
    :
    : file_handle=FindFirstFile("E:\*.*", &Drive);
    :
    : if ( file_handle==0)
    : {
    : printf("
    Unable to find directory or file!

    ");
    : system("pause");
    : return 0;
    : }
    :
    : fputs(Drive.cFileName, mfile);
    : fputs("
    --------------------", mfile);
    :
    : do
    : {
    : x=FindNextFile(file_handle, &Drive);
    : if (x!= false)
    : {
    : fputc('
    ', mfile);
    : fputs(Drive.cFileName, mfile);
    : fputs("
    --------------------", mfile);
    : }
    :
    : }
    : while(x==true);
    :
    : printf("
    Disc number: %s
    ", disc_num);
    : system ("pause");
    : return 0;
    : }
    : [/code]
    : [green]
    : What I need suggestions on is how to do the search through the directories using a loop construct. I got it to print out all of the directories in root with no problem as shown below.
    : [code]
    : Aerosmith
    : --------------------
    : Bad Company
    : --------------------
    : Beach Boys
    : --------------------
    : Billy Joel
    : --------------------
    : Bon Jovi
    : --------------------
    : Boston
    : --------------------
    : Creedence Clearwater Revival
    : --------------------
    : Doobie Brothers
    : --------------------
    : Drivin n' Cryin
    : --------------------
    : Eric Clapton
    : --------------------
    : Genesis
    : --------------------
    : Gloria Estefan
    : --------------------
    : Heart
    : --------------------
    : Lynyrd Skynyrd
    : --------------------
    : Megadeth
    : --------------------
    : Nirvana
    : --------------------
    : [/code]
    : I would like to loop through the first directory, for instance Aerosmith, and all of it's subdirectories and then move on to the next directory in root, like Bad Company. What would be the best way to go about doing this? Any suggestions or critiques on my code welcomed too. Thanks for any help.
    : [/green]
    :
    :
    I have already implemented such code, but without a loop to browse into the subdirectories. I use recursion, which is easier to implement. Are you interested in this recursive code, or do you really want the loop version?

    Steph
  • shaolin007shaolin007 Posts: 1,018Member
    : : [green]
    : : OK, I got the basics down. The code below is preliminary and simple but I will streamline it when it is fully functional so bear with me.
    : : [/green]
    : : [code]
    : : /*
    : :
    : : Artist & Album sorter
    : :
    : : Description: Search CDDVD drive for artist+album directories and
    : : write this information to a text file.
    : :
    : : */
    : : #include
    : : #include
    : : #include
    : :
    : :
    : :
    : : int main ()
    : : {
    : : WIN32_FIND_DATAA Drive;
    : : HANDLE file_handle;
    : : FILE *mfile;
    : : BOOL x;
    : : char disc_num[4]={0};
    : :
    : : printf("Enter the disc number please (up to 999 for now): ");
    : : fgets(disc_num, 4, stdin);
    : :
    : : system ("mkdir C:\Sort");
    : :
    : : mfile=fopen("C:\Sort\Music.txt", "w+");
    : :
    : : if (mfile==NULL)
    : : {
    : : printf("
    Unable to open or create file!");
    : : system("pause");
    : : return 0;
    : : }
    : :
    : : file_handle=FindFirstFile("E:\*.*", &Drive);
    : :
    : : if ( file_handle==0)
    : : {
    : : printf("
    Unable to find directory or file!

    ");
    : : system("pause");
    : : return 0;
    : : }
    : :
    : : fputs(Drive.cFileName, mfile);
    : : fputs("
    --------------------", mfile);
    : :
    : : do
    : : {
    : : x=FindNextFile(file_handle, &Drive);
    : : if (x!= false)
    : : {
    : : fputc('
    ', mfile);
    : : fputs(Drive.cFileName, mfile);
    : : fputs("
    --------------------", mfile);
    : : }
    : :
    : : }
    : : while(x==true);
    : :
    : : printf("
    Disc number: %s
    ", disc_num);
    : : system ("pause");
    : : return 0;
    : : }
    : : [/code]
    : : [green]
    : : What I need suggestions on is how to do the search through the directories using a loop construct. I got it to print out all of the directories in root with no problem as shown below.
    : : [code]
    : : Aerosmith
    : : --------------------
    : : Bad Company
    : : --------------------
    : : Beach Boys
    : : --------------------
    : : Billy Joel
    : : --------------------
    : : Bon Jovi
    : : --------------------
    : : Boston
    : : --------------------
    : : Creedence Clearwater Revival
    : : --------------------
    : : Doobie Brothers
    : : --------------------
    : : Drivin n' Cryin
    : : --------------------
    : : Eric Clapton
    : : --------------------
    : : Genesis
    : : --------------------
    : : Gloria Estefan
    : : --------------------
    : : Heart
    : : --------------------
    : : Lynyrd Skynyrd
    : : --------------------
    : : Megadeth
    : : --------------------
    : : Nirvana
    : : --------------------
    : : [/code]
    : : I would like to loop through the first directory, for instance Aerosmith, and all of it's subdirectories and then move on to the next directory in root, like Bad Company. What would be the best way to go about doing this? Any suggestions or critiques on my code welcomed too. Thanks for any help.
    : : [/green]
    : :
    : :
    : I have already implemented such code, but without a loop to browse into the subdirectories. I use recursion, which is easier to implement. Are you interested in this recursive code, or do you really want the loop version?
    :
    : Steph
    :
    [green]
    Sure post your example by recursion.
    [/green]

  • stephlstephl Posts: 422Member
    : : I have already implemented such code, but without a loop to browse into the subdirectories. I use recursion, which is easier to implement. Are you interested in this recursive code, or do you really want the loop version?
    : :
    : : Steph
    : :
    : [green]
    : Sure post your example by recursion.
    : [/green]
    :
    :
    Well, I've found my old code back again. I've isolated the interesting part and removed the dependencies. I've tested it quickly and it seems to work. However, if you encounter any problem, tell me.
    [code]
    #include
    #include
    #include


    void listdir(char *dir,int level)
    {
    WIN32_FIND_DATA finddata;
    HANDLE hfind;
    int i;
    char *p;

    p=dir+strlen(dir);
    strcpy(p,"\*.*");
    if ((hfind=FindFirstFile(dir,&finddata))!=INVALID_HANDLE_VALUE)
    {
    do
    if (strcmp(finddata.cFileName,".") && strcmp(finddata.cFileName,".."))
    {
    for (i=0;i
    ");
    return -1;
    }
    strcpy(buf,argv[1]);
    p=buf+strlen(buf)-1;
    if (*p=='\') *p=0;
    listdir(buf,0);
    return 0;
    }
    [/code]

    Steph
  • shaolin007shaolin007 Posts: 1,018Member
    : : : I have already implemented such code, but without a loop to browse into the subdirectories. I use recursion, which is easier to implement. Are you interested in this recursive code, or do you really want the loop version?
    : : :
    : : : Steph
    : : :
    : : [green]
    : : Sure post your example by recursion.
    : : [/green]
    : :
    : :
    : Well, I've found my old code back again. I've isolated the interesting part and removed the dependencies. I've tested it quickly and it seems to work. However, if you encounter any problem, tell me.
    : [code]
    : #include
    : #include
    : #include
    :
    :
    : void listdir(char *dir,int level)
    : {
    : WIN32_FIND_DATA finddata;
    : HANDLE hfind;
    : int i;
    : char *p;
    :
    : p=dir+strlen(dir);
    : strcpy(p,"\*.*");
    : if ((hfind=FindFirstFile(dir,&finddata))!=INVALID_HANDLE_VALUE)
    : {
    : do
    : if (strcmp(finddata.cFileName,".") && strcmp(finddata.cFileName,".."))
    : {
    : for (i=0;i
    ");
    : return -1;
    : }
    : strcpy(buf,argv[1]);
    : p=buf+strlen(buf)-1;
    : if (*p=='\') *p=0;
    : listdir(buf,0);
    : return 0;
    : }
    : [/code]
    :
    : Steph
    :
    [green]
    Ok, I tried it and it works but not in the way that I want. It does give me some excellent ideas though on how to proceed now. Thanks for the help steph! :-)
    [/green]

Sign In or Register to comment.