locked
Process cannot access the file RRS feed

  • Question

  • I am using below code to upload the files on a premise local folder to sharepoint Office 365.

    #Upload file
    Foreach ($File in (dir $Folder -File))
    {
        $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $File
        $Upload = $FolderToUpload.Files.Add($FileCreationInfo)
        $Context.Load($Upload)
        $Context.ExecuteQuery()
    }

    I got an error: 

    BFile> C:\DBFile\Upload to sharepoint.ps1
    New-Object : Exception calling ".ctor" with "2" argument(s): "The process cannot access the file 'C:\dbfiles\myfile.csv' because it is being used by another process."
    At C:\DBFile\Upload to sharepoint.ps1:51 char:19
    + ... ileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMo ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
     

    The file or file is not open, but not sure why it says it is used by another process. searched online it says that file could be locked somehow. it is related with the filestream open. Is there a way to fix this? Thanks


    Thanks

    Monday, March 2, 2020 8:30 PM

Answers

  • Because it is being used by another process.  You cannot access a file that is in use.  You cannot track file usage without changing the OS to track handles.  Mostly there is no way to know what has a file open.  When using the files system API directly it is often a problem with you rerunning the same code and all of the files have been opened and not closed.

    Start by closing the file:

    $filestream.Close()

    This will prevent you from stepping on your own files that have already been opened.


    \_(ツ)_/

    • Marked as answer by jrv Monday, March 2, 2020 11:37 PM
    Monday, March 2, 2020 11:06 PM
  • Yes.  That should work for that issue but the file may be open for other reasons.  Only testing will give you that information.


    \_(ツ)_/

    • Marked as answer by msloy Monday, March 2, 2020 11:34 PM
    Monday, March 2, 2020 11:30 PM

All replies

  • Because it is being used by another process.  You cannot access a file that is in use.  You cannot track file usage without changing the OS to track handles.  Mostly there is no way to know what has a file open.  When using the files system API directly it is often a problem with you rerunning the same code and all of the files have been opened and not closed.

    Start by closing the file:

    $filestream.Close()

    This will prevent you from stepping on your own files that have already been opened.


    \_(ツ)_/

    • Marked as answer by jrv Monday, March 2, 2020 11:37 PM
    Monday, March 2, 2020 11:06 PM
  • Thank you,

    What line shall I add the $filestream.Close() into my existing code? like below?

    Foreach ($File in (dir $Folder -File))
    {
        $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $File
        $Upload = $FolderToUpload.Files.Add($FileCreationInfo)
        $Context.Load($Upload)
        $Context.ExecuteQuery()
         $filestream.Close()

    }


    Thanks

    Monday, March 2, 2020 11:28 PM
  • Yes.  That should work for that issue but the file may be open for other reasons.  Only testing will give you that information.


    \_(ツ)_/

    • Marked as answer by msloy Monday, March 2, 2020 11:34 PM
    Monday, March 2, 2020 11:30 PM