检查文件夹中是否存在文件?

时间:2018-10-05 18:45:37

标签: powershell

我不确定该怎么做。我需要执行一个脚本。

脚本需要监视文件夹中创建的文件。如果文件被创建,它将触发一系列功能。

但是在此之前,脚本必须始终检查某个文件是否存在。因为否则脚本将无法执行。必须先移动现有文件,然后再执行其他操作。

我尝试使用filechanged事件,但是它将覆盖现有文件...

有没有办法做到这一点?

这是脚本的开头:

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true              # <-- set this according to your requirements
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    Write-Host "The file '$name' at '$path'was $changeType at $timeStamp" -Fore white
    $lineas = (Get-Content $root\UNB\TMP\FACT_TEMPORAL.TXT | Measure-Object -Line).Lines
    Write-Host "El archivo contiene $lineas lineas" -Fore white

    if ($lineas -gt 3) {
        Write-Host "El archivo contiene información" -Fore white
        limpia
        leeyedita
        printpdf
        borrar
        renombra
    } else {
        Write-Host "El archivo NO contiene información" -Fore white
        borrar
    }
}

0 个答案:

没有答案