[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

I need to check whether some files in 1 folder is locked or not and I want to log the locking situation in a time frame. How to?

Hi there;

I need to check whether some files in 1 folder is locked or not and I want to log the locking situation in a time frame. How to?

C, C# or Java are the options, I just need a simple program that logs the locks of the files in a folder. Any example codes?

Kind regards.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 16
  • 13
  • 3
2 Solutions
 
CEHJCommented:
You don't mention your OS. That is key. On Windows, Java file locking is only advisory. Other processes can and do ignore a file lock got through Java
0
 
jazzIIIloveAuthor Commented:
Windows 7-64 bit.
0
 
CEHJCommented:
You'd probably be better using C# interop then
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jazzIIIloveAuthor Commented:
sure any example code?

Regards.
0
 
jkrCommented:
So, you basically want a 'snapshot' information regarding if these files are locked at a certain moment? If so, try to lock them - if that fails, they are already locked.
0
 
jazzIIIloveAuthor Commented:
Yes, good way jkr:

>>So, you basically want a 'snapshot' information regarding if these files are locked at a certain moment? >>If so, try to lock them - if that fails, they are already locked.

Ok, but how frequent can I do the checking? Can you provide me a code?

Regards.
0
 
jkrCommented:
http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx ("Listing the Files in a Directory") pretty much would provide the outline - you can extend that to
#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)
      {
         continue; // skip directories
      }
      else
      {
         TCHAR acFullPath[MAX_PATH];

         // build full path

         StringCchCopy(acFullPath, MAX_PATH, argv[1]);
         StringCchCat(acFullPath, MAX_PATH, TEXT("\\"));
         StringCchCat(acFullPath, MAX_PATH, ffd.cFileName);

         HANDLE hFile = CreateFile(acFullPath,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

         if (INVALID_HANDLE_VALUE == hFile)
         {
            _tprintf(TEXT("\nCannot determine lock status of %s\n\n"), acFullPath);
            continue;
         }

         DWORD dwHi,dwLow;

         dwLow = GetFileSize(hFile,&dwHi);

         if (!LockFile(hFile,0,0,dwLow,dwHi))
         {
           if (ERROR_LOCK_VIOLATION == GetLastError())
             _tprintf(TEXT("\nCannot fully lock %s, file is locked by another process\n\n"), acFullPath);
            else _tprintf(TEXT("\nCannot fully lock %s, file seems to have a lock on it\n\n"), acFullPath);
         }
         else
         {
            _tprintf(TEXT("\n%s is not locked\n\n"), acFullPath);

            UnockFile(hFile,0,0,dwLow,dwHi);
         }

         CloseHandle(hFile);
      }
   }
   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
 
jazzIIIloveAuthor Commented:
Hi there,

This is working but I couldn't find a way to adapt the code to check for lock for the files in every second.

Kind regards.
0
 
jazzIIIloveAuthor Commented:
In fact, this is what I got for the file;

Cannot determine lock status

Btw, I am passing the directory path as an input parameter, is it OK?

Kind regards.
0
 
jkrCommented:
Yes, that should be OK, can you change that to the following and post the output?
if (INVALID_HANDLE_VALUE == hFile)
         {
            _tprintf(TEXT("\nCannot determine lock status of %s - reason: %d\n\n"), acFullPath, GetLastError());
            continue;
         }

Open in new window

0
 
jazzIIIloveAuthor Commented:
What about 5000 ms. interval for the check?

and the crucial question; when I check for the lock, am i creating a lock for the file?

Kind regards.
0
 
jazzIIIloveAuthor Commented:
The output is as
reason 32
0
 
jkrCommented:
That's better than I thought - 32 is

  ERROR_SHARING_VIOLATION
# The process cannot access the file because it is being used
# by another process.

So that means 'openened exclusively', but not locked. This can be take care of by using
HANDLE hFile = CreateFile(acFullPath,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

         if (INVALID_HANDLE_VALUE == hFile)
         {
            if ( ERROR_SHARING_VIOLATION == GetLastError()) _tprintf(TEXT("\n%s is opened exclusively by another process\n\n"), acFullPath);
            continue;
         }

         if (INVALID_HANDLE_VALUE == hFile)
         {
            _tprintf(TEXT("\nCannot determine lock status of %s\n\n"), acFullPath);
            continue;
         }

Open in new window

0
 
jkrCommented:
Ooops, make that
HANDLE hFile = CreateFile(acFullPath,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

         if (INVALID_HANDLE_VALUE == hFile)
         {
            if ( ERROR_SHARING_VIOLATION == GetLastError())
            { 
              _tprintf(TEXT("\n%s is opened exclusively by another process\n\n"), acFullPath);
              continue;
            }
             
            _tprintf(TEXT("\nCannot determine lock status of %s\n\n"), acFullPath);
            continue;
         }

Open in new window

0
 
jkrCommented:
Sorry, missed that part:

>>What about 5000 ms. interval for the check?

Sounds OK Just call that function every 5s

>>and the crucial question; when I check for the lock, am i creating a lock for
>>the file?

Not if it is opened exclusively as above - that is different to locking.
0
 
jazzIIIloveAuthor Commented:
ok, that's OK

>># The process cannot access the file because it is being used
Meaning read or write?

time interval? I try to surround with while(true) that the application should run continuously to check but failed.

>>What about 5000 ms. interval for the check?

>>and the crucial question; when I check for the lock, am i creating a lock for the file?

Apparently, in C# version of this question I am locking the file itself, but I can loop it in that

Kind regards.


0
 
jazzIIIloveAuthor Commented:
You mean LockFile in if (!LockFile(hFile,0,0,dwLow,dwHi))?

Kind regards.
0
 
jkrCommented:
'LockFile()' won't even be reached if the file is opened exclusively.

You can achieve the 5000ms interval by using
while(1) {

  CheckLocks("c:\\path");

  Sleep(5000);
}

Open in new window

0
 
jazzIIIloveAuthor Commented:
Ah you will be mad;

but I couldn't find where to put the infiinte loop, moreover what should I put to the checkLocks file?

Should I take the main content to that checkLocks file?

Regards.
0
 
jkrCommented:
>>Should I take the main content to that checkLocks file?

Yes, that would be the idea, e.g.
#include <windows.h>
#include <tchar.h> 
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")

void DisplayErrorBox(LPTSTR lpszFunction);

int CheckLocks(TCHAR* pPath )
{
   WIN32_FIND_DATA ffd;
   LARGE_INTEGER filesize;
   TCHAR szDir[MAX_PATH];
   size_t length_of_arg;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   DWORD dwError=0;
   

   // Check that the input path plus 3 is not longer than MAX_PATH.
   // Three characters are for the "\*" plus NULL appended below.

   StringCchLength(pPath, 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"), pPath);

   // Prepare string for use with FindFile functions.  First, copy the
   // string to a buffer, then append '\*' to the directory name.

   StringCchCopy(szDir, MAX_PATH, pPath);
   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)
      {
         continue; // skip directories
      }
      else
      {
         TCHAR acFullPath[MAX_PATH];

         // build full path

         StringCchCopy(acFullPath, MAX_PATH, pPath);
         StringCchCat(acFullPath, MAX_PATH, TEXT("\\"));
         StringCchCat(acFullPath, MAX_PATH, ffd.cFileName);

         HANDLE hFile = CreateFile(acFullPath,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

         if (INVALID_HANDLE_VALUE == hFile)
         {
            if ( ERROR_SHARING_VIOLATION == GetLastError())
            { 
              _tprintf(TEXT("\n%s is opened exclusively by another process\n\n"), acFullPath);
              continue;
            }
             
            _tprintf(TEXT("\nCannot determine lock status of %s\n\n"), acFullPath);
            continue;
         }

         DWORD dwHi,dwLow;

         dwLow = GetFileSize(hFile,&dwHi);

         if (!LockFile(hFile,0,0,dwLow,dwHi))
         {
           if (ERROR_LOCK_VIOLATION == GetLastError())
             _tprintf(TEXT("\nCannot fully lock %s, file is locked by another process\n\n"), acFullPath);
            else _tprintf(TEXT("\nCannot fully lock %s, file seems to have a lock on it\n\n"), acFullPath);
         }
         else
         {
            _tprintf(TEXT("\n%s is not locked\n\n"), acFullPath);

            UnockFile(hFile,0,0,dwLow,dwHi);
         }

         CloseHandle(hFile);
      }
   }
   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
 
jazzIIIloveAuthor Commented:
So opened exclusively doesn't mean locking, right?

then, what does it mean?

Kind regards.
0
 
jkrCommented:
You can lock a whole file or just a portion of a file. That's locking. Opening a file exclusively means *not* even specifying 'FILE_SHARE_READ' when opening it, indicating 'exclusive access'.
0
 
jazzIIIloveAuthor Commented:
Ok, what is exclusive access? Can you give with an example?

Kind regards.
0
 
jkrCommented:
Well, as I wrote above: Opening a file exclusively means *not* even specifying 'FILE_SHARE_READ' when opening it. This will not allow any other process to access the file while you have it open.
0
 
jazzIIIloveAuthor Commented:
>>Opening a file exclusively means *not* even specifying 'FILE_SHARE_READ' when opening it
Ok but then what? I mean not READ but what?

Regards.

P.S. too many questions I know, sorry but you are a good expert, it's good to ask when I grab you in this question :D
0
 
jazzIIIloveAuthor Commented:
I mean definitely another process ?reachs? to the file, that's the reason the file is in use. But by which means? read/write/execute?

Regards.
0
 
jkrCommented:
No, it just means "has opened the file without any sharing options". It does not matter whether the process reads the file, writes to the file or des nothing else with it other than having it open.
0
 
jazzIIIloveAuthor Commented:
Ok but the code u provide finds out the file out of files which have been accessed, operated by an another executable and that exe is operating on all files and your code checks for that files and for the other files which are used by that exe, your code gives no file in use whereas for one particular file gives the output as the file in use. I wonder how that one is in use, i mean the other exe is in C++ and i used your code to find out that culprit file.

So, what can be the reason for that file to be opened as such w.r.t. your code logic?

Regards.
0
 
jkrCommented:
Seems we have a big misunderstanding here - what the code does now:

- opening a file handle. If that works, no other process has opened it exclusively
- trying to lock the entire file. If that works, no other process has a lock on that file
0
 
jazzIIIloveAuthor Commented:
Ah, I am illiterate but I think I understand the issue;

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27413769.html

Same question but C#, so what you say is that you cover already the last comment in the above question in Idle_Mind's comment in your code.

True?

Regards.
0
 
jkrCommented:
Yes, pretty much true ;o)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 16
  • 13
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now