We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Reading a text file - Concerns about others trying to write to file

Afrofish
Afrofish asked
on
Medium Priority
212 Views
Last Modified: 2012-05-06
Hi everyone,

I am trying to import a text file every five minutes to give the latest items scanned in at the factory.  My code seems to work perfectly but I am concerned that I could be locking the source text file and causing other users not to be able to save data to it.  I have pasted my code into the code window, can someone tell me if this reads the source data into memory and then closes it or does it leave the source file open (and hence locked) while I read it.  Is there a better way to do this process than the one I am using.

Thanks in advance,

John
Open "p:\mypath" & Format(Date, "DDMMYYYY") + ".amp" For Input As #1

Open in new window

Comment
Watch Question

Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
Exclusive access is required to write to a file. Can't you copy the file before opening it in read mode?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
You would need to close it for others to open it. Can you let others view it in a browser (some extra coding) instead of opening the actual file or as mentioned copy it. Maybe you can name the original with an unknown extension and then read and write to it but save the copy as a text file for others to read. A few thoughts.

Author

Commented:
Thanks Guys,

How would you recomend I copy the data? I have pasted my new code below, please could you see if it looks OK.  It seems to work fine but if there is a more efficient way to do it I would love to know.

Thanks Again,

John
FileCopy "C:\mypath\" & Format(Date, "DDMMYYYY") + ".amp", _
         "C:\" & Format(Date, "DDMMYYYY") + ".amp"
Open "C:\" & Format(Date, "DDMMYYYY") + ".amp" For Input As #1
 
'Code Here
 
Kill "C:\" & Format(Date, "DDMMYYYY") + ".amp"

Open in new window

VK
Commented:
1. Your app has to read the file, is doesn't need any write permissions, right?
2. Other apps have to write to the file, they need write permissions.

First you it should be guaranteed that in 2. no collisions can occur.
Therefore exclusive access is the best for 2.
If the file is already open exclusively by an app, and a second tries
to open it also exclusively, an error will be raised which can
be handled to wait until the file is free for access.

If 2. is implemented well your access to the file should be no problem.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
That's the way to go. The only concerns I would make is that not all users have access to write to the root drive. You may prefer to keep the file into the same path but with a different name or extension.

Author

Commented:
Again, thanks very much for the prompt replies guys..

While I accept VKs suggestion to add the error checking to the database trying to open the file, they are unfortunately out of my current project scope.  I have now been running my code for around 30 mins with no errors to the other DB so it seems to be working.  Time permitting I will revisit the other databases to check their error checking routines but I am happy in the meantime that I have minimised my impact on the system.

Author

Commented:
Thanks very much guys.  Extremely helpful, prompt and clear.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.