Listing the Files in a Directory (C++) doesn't work on Win 7

ruafly (3)
Follow this http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx, i've added to VC 2010 but can't list any files and some properties of them.
I try on win 7. Some one can fix this for me or give me another code. I need it :(
All i need is Listing the Files in a Directory and Properties of them (ex: size... same command "dir" in cmd.exe on windows)
This is code(Get from http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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);
}
modoran (1101)
It works fine for me if double backslashes are used in command line:
C:\Users\george\Desktop\testconsole\bin\Debug>testconsole.exe "C:\Program Files\\"

Target directory is C:\Program Files\

. <DIR>
.. <DIR>
2K Games <DIR>
Adobe <DIR>
AIMP3 <DIR>
AMD APP <DIR>
AMD AVT <DIR>
ATI <DIR>
ATI Technologies <DIR>
Bethesda Softworks <DIR>
BreakPoint Software <DIR>
CMake 2.8 <DIR>
CodeBlocks <DIR>
Common Files <DIR>
Defraggler <DIR>
desktop.ini 174 bytes
DjVu Viewer <DIR>
DVD Maker <DIR>
Elaborate Bytes <DIR>
FileZilla FTP Client <DIR>
HTML Help Workshop <DIR>
IIS <DIR>
ImgBurn <DIR>
InstallShield Installation Information <DIR
Internet Download Manager <DIR>
Internet Explorer <DIR>
Microsoft ASP.NET <DIR>
Microsoft Chart Controls <DIR>
Microsoft F# <DIR>
Microsoft Games <DIR>
Microsoft Games for Windows - LIVE <DIR>
Microsoft Help Viewer <DIR>
Microsoft SDKs <DIR>
Microsoft Security Client <DIR>
Microsoft Silverlight <DIR>
Microsoft SQL Server <DIR>
Microsoft SQL Server Compact Edition <DIR>
Microsoft Sync Framework <DIR>
Microsoft Synchronization Services <DIR>
Microsoft Visual Studio 10.0 <DIR>
Microsoft Visual Studio 9.0 <DIR>
Microsoft.NET <DIR>
Mozilla Firefox <DIR>
Mozilla Maintenance Service <DIR>
MPC-HC <DIR>
MSBuild <DIR>
NetSarang <DIR>
Notepad++ <DIR>
NVIDIA Corporation <DIR>
Opera <DIR>
phpDesigner 8 <DIR>
Realtek <DIR>
Reference Assemblies <DIR>
Say the Time <DIR>
Send To Toys <DIR>
SopCast <DIR>
Temp <DIR>
The KMPlayer <DIR>
Uninstall Information <DIR>
uTorrent <DIR>
VertrigoServ <DIR>
VideoLAN <DIR>
WinDjView <DIR>
Windows Defender <DIR>
Windows Journal <DIR>
Windows Mail <DIR>
Windows Media Player <DIR>
Windows NT <DIR>
Windows Photo Viewer <DIR>
Windows Portable Devices <DIR>
Windows Sidebar <DIR>
Windows Virtual PC <DIR>
Windows XP Mode <DIR>
WinFast <DIR>
WinRAR <DIR>
Xilisoft <DIR>
Yahoo! <DIR>

C:\Users\george\Desktop\testconsole\bin\Debug>
Last edited on
ruafly (3)
you mean i need change what in this code?
can post your way here?
thanks man!
Last edited on
ruafly (3)
i using current code (from mdsn), debug, it running and finished with this:

Usage: C:\Users\Shin\Documents\Visual Studio 2010\Projects\My First Program\Debu
g\My First Program.exe C:\Program Files\
Press any key to continue . . .

kbw (5374)
Have you tried stepping thru the code in the debugger? If not, why not?

And if you had, you'd see that it was expecting the path to traverse as a command line argument,
Topic archived. No new replies allowed.