首页 > 解决方案 > 检查文件夹中是否存在文件?

问题描述

我不知道该怎么做。我需要执行一个脚本。

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

但在此之前,脚本必须始终检查某个文件是否存在。因为否则脚本将不会执行。在执行任何其他操作之前,需要移动现有文件。

我尝试使用 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
    }
}

标签: powershell

解决方案


推荐阅读