监控创建,然后通过windows.bat脚本读取文件



我想写一个批处理脚本,它将在一定的时间限制内轮询windows目录,并在文件放入该目录后立即拾取文件。如果文件没有在该时间范围内放置在该目录中,那么它也会在特定时间后超时。

我还想解析xml文件并检查状态。

下面是一个PowerShell脚本,它将按照您的要求执行操作。

$content变量将存储文件的内容(它实际上是一个行数组,因此您可以将其放入foreach循环中)。

$file = 'C:flag.xml'
$timeout = New-TimeSpan -Minutes 1
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout)
{
    if (Test-Path $file)
    {
        "$(Get-Date -f 'HH:mm:ss') Found a file: $file"
        $content = gc $file
        if ($content -contains 'something interesting')
        {
            "$(Get-Date -f 'HH:mm:ss') File has something interesting in it!"
        }
        break
    }
    else
    {
        "$(Get-Date -f 'HH:mm:ss') Still no file: $file"
    }
    Start-Sleep -Seconds 5
}

最新更新