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?
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]
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 )
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"}
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!