list files and subfolders in directory with shurtcut path - Help

This is a discussion on list files and subfolders in directory with shurtcut path - Help within the C++ Programming forums, part of the General Programming Boards category; Hello, this is simple code to show list of files and sub-directories in folder with command line.. it so good. ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    34

    Lightbulb list files and subfolders in directory with shurtcut path - Help

    Hello,
    this is simple code to show list of files and sub-directories in folder with command line.. it so good.

    Usage: test.exe <directory name>
    Code:
     test.exe c:\
    the out put:
    Code:
     Target directory is c:\
     
       PAGEFILE.SYS   792723456 bytes
       WINDOWS   <DIR>
       ntldr   250032 bytes
       NTDETECT.COM   47564 bytes
       boot.ini   211 bytes
       Documents and Settings   <DIR>
       Program Files   <DIR>
       CONFIG.SYS   0 bytes
       AUTOEXEC.BAT   0 bytes
       IO.SYS   0 bytes
       MSDOS.SYS   0 bytes
       System Volume Information   <DIR>
       Dev-Cpp   <DIR>
       Intel   <DIR>
       Recycled   <DIR>
       MSOCache   <DIR>
       dfinstall.log   0 bytes
       Persi0.sys   7372288 bytes
    this nice but... i can't view directories that have spaces in path like:
    Code:
     test.exe c:\Documents and Settings
    it returns to command usage..
    the out put:
    Code:
     Usage: test.exe <directory name>
    i want to use it like dos command:
    Code:
      dir c:\docume*
    to be like this:
    Code:
      test.exe c:\docume*


    this is full code:
    Code:
     #include <windows.h>
     #include <tchar.h> 
     #include <stdio.h>
     #include <strsafe.h>
     #pragma comment(lib, "User32.lib")
     
     void DisplayErrorBox(LPTSTR lpszFunction);
     
     int _tmain(int argc, TCHAR *argv[])
     {
        WIN32_FIND_DATA ffd;
        LARGE_INTEGER filesize;
        TCHAR szDir[MAX_PATH];
        size_t length_of_arg;
        HANDLE hFind = INVALID_HANDLE_VALUE;
        DWORD dwError=0;
        
        // If the directory is not specified as a command-line argument,
        // print usage.
     
        if(argc != 2)
        {
           _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
           return (-1);
        }
     
        // Check that the input path plus 3 is not longer than MAX_PATH.
        // Three characters are for the "\*" plus NULL appended below.
     
        StringCchLength(argv[1], MAX_PATH, &length_of_arg);
     
        if (length_of_arg > (MAX_PATH - 3))
        {
           _tprintf(TEXT("\nDirectory path is too long.\n"));
           return (-1);
        }
     
        _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);
     
        // Prepare string for use with FindFile functions.  First, copy the
        // string to a buffer, then append '\*' to the directory name.
     
        StringCchCopy(szDir, MAX_PATH, argv[1]);
        StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
     
        // Find the first file in the directory.
     
        hFind = FindFirstFile(szDir, &ffd);
     
        if (INVALID_HANDLE_VALUE == hFind) 
        {
           DisplayErrorBox(TEXT("FindFirstFile"));
           return dwError;
        } 
        
        // List all the files in the directory with some info about them.
     
        do
        {
           if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
           {
              _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
           }
           else
           {
              filesize.LowPart = ffd.nFileSizeLow;
              filesize.HighPart = ffd.nFileSizeHigh;
              _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
           }
        }
        while (FindNextFile(hFind, &ffd) != 0);
      
        dwError = GetLastError();
        if (dwError != ERROR_NO_MORE_FILES) 
        {
           DisplayErrorBox(TEXT("FindFirstFile"));
        }
     
        FindClose(hFind);
        return dwError;
     }
     
     
     void DisplayErrorBox(LPTSTR lpszFunction) 
     { 
         // Retrieve the system error message for the last-error code
     
         LPVOID lpMsgBuf;
         LPVOID lpDisplayBuf;
         DWORD dw = GetLastError(); 
     
         FormatMessage(
             FORMAT_MESSAGE_ALLOCATE_BUFFER | 
             FORMAT_MESSAGE_FROM_SYSTEM |
             FORMAT_MESSAGE_IGNORE_INSERTS,
             NULL,
             dw,
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
             (LPTSTR) &lpMsgBuf,
             0, NULL );
     
         // Display the error message and clean up
     
         lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
             (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
         StringCchPrintf((LPTSTR)lpDisplayBuf, 
             LocalSize(lpDisplayBuf) / sizeof(TCHAR),
             TEXT("%s failed with error %d: %s"), 
             lpszFunction, dw, lpMsgBuf); 
         MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
     
         LocalFree(lpMsgBuf);
         LocalFree(lpDisplayBuf);
     }

    help please

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,294
    try
    Code:
    test.exe "c:\Documents and Settings"

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    Elkvis yo yo
    you gotta it.. it's working dear

    Thaaaaaaaaaaaaaaaaaaaaaaaaaaaanx soo much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-12-2012, 06:04 AM
  2. c program to print list of files in a directory
    By leo_1024 in forum Linux Programming
    Replies: 3
    Last Post: 01-24-2009, 04:54 AM
  3. List files in a directory (without folders)
    By 3saul in forum Linux Programming
    Replies: 4
    Last Post: 04-20-2006, 01:08 AM
  4. list size of files in directory
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 02-27-2005, 10:52 PM
  5. Who knows a function that list a path immediate files list ?
    By rami_gerassi in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 01:17 AM

Tags for this Thread


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