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?
 
jkrConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.