Lock a big number (over 1000) of files

lock a big number of files on the same time

Page 1 of 1

5 Replies - 1362 Views - Last Post: 05 June 2009 - 06:39 AM Rate Topic: -----

#1 justgreat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-June 09

Lock a big number (over 1000) of files

Posted 01 June 2009 - 07:27 AM

I would like to know, how can we lock a big number of files on the same time (over 1000 files), so that other applicatons/users can't access them, till i unlock them.
I know that usually we can lock a file by using something like :
Open "c:\MyFile.txt" For Binary Access write Lock Read write As #fileNumber
but the problem is that as i know #fileNumber takes a number in the ranges 1 255 and 256 511 ... thus, we can open 511 files on same time, am i right ?
If yes, how can i lock more than 511 files on the same time ?
what i want is to loop through a big number of files in a listbox and lock them all
then loop again and unlock them once i finish from the changes that i want to do.

Is This A Good Question/Topic? 0
  • +

Replies To: Lock a big number (over 1000) of files

#2 schaefer007  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 04-February 09

Re: Lock a big number (over 1000) of files

Posted 02 June 2009 - 02:01 PM

Assuming that you have a ListBox containing the files you could just do:

Private Function lockFiles()
Dim i As Long, j As Long
For i = 0 to List.ListCount - 1
j = FreeFile
Open List.List(i) For Binary Access write Lock Read write As #j
Next i
End Function



This would run through the list and lock the files.

Best of luck, Chris
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Lock a big number (over 1000) of files

Posted 03 June 2009 - 11:08 AM

@schaefer007:
That is a bad idea because you're not storing the file handles of the files anywhere so there's absolutely no way you can access the file at the end of the loop iteration.

A good idea would be to use FreeFile to obtain file handles and store them in an array so that you can access open files later.

If I'm not mistaken, VB6 has a limit for the total number of files that can be open at any given point so I suggest using a technique that opens only about 200 files at a time (I think 255 is the limit) and close a file if you need to open another.

You can decide which file to replace in the technique that I mentioned above by using any of the methods mentioned below:
  • Randomly select a file to be replaced
  • LRU (Least Recently Used)
  • LFU (Least Frequently Used)
  • Optimal Algorithm (Don't replace the files that you know are going to be needed at a later stage)
  • NRU (Not Recently Used)
  • ...or the easiest - FIFO (First In First Out)

As surprising as this might sound, studies show that randomly choosing files for replacement performs better than all the other techniques with LRU method as a runner up.
Was This Post Helpful? 0
  • +
  • -

#4 justgreat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-June 09

Re: Lock a big number (over 1000) of files

Posted 04 June 2009 - 06:56 AM

thanks to both of you, but your answers were not something new to me, if you read well my post, i want to open more than 200 files on the same time and can't close them before !
I have a preview mode and all files are locked till the user decide to cancel his changes to submit or to close my application....i want to liock them because i don't want to give the right to another application or person to delete a file or change it before that the user who is in preview mode leave the file.

the limits in vb is 511 files open on the same time ..
they told me to seek something call ACL but am not finding a tutoriel about it in vb 6 :(
Was This Post Helpful? 0
  • +
  • -

#5 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Lock a big number (over 1000) of files

Posted 05 June 2009 - 12:54 AM

ACL = Access Control List (I think)

While not directly solving your problem, search the web for "Watch Folder". There are a couple of examples out there that will tell you when a file is added/modified/deleted from a directory.

Easy to break suggestion, create folder as hidden (not all (and I wonder why) users change the setting of how they view the directory structure).

Good Luck
Was This Post Helpful? 0
  • +
  • -

#6 Jupiter 2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 05-June 09

Re: Lock a big number (over 1000) of files

Posted 05 June 2009 - 06:39 AM

Okay, Rename each file in the folder filename.txt renamed to X0001_filename.txt to X1000_filename.txt (ie just add a prefix to the original filename so youknow what to remove when you are done with the file.)

Anybody trying to access the original filename will be met with "File does not exist" message box.
You can then open and close as many as you want. Particularly useful if the file size is larger than 500kb.

Probably best to send an administrative notice to users of such files that the files are currently being upgated and cannot be accessed till 10am (or whenever)

===========================================
"If all my answers are wrong only then will I read the Question"

This post has been edited by Jupiter 2: 05 June 2009 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1