none
Powershell is locking the file when I test for locked status RRS feed

  • Question

  • I've written a script that processes video files and then moves the original to another folder if they are not currently locked by another process. The function that I use to test for a file lock appears to to be locking the file. Can anyone give a suggestion on how to get powershell to release it's lock on the file so I can move the file?
    --------------------
    Here's the error-

    Move-Item : The process cannot access the file because it is being used by another process.
    At E:\Scripts\VideoMover\VideoMover.ps1:55 char:13
    +             Move-Item $TempPath$item $CopyPath
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (E:\VD_Temp\rm10...2013_121426.asf:FileInfo) [Move-Item], IOException
        + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

    ---------------------
    And the script-

    $TempPath = "E:\VD_Temp\"
    $VideoBase = "E:\Video\"
    $CopyBase = "E:\Copy\"

    function TestLockStatus($filePath) {
        $fileInfo = New-Object System.IO.FileInfo $filePath
        
        try {
            $fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
            #write-host $filePath "is NOT locked"
            return $false
        }
        catch {
            #write-host $filePath "IS locked"
            return $true
        }
    }

    $items = Get-ChildItem -Path "$TempPath" | Where-Object {$_.Extension -eq ".asf"}
    foreach ($item in $items)
    {
        # if the item is not a directory and it is NOT locked, then process it.
        $FileToTest = $TempPath+$item
        $FileStatus = (TestLockStatus($FileToTest))
        if (($item.Attributes -ne "Directory") -and ($FileStatus -eq $false)) {
            # Gather info from file name
            $Dept = $item.Name.Substring(7,2)
            $UserType = $item.Name.Substring(9,2)
            $UserNumber = $item.Name.Substring(11,2)
            $RoomNumber = $item.Name.Substring(0,6)

            $VideoPath = $VideoBase + $Dept + "\" + $Dept + $UserType + $UserNumber
            $MP4Path = $VideoPath + "\" + $item.name.Replace(".asf", ".mp4")

            if ((Test-Path $MP4Path) -ne $true) {
                $Prog = ".\ffmpeg.exe"
                $Args = "-n -i $TempPath$item -vcodec copy $MP4Path"
                Start-Process $Prog -ArgumentList $Args -Wait
            }

            $CopyPath = $CopyBase + $RoomNumber
            if ((Test-Path $CopyPath$item) -ne $true) {
                Move-Item $TempPath$item $CopyPath
            }
        }
    }

    Friday, August 30, 2013 7:37 PM

Answers

  • Possibly try calling the Close method on your file stream?

    try {
             $fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
             #write-host $filePath "is NOT locked"
             $fileStream.Close() # Close the file
             return $false
    


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    • Marked as answer by Dr_Optical2 Friday, August 30, 2013 8:18 PM
    Friday, August 30, 2013 7:51 PM

All replies

  • Possibly try calling the Close method on your file stream?

    try {
             $fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
             #write-host $filePath "is NOT locked"
             $fileStream.Close() # Close the file
             return $false
    


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    • Marked as answer by Dr_Optical2 Friday, August 30, 2013 8:18 PM
    Friday, August 30, 2013 7:51 PM
  • I love getting quick and accurate answers!

    Thanks for your response, Clayman2.

    Friday, August 30, 2013 8:18 PM