directory and file listing

This is a discussion on directory and file listing within the C Programming forums, part of the General Programming Boards category; Hello, i have a problem. I would like to print out everything that is on a hard drive. Like c: ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36

    directory and file listing

    Hello,

    i have a problem. I would like to print out everything that is on a hard drive. Like c: or d: etc...

    For now i came up with this:

    Code:
    #include <stdarg.h>
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    
    
    void IzpisiDir(char Pot[])
    
    {
    	
    	WIN32_FIND_DATA podatki;
    	HANDLE rocka;
    	char tmpPot[MAX_PATH];
    	char tmp[MAX_PATH];
    	char zvezda[]="*.*";
    	
    	
    	strcpy(tmpPot, Pot);
    	strcat(tmpPot, zvezda);
    	
    	
    	rocka = FindFirstFile(tmpPot,&podatki);
    	
    	if(rocka==INVALID_HANDLE_VALUE)  
    		return; 
    	
    	do
    	{
    		
    		if(podatki.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			printf("%s", podatki.cFileName);		  
    			//if((0 != strcmp(podatki.cFileName,".")) && (0 != strcmp(podatki.cFileName,"..")));
    			strcpy(tmpPot, Pot);
    			strcat(tmpPot, podatki.cFileName);
    			strcat(tmpPot,"\\");
    			strcat(tmpPot, zvezda);
    			
    		}
    		else
    			printf(" %s\n",podatki.cFileName);
    		
    	}  
        while(FindNextFile(rocka,&podatki));  
    	
    	
    	FindClose(rocka);
    	
    	   
    }
    
    int main()
    {
    	
    	IzpisiDir("C:\\");
    	
    }
    I just don't know how to print out a folder go into that folder print out everything that is in there and then go back out of the folder and move on to the next one like:

    temp (directory)
    test.ini
    test01.ini
    cboard(directory)
    cboard.doc
    cboard.pdf
    ... and so on

    I just print out some of the folders and files.

    Can somebody give me some pointer please. I've been doing this for 4 days now and i don't have any idea or energy to go on.

    Thank you in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    There's a directory tree walker in the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Thanks Salem, i did it with the help of the FAQ.

    I still have a question. Lets say i would need to find a txt file in the directory structure?

    My code is listing everything. So i already have file names but how can i compare them so that i would find a text file (example: test.txt). I think only the txt part is important but i just cant figure out how to look for that?

    Is there any function like strncmp? With the n in strncmp you can assign how many places(characters) would you like to compare the string. But that is from the start of the string.

    Is there any function that would compare the last 3 characters in a string?

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by bnkslo View Post
    Is there any function that would compare the last 3 characters in a string?
    There's not such function in the standard C library (i'd say the closest thing to what you are looking for is the strstr() function). Of course, nothing stops you from writing your own function.
    I hate real numbers.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Location
    tampa
    Posts
    2
    You could also save yourself some trouble and pass the filter you're looking for into FindFirstFile, ie. *.txt or filename.txt

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is there any function like strncmp?
    Yes, and you guessed the name. So using strncmp() and strrchr(), you could do something like:
    Code:
    if (strncmp(strrchr(filename,'.')+1, "txt", 3) == 0)
    Although you probably want to do it in two stages so you can check the return value of strrchr() for NULL.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Quick and dirty extension comparison:
    Code:
    int has_extension(const char * const filename, const char * const extension)
    {
        // Get the last . in the filename.
        const char *p = strrchr(filename, '.');
        // _stricmp is a Microsoft-specific implementation of a case-
        // insensitive string comparison. For more info see:
        // http://msdn.microsoft.com/en-us/library/k59z8dwe(VS.80).aspx
        return (p && (0 == _stricmp(p, extension));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21