Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Rename unreliable on Windows

by elef (Friar)
on Apr 04, 2012 at 10:01 UTC ( #963394=note: print w/replies, xml ) Need Help??

in reply to Re: Rename unreliable on Windows
in thread Rename unreliable on Windows

I didn't say that "permission denied" is not actually the cause... It could be, technically, in some roundabout way.
The more relevant question is, what other application could have possibly opened the file in exclusive mode and why? I can't see an answer to that in the test script above, which fails for me with "Permission denied" somewhere between loop 1 and loop 900 every time.

Replies are listed 'Best First'.
Re^3: Rename unreliable on Windows
by Corion (Pope) on Apr 04, 2012 at 11:22 UTC

    Maybe your virus scanner, maybe your backup application, maybe some other program. How would we know? think that the Sysinternals Tools have some program to show what programs hold a filehandle to a file, but I don't know.

    If in doubt, I would talk to the system administrator for the machines in question.

      think that the Sysinternals Tools have some program to show what programs hold a filehandle to a file,

      Yes. it is called handle.exe:

      C:\test>handle /? Handle v3.42 Copyright (C) 1997-2008 Mark Russinovich Sysinternals - usage: handle [[-a [-l]] [-u] | [-c <handle> [-y]] | [-s]] [-p <proces +s>|<pid>] [name] -a Dump all handle information. -l Just show pagefile-backed section handles. -c Closes the specified handle (interpreted as a hexadecimal nu +mber). You must specify the process by its PID. WARNING: Closing handles can cause application or system ins +tability. -y Don't prompt for close handle confirmation. -s Print count of each type of handle open. -u Show the owning user name when searching for handles. -p Dump handles belonging to process (partial name accepted). name Search for handles to objects with <name> (fragment accepted +). No arguments will dump all file references.

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      Thanks for that one!
      It was indeed my backup sw (WD anywhere backup). After disabling it, the test script runs to 10000 loops without errors.

      Followup question: why does this happen and is there an easy workaround (apart from disabling the backup sw)? I mean, I don't think I've had problems with deleting or modifying files, only renaming... And I don't think I've had issues outside of perl.

        This is how Windows as an operating system works. A file cannot be renamed while it is held open.

        A good backup software would request the backup privilege, and use the Windows Backup API. But even then, I'm not sure whether that would allow renaming a file while it is held open.

        is there an easy workaround (apart from disabling the backup sw)?

        You could just keep trying to rename it...

        1 until rename $file1, $file2;

        Of course, if a file can genuinely never be renamed, this will cause your script to keep trying forever. Better to limit to a finite number of attempts at renaming.

        my $attempts; while (not rename $file1, $file2) { $attempts++; die "Could not rename file: $!" if $attempts > 7; sleep $attempts; }
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        One of the solutions that you could use in conjunction with your script is Unlocker. Although the screen shots highlight the GUI, there's also a command line version. If you used that, your script should then be able to do the rename.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://963394]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-06-24 22:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.