使用CSV重命名文件夹时,有没有办法在命中发生时尽早跳出循环?

问题描述 投票:0回答:1

我有一个运行顺利的脚本,但由于它评估的文件夹数量巨大,它正在评估四层以下所有嵌套子文件夹中的每个条件(根据脚本的起点)。由于由于评估内容的具体情况,只能发生一次命中,如果发生命中(例如 1,000 个文件夹),是否可以停止进一步评估并移至下一个记录评估?

下面的脚本,欢迎见解。

# Assume the CSV is 100,000 entries and that Column3 contains a snippet of alphanumeric text
# that is grabbed using a split, then the file server is searched for a matching subfolder
# four levels down and renamed with a different snippet of alphanumeric text from Column1.

$folderPath = "Z:\TopLevelFolder\*\*\*\*"
$csvPath = "C:\PathToCSV\file.csv"
$count = 0

$csvData = Import-Csv -Path $csvPath

foreach ($row in $csvData)
{
    $prefix = ($row.Column3 -split ' - ')[0]
    $prepend = ($row.Column1 -split '-')[-1]
    $subfolders = Get-ChildItem -Path $folderPath -Directory -ErrorAction SilentlyContinue

    foreach ($subfolder in $subfolders)
    {
        if ($subfolder.Name -like "*$prefix*")
        {
            $newName = "("+ $prepend + ") " + $subfolder.Name
            Rename-Item -Path $subfolder.FullName -NewName $newName -ErrorAction SilentlyContinue
            $count++
            Write-Host $subfolder.FullName
        }
    }
}
Write-Host $count.ToString() "folders renamed"
powershell loops csv directory
1个回答
0
投票

使用

break
语句提前退出循环。

参考

© www.soinside.com 2019 - 2024. All rights reserved.