Reading Directories

Hello Friends!! I have been assigned a project to create a Thread
that copies the content of a specified Directory to a Destination(MSMQ).
I have a queue class that can read and write stuff to it. I needed
some help in constructing a thread that can read the directories contents
and move it to a destination and sleep until new files arrive in the
directory, then do the same(move it to a target).
Need help in constructing the classes and methods!!!
Thx in adv.
Pseudo Code will also do!!
veselAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
At first, as you certainly don't want to 'poll' the directories status, you might want to use a notification when the directory changees, e.g.


    HANDLE  g_hNotify;
    HANDLE  g_hFileSysWatcher;
    DWORD   g_dwTID;

    g_hNotify   =   FindFirstChangeNotification (   pszDirectoryToWatch,
                                                    TRUE, // set this to FALSE if you don't want to watch the subtree
                                                    FILE_NOTIFY_CHANGE_SIZE
                                                );


    // ...

    // Create a thread that handles 'watching'


    g_hFileSysWatcher   =   CreateThread    (   NULL,
                                                0,
                                                FileSysWatcher,
                                                g_hNotify,
                                                0,
                                                &g_dwTID
                                            );

    // ...


    Now, here's the thread that actually does the work:

int WINAPI FileSysWatcher   (   LPVOID  pv)
{
    HANDLE  hNotify;
    DWORD   dwWaitResult;

    hNotify =   ( HANDLE)   pv;

    for (   ;;)
        {
            dwWaitResult    =   WaitForSingleObject (   hNotify,
                                                        INFINITE
                                                    );

            if  (   WAIT_ABANDONED  ==  dwWaitResult)
                    return( dwWaitResult);

            DoSomethingWithFiles    ();

            FindNextChangeNotification  (   hNotify);
        };

    return( 0);
}



<continues>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
void DoSomethingWithFiles ()
{
WIN32_FIND_DATA w32fd;
HANDLE hFind;

char acFiles[MAX_PATH];

wsprintf ( acFiles, "%s\\*.*", pszDirectoryToWatch);
// don't use '\\' above if your path already ends with it!

hFind = FindFirstFile ( acFiles, &w32fd);

if ( INVALID_HANDLE_VALUE == hFind)
{
 // error
}
MoveFileToDestination ( w32fd.cFileName);

while ( FindNextFile ( hFind, &w32fd))
{
  MoveFileToDestination ( w32fd.cFileName);
}

if ( ERROR_NO_MORE_FILES != GetLastError()))
{
 // error
}
FindClose ( hFind);

}

and 'MoveFileToDestination()' could simply be

int MoveFileToDestination( char* pszFile)
{
 if ( !MoveFileEx ( pszFile, "c:\\newlocation", FALSE))
 {
   return ( GetLastError());
 }

 return ( 0);
}

Feel free to ask if you need more information!
0
veselAuthor Commented:
Dear jkr I will be right back with you after impleneting this stuff, thx a lot for the prompt reply I sincerly appreciate your help
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

veselAuthor Commented:
Dear jkr, Thx for the reply, I tested the code with few modifications! I am not using the FileMoveEx function as I wanna copy the files to a buffer rather than a disk!! but hey!! thx a lot for that suggestion and code!!!
Sincere thxs....
PS: If possible can U send me a code that moves file to buffer and the buffer is emptied...in the Queue(I have that class for Queue)
0
jkrCommented:
>>If possible can U send me a code that moves file to
>>buffer and the buffer is emptied

Will get back to this issue tomorrow - Europe here, and I'm at home meanwhile (11pm). Fine that it worked so far!

PS: You mean a memory buffer, don't you?
0
veselAuthor Commented:
Yes!!!Sorry about that I didn;t knew U were in Europe!!
0
veselAuthor Commented:
Dear JKR, Can U please help me with this!! I see that after the program completes the Files are still in the Directory. I wanna basically Move the files from the Dir to a TargetSource, so I guess I will have to delete the Dir after the Send Function, How can I accomplish that!!!
PLease reply asap

Thx
0
jkrCommented:
You don't have to delete the directory, but the files in it, e.g.

int MoveFileToDestination( char* pszFile)
{

 HANDLE hFile;
 DWORD dwSize;
 DWORD dwRead;
 char* pcBuf;

 hFile = CreateFile ( pszFile, ...);

 dwSize = GetFileSize ( hFile, NULL);

 pcBuf = new char [ dwSize + 1];

 ReadFile ( hFile, pcBuf, dwSize, &dwRead, NULL);

 // now the whole file is in a memory buffer, copy it to where you need it

 CloseHandle ( hFile);

 delete[] pcBuffer;

 // remove file
 if ( !DeleteFile ( pszFile))
 {
   return ( GetLastError());
 }

 return ( 0);
}
0
veselAuthor Commented:
Thx thx thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.