CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Thumbs up Need a simple program!

    Hi all,

    It's been some time since I last touched programming, but if anyone is free, here's a simple request which I'm sure most programmers can churn out in 5 mins or less.

    Basically, I need a program that will loop through a folder and get the names of all the files in that folder, and output them in a list box or text box.

    To make things even simpler, all the user need is to place the program in the same folder, click a button and and it will loop through all the files and get the file names etc.

    Or if anyone has a link to a program like that, let me know as well.

    Thanks!
    Xeon.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  2. #2
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189

    Re: Need a simple program!

    Your right - it's simple. I can guide you through it in 6 easy steps, and you don't have to even fire up your favorite code generator....

    Please note that these instructions are tailored for Windows users...

    1) Open a command window
    2) Navigate to the desired folder
    3) type 'dir > filename.txt' (insert the filename of your choice here)
    4) hit Enter
    5) type 'notepad filename.txt' (same filename as above)
    6) View the list you just created in all it's glory in the Notepad textbox.

    If you really want to get fancy, you can add a 7th step, and copy the above steps into a batch file :-)


    Enjoy!!

  3. #3
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Need a simple program!

    Here's how you find files in the directory that this .exe resides in:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char** argv)
    {
       CHAR szDir[MAX_PATH];
       GetCurrentDirectoryA(sizeof(szDir), szDir);
       printf("Path: %s\n\n", szDir);
       strcat(szDir, "\\*");
    
       WIN32_FIND_DATA ffd;
       HANDLE hFind = FindFirstFile(szDir, &ffd);
    
       do
       {
          if(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
          {
             LARGE_INTEGER filesize;
             filesize.LowPart = ffd.nFileSizeLow;
             filesize.HighPart = ffd.nFileSizeHigh;
             printf("Name: %s\t\tSize: %ld\n", ffd.cFileName, filesize.QuadPart);
          }
       }
       while(FindNextFile(hFind, &ffd) != 0);
    
       FindClose(hFind);
       puts("\nOperation Complete.");
       getchar();
       return 0;
    }

  4. #4
    Join Date
    May 2007
    Posts
    437

    Re: Need a simple program!

    Quote Originally Posted by Xeon
    Hi all,

    It's been some time since I last touched programming, but if anyone is free, here's a simple request which I'm sure most programmers can churn out in 5 mins or less.

    Basically, I need a program that will loop through a folder and get the names of all the files in that folder, and output them in a list box or text box.

    To make things even simpler, all the user need is to place the program in the same folder, click a button and and it will loop through all the files and get the file names etc.

    Or if anyone has a link to a program like that, let me know as well.

    Thanks!
    Xeon.
    Listing the Files in a Directory

    http://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx
    ashu
    always use code tag

  5. #5
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Lightbulb Re: Need a simple program!

    Thanks a lot, guys! Actually, this program is for a friend of mine who wants to list the file names of all the videos in his folder (he has hundreds of videos), and he needs to send the list to me.

    The steps given by gjs is very good, but even I myself is stuck at that cos' I've never really touched MS-DOS in my life, same as my friend.

    So, if anyone could generate a small simple program doing the above and attach it to this thread, I would be grateful! (specs: looping through the folder, getting all the file names and putting them in text box for easy copying)

    I wanted to copy ashukasama and Bitshifter's code to create my own program yesterday, but I found my VC++ 6.0 software was missing since 2 years ago!

    And this is not school homework! View my past posts and you'll see I'm some sort of half-boiled shareware programmer at one time.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Need a simple program!

    Just do what gjs368 suggested. It'll take 30 seconds. No program needed.

  7. #7
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189

    Re: Need a simple program!

    Xeon,
    It's obvious that you can type...
    And as mentioned above, this is really NOT that complicated. The hardest part of it is the same no matter.. You have to navigate to the folder that you want to list. If you want it simpler than typing the path, well then, just open up Windows Explorer and navigate to the folder that you want a listing from with your mouse, copy the FULL directory listing from the address bar to the Clipboard, and paste it into a command window for step 2 of my original post (at the cursor, type in the two letters cd <change directory> then a space, and then right click in the window to paste the address you copied in from the Explorer address bar).

  8. #8
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Need a simple program!

    Thanks a lot, guys.

    My problem is that when I open up the DOS window, the directory path is at:

    C:\Documents and Settings\Xeon >

    The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"

    So, I tried to type the following:

    C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder

    But it gives an error saying "paramete format not correct".
    I've also tried putting quotes around my paths but it still doesn't work.

    Thanks!
    Xeon
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need a simple program!

    Quote Originally Posted by Xeon
    Thanks a lot, guys! Actually, this program is for a friend of mine who wants to list the file names of all the videos in his folder (he has hundreds of videos), and he needs to send the list to me.

    The steps given by gjs is very good, but even I myself is stuck at that cos' I've never really touched MS-DOS in my life, same as my friend.
    The command-line is not MS-DOS. MS-DOS is the old 16-bit operating system that was popular during the 1980's and early 1990's.

    But in all seriousness, any self-respecting Windows programmer should know how to use the command-line. There really is no excuse for a programmer not knowing simple commands such as "dir", "copy", "cd", etc.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Need a simple program!

    As gjs368 said, put this code into into a file then give it a .bat extension.
    When you double click on the batch file it will spit the results into a text file.
    This searches the current directory for all file types and sub-directories.
    It doesn't get any easier than this...
    Code:
    @echo off
    dir > results.txt
    pause

  11. #11
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189

    Re: Need a simple program!

    Bitshifter, using GetCurrentDirectoryA can come back to bite you....
    Quote Originally Posted by bitshifter420
    Here's how you find files in the directory that this .exe resides in:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char** argv)
    {
       CHAR szDir[MAX_PATH];
       GetCurrentDirectoryA(sizeof(szDir), szDir);
       printf("Path: %s\n\n", szDir);
       strcat(szDir, "\\*");
    
       WIN32_FIND_DATA ffd;
       HANDLE hFind = FindFirstFile(szDir, &ffd);
    
       do
       {
          if(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
          {
             LARGE_INTEGER filesize;
             filesize.LowPart = ffd.nFileSizeLow;
             filesize.HighPart = ffd.nFileSizeHigh;
             printf("Name: %s\t\tSize: %ld\n", ffd.cFileName, filesize.QuadPart);
          }
       }
       while(FindNextFile(hFind, &ffd) != 0);
    
       FindClose(hFind);
       puts("\nOperation Complete.");
       getchar();
       return 0;
    }
    Note what the function returns:
    GetCurrentDirectoryA( )
    This function returns the current working directory into a string
    The current working directory may not be the one your app launched from. Here is a VC++ 6.0 function that should ALWAYS get you the directory the application resides in:
    Code:
    void GetAppDirectory(CString& szDirectory) 
    { 
            TCHAR           szPath[(MAX_PATH + 1)]; 
            TCHAR      *szTemp            = NULL; 
    
            GetModuleFileName(NULL, szPath, (sizeof(TCHAR) * MAX_PATH)); 
    
            szTemp = strrchr(szPath, _TCHAR('\\')); 
            if (szTemp) *szTemp = NULL; 
    
        szDirectory.Format(_T("%s"), szPath); 
    }

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need a simple program!

    Quote Originally Posted by Xeon
    Thanks a lot, guys.

    My problem is that when I open up the DOS window, the directory path is at:

    C:\Documents and Settings\Xeon >

    The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"

    So, I tried to type the following:

    C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder

    But it gives an error saying "paramete format not correct".
    I've also tried putting quotes around my paths but it still doesn't work.
    We can't help you directly, since we don't have access to your computer. We don't know if that directory really exists. We don't know if you're really typing in that command-line. We don't know anything -- this needs to be diagnosed by you by first knowing how to use the cd command properly.

    Can you change to the root directory?
    Code:
    cd \
    If you can, then go up one folder at a time:
    Code:
    cd Documents and Settings
    If this takes you to the directory, then go up another level. If it doesn't then your directory is not named "Documents and Settings", or it is hidden and you can't get access to it, or some other error we can't diagnose from our end.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Need a simple program!

    Quote Originally Posted by gjs368
    Bitshifter, using GetCurrentDirectoryA can come back to bite you....
    Yes, that's not my normal practice, i was trying to keep the code small.
    I should have supplied a function that strips the name off of argv[0].
    Anyway, thanks for the constructive criticism.

  14. #14
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189

    Re: Need a simple program!

    Quote Originally Posted by Xeon
    Thanks a lot, guys.

    My problem is that when I open up the DOS window, the directory path is at:

    C:\Documents and Settings\Xeon >

    The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"

    So, I tried to type the following:

    C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder

    But it gives an error saying "paramete format not correct".
    I've also tried putting quotes around my paths but it still doesn't work.

    Thanks!
    Xeon
    I have mislead you a bit, sorry... The command line does not know how to deal with the spaces (Just like MS to go half-way by letting you use spaces and then have grief over trying to get there....) unless you enclose the path in quotes... Try this:
    cd "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"

  15. #15
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Need a simple program!

    Thanks guys!

    But my main problem is with the "change directory", as follows (posted above previously):
    When I open up the DOS window, the directory path is at:

    C:\Documents and Settings\Xeon >

    The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"

    So, I tried to type the following:

    C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder

    But it gives an error saying "paramete format not correct".
    I've also tried putting quotes around my paths but it still doesn't work.
    I'm sure if I can get around this, everything would be smooth.

    And yeah, it's not surprising for noob programmers not to know how to use these command-line stuff if all they ever did was MFC visual stuff all done in Windows 98 / XP interface.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured