如何将文件夹中的文件关联为PowerShell中的其他文件?
我有文件夹A和B包含一些文件。未分配这些文件夹中的文件总数。 我想更改文件夹B中某个文件的名称,我想更改的总文件数取决于文件夹A中的总文件数。如果除以文件夹A,则为文件夹B的平均值 例如: 文件夹A文件(2个文件): ID_12345-ABC.txt ID_67890-DEF.txt 文件夹B文件(10个文件): NO_1111-A.txt NO_1111-B.txt NO_1111-C.txt NO_1111-D.txt NO_1111-E.txt NO_1111-F.txt NO_1111-G.txt ... NO_1111-J.txt 然后,我想更改文件夹B中的文件名。在这种情况下,我会将文件夹B中的每5个文件分配给文件A。 在文件夹B中,我将使用此文件名 12345-ABC_NO_1111-A.txt 12345-ABC_NO_1111-B.txt 12345-ABC_NO_1111-C.txt 12345-ABC_NO_1111-D.txt 12345-ABC_NO_1111-E.txt 67890-DEF_NO_1111-F.txt 67890-DEF_NO_1111-G.txt ... 67890-DEF_NO_1111-J.txt 12345-ABC_编号_1111-A.txt 12345-ABC_编号_1111-B.txt 12345-ABC_编号_1111-C.txt 12345-ABC_编号_1111-D.txt 12345-ABC_编号_1111-E.txt 67890-DEF_NO_1111-F.txt 67890-DEF_NO_1111-G.txt ... 67890-DEF_NO_1111-J.txt如何将文件夹中的文件关联为PowerShell中的其他文件?,powershell,Powershell,我有文件夹A和B包含一些文件。未分配这些文件夹中的文件总数。 我想更改文件夹B中某个文件的名称,我想更改的总文件数取决于文件夹A中的总文件数。如果除以文件夹A,则为文件夹B的平均值 例如: 文件夹A文件(2个文件): ID_12345-ABC.txt ID_67890-DEF.txt 文件夹B文件(10个文件): NO_1111-A.txt NO_1111-B.txt NO_1111-C.txt NO_1111-D.txt NO_1111-E.txt NO_1111-F.txt NO_1111-
任何人都可以告诉我这个案例的情况吗?您可以通过以下方式进行:
# 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:\FolderB\NO_1111-A.txt Destination: D:\FolderB\12345-ABC_NO_1111-A.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-B.txt Destination: D:\FolderB\12345-ABC_NO_1111-B.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-C.txt Destination: D:\FolderB\12345-ABC_NO_1111-C.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-D.txt Destination: D:\FolderB\12345-ABC_NO_1111-D.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-E.txt Destination: D:\FolderB\12345-ABC_NO_1111-E.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-F.txt Destination: D:\FolderB\67890-DEF_NO_1111-F.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-G.txt Destination: D:\FolderB\67890-DEF_NO_1111-G.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-H.txt Destination: D:\FolderB\67890-DEF_NO_1111-H.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-I.txt Destination: D:\FolderB\67890-DEF_NO_1111-I.txt".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-J.txt Destination: D:\FolderB\67890-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:\FolderB\NO_1111-A.txt Destination: D:\FolderB\12345-ABC_NO_1111-A.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-B.txt Destination: D:\FolderB\12345-ABC_NO_1111-B.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-C.txt Destination: D:\FolderB\12345-ABC_NO_1111-C.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-D.txt Destination: D:\FolderB\12345-ABC_NO_1111-D.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-E.txt Destination: D:\FolderB\12345-ABC_NO_1111-E.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-F.txt Destination: D:\FolderB\67890-DEF_NO_1111-F.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-G.txt Destination: D:\FolderB\67890-DEF_NO_1111-G.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-H.txt Destination: D:\FolderB\67890-DEF_NO_1111-H.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-I.txt Destination: D:\FolderB\67890-DEF_NO_1111-I.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-J.txt Destination: D:\FolderB\67890-DEF_NO_1111-J.csv".
Renaming 1 item(s) randomly
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-K.txt Destination: D:\FolderB\12345-ABC_NO_1111-K.csv".
如果对结果感到满意,请从
重命名项
cmdlet中删除-WhatIf
开关。从文件夹a中的文件创建要添加到文件夹B中文件的前缀列表。然后将文件夹B中的文件放入数组,并使用for
循环更改文件名。根据循环计数器的值指定前缀。哇。。非常感谢你。它起作用了。但我有一个问题,如果文件夹a中的总文件除以文件夹B有Mod,Mod不能重命名它。重命名后还需要更改扩展名文件。你能帮帮我吗$Theo@Job当然,你希望新的扩展是什么?你想对保留的文件(Mod文件)做什么?扩展名是.csv,如果文件保留了,它将随机分配@Theo@job通过随机分配,您的意思是代码应该使用PowerShell控制台中FolderA?@job中的文件名中的随机名称对其进行重命名,请多次输入“a”、“b”| Get random
。您应该看到随机返回的a
或b
# 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'"
}
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-A.txt Destination: D:\FolderB\12345-ABC_NO_1111-A.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-B.txt Destination: D:\FolderB\12345-ABC_NO_1111-B.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-C.txt Destination: D:\FolderB\12345-ABC_NO_1111-C.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-D.txt Destination: D:\FolderB\12345-ABC_NO_1111-D.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-E.txt Destination: D:\FolderB\12345-ABC_NO_1111-E.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-F.txt Destination: D:\FolderB\67890-DEF_NO_1111-F.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-G.txt Destination: D:\FolderB\67890-DEF_NO_1111-G.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-H.txt Destination: D:\FolderB\67890-DEF_NO_1111-H.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-I.txt Destination: D:\FolderB\67890-DEF_NO_1111-I.csv".
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-J.txt Destination: D:\FolderB\67890-DEF_NO_1111-J.csv".
Renaming 1 item(s) randomly
What if: Performing the operation "Rename File" on target "Item: D:\FolderB\NO_1111-K.txt Destination: D:\FolderB\12345-ABC_NO_1111-K.csv".