How to read a file

Posted on 2003-03-15
Medium Priority
Last Modified: 2010-05-01
i need to read a file which is already opened by another process, that process also denys reading the file by another process. but i need to read that file. how to read the file using VB. can any one help me
Question by:maheshexp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5

Expert Comment

ID: 8145453
Is the Input access mode OK?
Open filename For Input As 1
 a = Input(FileLen(filename), 1)
 Debug.Print a

Expert Comment

ID: 8145467
I dont have a straight answer.
You could try to copy this file to another directory and read it.
Usually even Windows 2000 doesnt allow us to copy or use locked files.
LVL 18

Expert Comment

ID: 8145474
This code will read the text even from an open file:

Set References to Microsoft Scripting Runtime in your VB project.

Declare the following variables in Declaration section:
     Dim objFSO As FileSystemObject
Dim objTextFile As TextStream
Dim strRead as String
Fim strFileName as String
Create the following Sub to Write to a textfile:
Public Sub ReadFromAFile()
     Set objFSO = New FileSystemObject
     strFileName="C:\My Documents\Testing.txt"
         Set objTextFile = objFSO.OpenTextFile(strFileName, ForReading)
strRead = objTextFile.ReadAll
End Sub
'--- strFileName is the file name where you want to read the text from.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 16

Expert Comment

ID: 8145712
Open a file depends of the type of file also. You could open as many txt files as you want (or M$ Access DBs in example) but it is not true for other types.What type of file are you trying to open? OS is a matter too.

Author Comment

ID: 8145734
what i want is, to open a file which is currently under windows 2000 process. the os doesn't allows me to open the file in notepad. but i want to open that file. how to do it. when i try to open the file in note pad, it says access denied.

Expert Comment

ID: 8147661
Open lpszFilename For Binary Access Read Shared As #1

Expert Comment

ID: 8185192
If the file is opened in a mode that don't allow you to read it, you have to find the handle on that file, kill it then open it.

I have never find a way to retreive handle information for a specific files.  I have been told that it need some low level acces that VB don't have.  I'm using a freeware to get that kind of information:  http://www.sysinternals.com/ntw2k/freeware/procexp.shtml

That app also allow you to close handles.

Expert Comment

ID: 8426518
Dear expert(s),

A request has been made to close this Q in CS:

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.


Community Support Moderator
Experts Exchange
LVL 16

Expert Comment

ID: 8427684
if you kill the handle, how manage the file the app that have been opening it in first place?

Expert Comment

ID: 8427746
Maheshexp wanted to know how to open a locked file using VB.  So to do that I think the only way is to kill the locking handle on that file.  I didn't mention it, but yes it can lead to serious problem for the application that had a lock on that file.

Maybe in assembler you can read it even if it locked, but I think the locking process is handled by the OS and it will deny any code that will try to read, delete, write, ... to that file.  But my asembler and OS knowledge on that subject is far behind me and the question require to use VB.

Accepted Solution

modulo earned 0 total points
ID: 8449361
Saving this Q as a PAQ and refunding the 100 points to the questionner


Community Support Moderator
Experts Exchange

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question