如何将文件夹中的文件断言为 PowerShell 中拥有的其他文件



我有文件夹A和B包含一些文件。不会分配这些文件夹中的文件总数。我想更改文件夹 B 中某个文件的名称,而要更改的文件总数取决于文件夹 A 中的文件总数。文件夹 B 的平均值(如果除以文件夹 A(。

例:

文件夹 A 文件 (2 文件(:

ID_12345-ABC.txtID_67890-DEF.txt

文件夹 B 文件 (10 文件(:

NO_1111-A.txtNO_1111-B.txtNO_1111-C.txtNO_1111-D.txtNO_1111-E.txtNO_1111-F.txtNO_1111-G.txt...NO_1111-J.txt

然后,我想更改文件夹B中的文件名。在这种情况下,我将文件夹 B 中的每 5 个文件分配给文件 A。在文件夹 B 中,我将拥有此文件名。

12345-ABC_NO_1111-A.txt12345-ABC_NO_1111-B.txt12345-ABC_NO_1111-C.txt12345-ABC_NO_1111-D.txt12345-ABC_NO_1111-E.txt67890-DEF_NO_1111-F.txt67890-DEF_NO_1111-G.txt...67890-DEF_NO_1111-J.txt

任何人都可以告诉我这个案子的想法吗?

您可以通过以下方式执行此操作:

# get an array of prefixes, taken from the file names in FolderA
$prefixes = Get-ChildItem -Path 'D:FolderA' -File | ForEach-Object { $_.BaseName -replace '^ID_' } | Sort-Object
# get an array of FileInfo objects of all files in FolderB
$filesB = Get-ChildItem -Path 'D:FolderB' -File | Sort-Object Name
if ($prefixes.Count -gt 0) {
    $batchSize = [Math]::Floor($filesB.Count / $prefixes.Count)  # number of items to receive the same prefix
    $offset    = 0
    foreach ($prefix in $prefixes) {
        for ($i = 0; $i -lt $batchSize; $i++) {
            $index = $offset * $batchSize + $i
            $filesB[$index] | Rename-Item -NewName ('{0}_{1}' -f $prefix, $filesB[$index].Name) -WhatIf
        }
        $offset++
    }
}
else {
    Write-Warning "No files found in FolderA. This would result in a 'Divide by Zero error'"
}

结果:

What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-A.txt Destination: D:FolderB12345-ABC_NO_1111-A.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-B.txt Destination: D:FolderB12345-ABC_NO_1111-B.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-C.txt Destination: D:FolderB12345-ABC_NO_1111-C.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-D.txt Destination: D:FolderB12345-ABC_NO_1111-D.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-E.txt Destination: D:FolderB12345-ABC_NO_1111-E.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-F.txt Destination: D:FolderB67890-DEF_NO_1111-F.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-G.txt Destination: D:FolderB67890-DEF_NO_1111-G.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-H.txt Destination: D:FolderB67890-DEF_NO_1111-H.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-I.txt Destination: D:FolderB67890-DEF_NO_1111-I.txt".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-J.txt Destination: D:FolderB67890-DEF_NO_1111-J.txt".


更新

从您的评论中,您还需要将重命名文件的扩展名更改为.csv,但还需要重命名由于它们不适合批处理而保留的任何文件。

下面的代码将同时执行这两项操作:

# get an array of prefixes, taken from the file names in FolderA
$prefixes = Get-ChildItem -Path 'D:FolderA' -File | ForEach-Object { $_.BaseName -replace '^ID_' } | Sort-Object
# get an array of FileInfo objects of all files in FolderB
$filesB = Get-ChildItem -Path 'D:FolderB' -File | Sort-Object Name
if ($prefixes.Count -gt 0) {
    $batchSize = [Math]::Floor($filesB.Count / $prefixes.Count)  # number of items to receive the same prefix
    $offset    = 0
    foreach ($prefix in $prefixes) {
        for ($i = 0; $i -lt $batchSize; $i++) {
            $index = $offset * $batchSize + $i
            $filesB[$index] | Rename-Item -NewName ('{0}_{1}.csv' -f $prefix, $filesB[$index].BaseName) -WhatIf
        }
        $offset++
    }
    # what to do with files that may remain?
    # this is one way of renaming them using random prefixes from the $prefixes array
    $remaining = $filesB.Count - $offset * $batchSize
    if ($remaining) {
        Write-Host "Renaming $remaining item(s) randomly" -ForegroundColor Yellow
    }
    while ($remaining -gt 0) {
        $randomPrefix = Get-Random $prefixes
        $filesB[-$remaining] | Rename-Item -NewName ('{0}_{1}.csv' -f $randomPrefix, $filesB[-$remaining].BaseName) -WhatIf
        $remaining--
    }
}
else {
    Write-Warning "No files found in FolderA. This would result in a 'Divide by Zero error'"
}

在 FolderB 中使用 11 个文件(11 不会平均除以 2,因此还有剩余的文件要重命名(,结果为:

What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-A.txt Destination: D:FolderB12345-ABC_NO_1111-A.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-B.txt Destination: D:FolderB12345-ABC_NO_1111-B.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-C.txt Destination: D:FolderB12345-ABC_NO_1111-C.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-D.txt Destination: D:FolderB12345-ABC_NO_1111-D.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-E.txt Destination: D:FolderB12345-ABC_NO_1111-E.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-F.txt Destination: D:FolderB67890-DEF_NO_1111-F.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-G.txt Destination: D:FolderB67890-DEF_NO_1111-G.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-H.txt Destination: D:FolderB67890-DEF_NO_1111-H.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-I.txt Destination: D:FolderB67890-DEF_NO_1111-I.csv".
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-J.txt Destination: D:FolderB67890-DEF_NO_1111-J.csv".
Renaming 1 item(s) randomly
What if: Performing the operation "Rename File" on target "Item: D:FolderBNO_1111-K.txt Destination: D:FolderB12345-ABC_NO_1111-K.csv".

如果对结果满意,请从Rename-Item cmdlet 中删除-WhatIf开关。

最新更新