Command to know if a file is open on the network

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 2 years, 4 months ago.

  • Author
    Posts
  • #17200
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Hi,

    I've created a script that loads three Excel files to three variables into PowerShell to make some queries (the three files are on a network path).
    Is there a way to see if those files are open by another user on the network (like we do on Windows Server) using PowerShell?

  • #17202
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    You can use openfiles tool to check on open files from the CMD or POWERSHELL

    try this:

    openfiles /query
    

    if you get a message like

    The system global flag 'maintain objects list' needs to be enabled to see local opened files.
    

    try this:

    openfiles /local on
    

    you will need to reboot.
    After that run something like

    $openfiles = openfiles /query
    

    and maniuplate $openfiles to find what you're looking for..

    For more information see [url]http://technet.microsoft.com/en-us/library/cc732490.aspx[/url]

  • #17203
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you are only doing queries, you can force Word to open in read-only mode and you don't need to validate if the documents are open. The MSDN documentation is here: http://msdn.microsoft.com/en-us/library/bb216319(v=office.12).aspx

    $Word = New-Object -Com Word.Application
    $Word.Visible = $true
    $doc=$word.Documents.Open( $document,,$true )
  • #17207
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    This may be more of what you're looking for:

     
    function TestFileLock ($FilePath ){
        $FileLocked = $false
        $FileInfo = New-Object System.IO.FileInfo $FilePath
        trap {Set-Variable -name Filelocked -value $true -scope 1; continue}
        $FileStream = $FileInfo.Open( [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None )
        if ($FileStream) {$FileStream.Close()}
        $FileLocked
    }
    $File = "c:\pagefile.sys"
    if (TestFileLock $File) {Write-Output "File '$File' is locked"} else {Write-Output "File '$File' is not locked"}
    
  • #17221
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Sam Boutros,

    Thanks a lot! The second reply worked perfeclty =]

    Rob Simmers,

    Thanks for your reply.
    But I needed to know if the file was open to alert the user that the file must be changed on that moment.
    So the info can be outdated during the query.

    This is the best forum ever. Thank you very much!

You must be logged in to reply to this topic.