Powershell复制项目:文件名正在更改

时间:2019-02-11 10:26:34

标签: powershell

非常感谢您的支持,因为这与患者的护理和隐私有关。

我正在为一家医院编写以下脚本,在该脚本中,应将患者的医学图像/视频复制到另一个文件夹以进行存档。

工作流程为:设备将AVITHB文件存储在名称完全相同的文件夹中,文件名的语法为ID_DATE,例如:

  • 123456_2112019.AVI
  • 123456_2112019.THB

我的脚本正在监视此文件夹,并将AVI文件仅复制到另一个文件夹。

问题是AVI文件已被成功复制,但是名称被修剪,它删除了下划线之前的所有内容,因此文件123456_2112019.AVI变成了_2112019.AVI

我注意到THB文件是此问题的根本原因,因为我在没有THB文件的情况下在其他文件夹上进行了测试,并且复制操作成功完成,而没有更改名称。 / p>

请问您如何解决此问题?这是我的代码

$folder = 'D:\test'
$filter = '*.AVI'                             

$fsw = New-Object System.IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true              
    NotifyFilter          = [System.IO.NotifyFilters]'FileName, LastWrite'
}

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

    [int]$sleepTime = 500
    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host $path -fore Green
    Write-Host $name -fore Green
    Write-Host $changeType -fore Green
    Write-Host $timeStamp -fore Green

    try {
        # **********check if the file is locked by another process (still being received from the soource)********
        function Test-FileLock {
            param (
                [parameter(Mandatory = $true)][string]$Path
            )

            $oFile = New-Object System.IO.FileInfo $Path

            if ((Test-Path -Path $Path) -eq $false) {
                return $false
            }

            try {
                $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)

                if ($oStream) {
                    $oStream.Close()
                }
                $false
            }
            catch {
                # file is locked by another process.

                return $true
            }
        }
        #******** End of Function (test if file is locked) *****************
        # below line : will keep testing if the file is locked ************
        while (Test-FileLock $path) { Start-Sleep 10 }

        # below line: once the file becomes "NOT Locked" , copy the file to the destination folder *******
        Copy-Item  $path  'D:\output' -Force -Verbose -PassThru 
    }
    catch {
        $error|Out-File  -FilePath d:\outlog.txt -Append
        $Error.Clear()
    }
}

0 个答案:

没有答案