0

I am currently able to check whether a CSV file with .csv extension is open by using this code

if (-e $filename) {
    if (open(TXT,">>$filename")){
        #file is closed 
    } 
    else {
        #file is open
    }  
} 

If I try this with a .txt file instead of a .csv file, then the test fails. It fails means the code executes if statement even when the file is opened by me whereas it should have executed else statement of the code.

But if I open a .txt file using Excel, it fails too.

How can I ensure that the test is successful if the file is opened using Notepad or Excel?

I checked another post How do you check if a file is open using Perl? but it didn't work for me. Can someone please suggest how to work on this.

UPDATE:

if (-e $filename) {
    if (open(TXT,">>$filename")){
          #file is not in use   
    } else {
          #file is in use
}  

If i replace the above code with the one mentioned in the answer

open TF, "<$filename" or die "unable to open file $!"; #open the existing file
if(<TF>){
    close TXT;  
} else {
    goto END;

}

It doesnot work. maybe i did a syntax error on line where we open the existing file. ?

Community
  • 1
  • 1
Ayesha
  • 835
  • 4
  • 14
  • 33
  • 2
    This question is _exactly_ the same as the question you linked to. – Matt Jacob Sep 02 '15 at 19:14
  • Which OS? On Linux there is also [`Linux::Fuser`](https://metacpan.org/pod/Linux::Fuser) – Håkon Hægland Sep 02 '15 at 19:38
  • Please explain what you mean by *"the test fails"*. Do you mean that the `open` in the test fails, so the file is already open? Or do you mean that the test gives the wrong result? – Borodin Sep 02 '15 at 19:41
  • I’ve closed this as a duplicate because you do not explain why that question is not the same as yours, nor why its solutions do not work for you. If you would please do so, we can reopen it again. – tchrist Sep 02 '15 at 19:42
  • @tchrist: I think it's pretty clear that the linked question doesn't consider the possibility that a file may already be opened by other processes. In this case it sounds like Excel specifically is opening files in such a way that a subsequent open for append doesn't fail – Borodin Sep 02 '15 at 19:45
  • @Ayesha: Please give detailed examples of what happens now, and what you want to happen? – Borodin Sep 02 '15 at 19:47
  • @Borodin It may perhaps not be a duplicate, but it is not really answerable in the current state, mostly for the reasons you mention. Also, the question is confusing because the comments seem to be exactly the opposite of the code. Moreover, it has a race condition. Someday someone will write *FMTEYEWTK on Filetests as the Root of All Race Conditions*. :( – tchrist Sep 02 '15 at 19:52
  • @Borodin- test fails means the above code doesnot recognize if i open that text file and run this code. i.e. it still shows that file is close even if i had opened the file – Ayesha Sep 02 '15 at 20:22
  • @HåkonHægland- its a windows platform. the above code doesnt recognize tat my text file is open. even if i open it, the above code will still show tat its closed – Ayesha Sep 02 '15 at 20:24
  • @tchrist- none of the answer in that post solves my problem. this is different because a user opens a text file in MS Excel and yet displays 'file is closed' error message whereas it should display "File is open" – Ayesha Sep 02 '15 at 21:04
  • @MattJacob- the solution over those post doesnot worrk. – Ayesha Sep 02 '15 at 21:05
  • @tchrist: *"I’ve closed this as a duplicate"* followed by *"It may perhaps not be a duplicate"* doesn't look good. Perhaps you should spend some time chilling before you hit the red button. I would accept a close *vote* on the basis of ***unclear what you're asking***, but I've never been sure about that one as the OP should be allowed at least a little time to fix their question based on the comments that it gets. In this case I think two hours is adequate – Borodin Sep 02 '15 at 21:53
  • @Borodin You’re right. I was going to come back to it and got distracted. – tchrist Sep 02 '15 at 21:55
  • @Ayesha check the solution I provided in answer section. I believe it will work for you – newcoder Sep 04 '15 at 10:20

1 Answers1

1

try this or you can use the other one also for same thing

open TF, "<test.txt" or die "unable to open file $!"; #open the existing test.txt file

if(<TF>)
{
       print "file is open";
}
else
{
    print "There might an issue with this file";
}

OR

if you want to check whether a file handler is open or not try this

 open TF, ">>test1.txt" or die "unable to open file $!";

    if(tell(TF) != -1)
    {

        print "file is open";

    }
    else
    {
        print "There might an issue with this file";
    }

tell reports the position where you are in the file . If it's -1 which is an invalid position means that you aren't anywhere in the file.

newcoder
  • 464
  • 9
  • 23
  • @iamnew- i get a syntax error because i am not specifying the text file as shown above. i am specifying the complete path of the file in $filename. Can you tell me how we should replace test.txt with $filename. $filename= sccmp.com\folder1\text1.txt – Ayesha Sep 04 '15 at 15:29
  • can you update that piece of code and the error in your question so that I can have a look ? – newcoder Sep 04 '15 at 16:25
  • @iamnew- updated the post. could you please have a look – Ayesha Sep 04 '15 at 16:36
  • you didn't mention what you have assigned in $filename and error you are getting. so Iam assuming that you have decleared $filename= sccmp.com\folder1\text1.txt now change this to $filename="\\ sccmp\.com\\folder1\\text1\.txt"; and then try – newcoder Sep 04 '15 at 17:43