From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how i can i know that text file is already open in windows

Hello all,

 

                 I make developing a test and measurement application. I am currently accquiring data from different instruments and then logging it in text files (text file has been demanded by the user). Everything is running fine but problem occurs when the user opens the file from directly from windows and my application is no longer able to log data because the application is open by OS/ windows.

 

How can i detect if the file has suddenly being opened in windows and display message to user that File is already opened. Because the I/O VIs in labview dont return any error even if it is opened and labview is writing it.

 

 

 

Thanks in advance 

 

Usman Asghar 

 

Universitat Bremen, Germany

0 Kudos
Message 1 of 6
(4,183 Views)

Hi Usman,

 

all (basic) FileIO functions return an error!

Simply check the error out of the file functions…

 

You can prevent opening your text file from other applications: Open it with mode "read/write" and keep it open until you're finished with data logging!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(4,181 Views)

Thanks for your reply.

 

I am attaching a testcase.VI with a simple program. I have tried your way but it simply doesnot work(it was obvious).  Could you check by yourself i mean give a path for text file (any) then run it. It wont give any error. Now stop the VI

 

Now for second case open the file (not using labview) and keep it open. Again run the program you will not get any error but still file not be written. 

 

Thanks 

 

0 Kudos
Message 3 of 6
(4,172 Views)

It depends on the other application - some applications only open the file for reading (non-locking) and others open the file for writing (locking) when you first open them.

 

In your test case - I would try creating two VIs - one that opens the file reference for writing (which should lock the file) and holds it open (until you press stop), in another VI, try again to open the file for writing. The second VI will produce an error because the file is open in the first VI.

 

 


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 4 of 6
(4,160 Views)

thanks man. 

 

But my scenario is as below. 

 

The data is being logged after 10sec continously. Now a user un knowingly open the log file (not using labview please keep in mind) but by just using mouse or keyboard. Now he/she has opened the file and at the same time my application is running. Since i am not getting any error the application runs but still it is not update. 

 

Using labview to open the other instance of text file is not the case. WIth this its obvious that i would get an error.

 

 

0 Kudos
Message 5 of 6
(4,152 Views)

If you open the file reference at the start of your logging/application, and only close it at the end of the logging/application - your application will hold the lock on the file and no other application will be able to write to the file.

 

Here is an example:

2015-12-15_13-43-17.png

 

In my VI (which you can see is running), it opens/creates a reference to file.txt and holds it open until I press stop. I opened the file in Notepad but if I try to save the file, I get the error you see in the screenshot.

 

If I stop the VI, I can then save in Notepad.

 


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 6 of 6
(4,145 Views)