check to see if a file is locked


How do we check to see if a .rar/.zip file is locked, using SSIS? If it is locked, then the package should stop executing. If the file is not locked, then it should unzip/extract/decompress the file using pkzip in the execute process task. How do we accomplish this using SSIS 2005. We should be using only PKZIP to decompress the file. What is the command that should be used to unzip/extract/decompress the file using pkzip.

Thanks  a million in advance.

Any logic/code is greatly appreciated. Can someone help me with this ASAP. I'm not permitted to install any additional zip/unzip software on the server.

I greatly appreciate your help ASAP.
Who is Participating?
DcpKingConnect With a Mentor Commented:
Well, you could use an Execute Process task to run a batch file to unzip:
pkunzip sourcefilename, targetfilename
exit /B 0

Open in new window

The last line means that it always returns success. Next, continue if the unzipped targetfile exists, and not if not. You can do this by running a foreach container for each instance of the targetfile (only ever one!) - if it exists then the file wasn't locked and you loop one time. If the file was locked no targetfile exists and the loop never executes.

I actually have something similar - an FTP job to download a large file. However, I can't download it until it's all finished being uploaded to the FTP site! We wrote a small Python program that looks at the file date/time and size every few minutes and compares it with the previous results. If it hasn't changed then we're ready to download! You might want to try doing something like that, but with the attributes of the Zip file.

Another possibility, if you're allowed, is to get the package of SSIS tasks from PragmaticWorks. The free package contains a task to return info about a file and I think it might tell you the locked status, although I'm not sure.


Anthony PerkinsCommented:
For the record, this is a continuation of this thread:
amuktaAuthor Commented:
Thank you Mike,

I need to only unzip the files if they are not locked.if atleast one file is locked, my SSIS package should stop executing.i found some code online to check to see if the files are locked. But the file status of .rar/.zip is always always being shown as locked, even when they are not locked. Is there any special way of checking these files using a script task?
What if you did something that would fail if it is locked?

Like maybe use a script task to rename to or move the file into another subfolder named ..\ready or something like that.

Maybe even use error handling and a loop.... if it fails, pause 30 seconds and try again... for up to a max of 10 retries....  that would be pretty cool.  You could make it send an email if it was still locked after the max number of retries.
I think you may be looking in the wrong place if you're seeing the .zip files as always locked - or else the creation program hasn't finished with them yet.

You might try running the unzip on them from a batch file and seeing - if they unzip regardless then it really doesn't much matter whether you think that they're locked or not ...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.