Solved

Directory browsing with MS C 2008

Posted on 2011-09-14
6
343 Views
Last Modified: 2012-05-12
Dear experts!

I’m programmer with 10+ years in industry, but totally new to MS C 2008 and I need help.

I have a variable
 LPCTSTR sPicFile=”C:\Temp\Pic001.tif”

I need a code which to search directory for files like
C:\Temp\Pic002.tif C:\Temp\Pic003.tif and so on up to C:\Temp\Pic999.tif

Please note that some of the pic may be missing, however I should put it in correct order in let say LPCTSTR[] sPic
0
Comment
Question by:dvplayltd
6 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36541119
LPCTSTR sPicFile= L”C:\Temp\Pic001.tif”;
or  LPCTSTR sPicFile=_T(”C:\Temp\Pic001.tif”);
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36541123
0
 

Author Comment

by:dvplayltd
ID: 36541231
Look that you do not understand me at all

I have this
LPCTSTR sPicFile= L”C:\Temp\Pic001.tif”;
 
I need code to check for:
sPicFile= L”C:\Temp\Pic002.tif”; - does the file extis in HDD - if yes - add to thearray
sPicFile= L”C:\Temp\Pic002.tif”; - does the file extis in HDD - if yes - add to thearray
sPicFile= L”C:\Temp\Pic004.tif”; - does the file extis in HDD - if No - do nothing, then check for
sPicFile= L”C:\Temp\Pic005.tif”; - does the file extis in HDD - if yes - add to thearray
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 36541803
first you need to change the backslash into two backslashes or the path would not be found:

LPCTSTR sPicFile= L”C:\\Temp\\Pic001.tif”;

the \  is escape character in c.

to check for existence of a file you can use stat function:

#include <sys/stat.h>
...

struct stat fileinfo;
if (stat("c:\\temp\\pic001.tif", &fileinfo) == 0)
{
     // file exists !!!
}

Open in new window


Sara
0
 
LVL 86

Expert Comment

by:jkr
ID: 36542938
The following should be helpful and works with  both ANSI and UNICODE: http://msdn.microsoft.com/en-us/library/aa365200%28v=VS.85%29.aspx ("Listing the Files in a Directory")
#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);
}

Open in new window

0
 

Author Closing Comment

by:dvplayltd
ID: 36548811
10x
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What technology should I use for communication Raspberry Pi 2b and LapTop java application 2 124
Find Visual Studio Tools 2 99
C++ question 3 65
Unable to start eclipse ? 17 141
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now