Learn how to a build a cloud-first strategyRegister Now

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

VB Script For File Locking

I have a custom Vista application which watches a certain folder for PDFs to drop in.  When it sees them it immediately moves a selected number (batch size) to another folder and opens a session of an OCR package to process the files.  I have been using ABLE7 to move the files from a staging folder which receives FTPed files (down), then it moves them into the watched folder.  The process works smoothly, but unfortunately ABLE7 sometimes just seems to get lost, forget, whatever.  ABLE8 is no better, BTW.  So I wrote a VB Script to move files from the staging folder to the watched folder.  The problem is that the custom program grabs a file as soon as it hits the watched folder and moves it to the OCR batch folder, and the file is incomplete.  When ABLE7 puts them into the watched folder,this does not happen, so I assume the file is locked until complete, then ABLE7 releases it and the custom SW can pick it up, but not before relased, therefore complete.

How can I prevent the custom SW (cannot be further modified at this point) from grabbing a file before it is complete and closed?  Can I define an empty file as locked, then transfer in the data, then close the file and unlock it?
0
Mike Caldwell
Asked:
Mike Caldwell
  • 7
  • 4
1 Solution
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
BTW: instead of locking the file, can I create it as hidden, write the data to it, then change it to not hidde?
0
 
purplepomegraniteCommented:
What VB method are you using to move the file from the staging folder to the watched folder?  If you use FSO methods (i.e. Move), then the file will not be available until the copy is complete.  Could you post the code you are using for the move process?
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
I am using the FSO; code attached.

'******  	MOVE FILES FROM DOWNLOAD STAGING FOLDER TO OCR BUCKET 
Const strSourceFolderName = "D:\EXP STAGE\"		'   From
Const strDest1 = "D:\EXP DN"			' To
 
Dim objsFSO, objsFolder, objFile, fso, nDestinationFileCoun
 
Set objsFSO = CreateObject("Scripting.fileSystemObject")
set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.fileSystemObject")
 
Set objFolder = objFSO.GetFolder(strSourceFolderName)
 
nDestinationFileCount = 0				' Initialize counters
 
Do while 1 = 1						' Monitor continuously
				
		nFiles = objFolder.Files.Count		' How many to get
 
		If nFiles >0 then
 
			WScript.Sleep		30000	' Make sure the last file is complete
 
			For Each objFile in objFolder.Files		' Now go move files
 
				If not objFSO.FileExists (strDest1 & "\" & objFile.Name) then
 
					Call objFile.Move(strDest1 & "\" & objFile.Name)	' Move the file
				 	nDestinationFileCount = nDestinationFileCount + 1	' increment the count
	               				If (nDestinationFileCount >= nFiles) Then
                        				Exit FOR
						Else
                  				End If
 
				Else	' File already exists, so delete the duplicate at the source
					Call objFile.Delete	' Delete the file; already at the target
 
				End if
 
			Next
 
		Else
			WScript.Sleep	10000			' Wait before looking again
		End if
 
Loop

Open in new window

0
Independent Software Vendors: 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!

 
purplepomegraniteCommented:
I am pretty sure that the Move method will not allow any process to open the file until the file has actually been moved.  Certainly, if the other application is watching the folder using .net FileWatcher or a system API, the event that says a new file is there won't be triggered until after the move anyway.

Is it that the file is being moved from the FTP folder before it has been fully uploaded?  How big are the files in question, and how long do they normally take to upload?  The script could be modified to only move a file if it was created or modified a set time previously; this may overcome the problem.

Other question is what FTP server are you using?
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Here's the flow:
1. FTP down from my server to local PC.  I have a VBS that uses the Chilkat Active X for FTP.  Seems to work fine.  Upon down load it changes the file modified time to the local time.
2.  The file is in the EXP STAGE folder, and ABLE7 moves it to EXP DN but only if the file was modified more than a minute ago.
3.  The OCR program watches D:\EXP DN\, and grabs whatever is there into another folder (D:\PDF5\), then it launches an OCR session.  The OCR software is configured to OCR what it finds in D:\PDF5\.

THe problem is that the file put into D:\PDF5 is sometimes zero bytes, and another copy of the file is still in D:\EXP DN\ because it finished the MOVE after the controller SW moved the zero byte file.

I think what I will do is just cheat on the system.  Instead of moving from D:\EXP STAGE\ to EXP DN I will copy it, then with a VBS I'll wait for the controller SW to move it to PDF5, but I will have the OCR script looking back to EXP STAGE for it's file to convert.  IN other words I'll use the file that moves from EXP STAGE to EXP DN to PDF5 as a trigger, then just delete them.

The issue is clearly not with the FTP, because there is a whole file in EXP STAGE and it eventually gets move whole into EXP DN, but the controller software (a customer app I had written by a consultant) is somehow able to swipe the file into PDF5 before the file is complete and closed.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Think I've made a breakthrough.   Apparantly the OCR controller SW only looks for PDFs.  So I just put a PDF file into the watched folder, but first changed the file extension to .TXT.  The SW ignored it.  When I changed the file extension back to .PDF it kicked off.  So I'm pretty sure it could not know it as a PDF file until the name change is complete.
0
 
purplepomegraniteCommented:
That looks like a good workaround - I am presuming you are then going to move the file with different extension and then rename?

FSO Move is actually FSO Copy followed by delete - so you may well have the same problem when copying the file (if it has pdf extension).

It would be interesting to know how the program monitors the EXP DN folder, as it doesn't seem to be a system hook (it may be a timer, which could explain the problem).
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
dummy = objFile.Rename(NewName) doesn't seem to work.  Gotta VBS for renaming?

Yes, will move into EXP DN with a .JNK extension, the rename.
0
 
purplepomegraniteCommented:
Renaming using FSO is actually move.  e.g.

    set fso = CreateObject("Scripting.FileSystemObject")
     fso.MoveFile "c:\name.pdf", "c:\name.jnk"
     set fso = Nothing

Alternatively you can use GetFile and access the Name property:

    set fso = CreateObject("Scripting.FileSystemObject")
     set file = fso.GetFile("c:\name.pdf")
     file.name = "name.jnk"
     set file = nothing
     set fso = nothing

0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Seems to be working well.  I think the difference might be that a MOVE to within the same path just changes the file table entry, so the file is totally valid before the name change can be seen by my triggering package.  We have a system test that is running every five mintues, and it will send me an SMS if the test file does not return post-OCVR within ten minutes.  I'll let it run all night that way and report tomorrow.  Thanks for the help.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
The MOVE works fine.  The key is that the file has to already be in the target folder.  So, to summarize, I download (FTP) the PDF file with an added file extension of {.JNK}.  My OCR controlling software is looking for files with the {.pdf} extension, so it leaves the file alone while downloading.  Once finished, I then do a MOVE with a new file name the same as the old but with only .PDF, and the controller software then fires.  The key is that the folder/path not change.  If I FTP down to a different folder, and then MOVE in retaining the .JNK extension, then then strip it off, that works too, just slower.

Thanks for the brainstorming help.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now