Предположим, ваш CSV-файл выглядит примерно так:
"LB_Number","TaxId","ClientName"
"987654","12345","Microsoft"
"321456","91234","Apple"
"741852","81234","HP"
Столбец 1 содержит часть имени существующего файла для соответствия
Столбец 3 содержит имя клиента, к которому вы хотите добавить имя файла
Тогда ваша функция может выглядеть примерно так:
# Common Paths
$PathRoot = "C:\Temp\somefiles" # Where the files are to rename
# Get csv file
$ClientAccounts = Import-CSV -path "\\server\some\path\to\csv\file.csv"
# Loop through all clients in the CSV
foreach($client in $ClientAccounts) {
$CurrentClientLB_Number = $client.LB_Number
$CurrentClientTaxId = $client.TaxId # unused...??
$CurrentClientName = $client.ClientName
# get the file(s) using wildcards (there can be more than one)
# and rename them
Get-ChildItem -Path "$PathRoot\*$CurrentClientLB_Number*" -File | ForEach-Object {
$_ | Rename-Item -NewName ($CurrentClientName + " " + $_.Name)
}
# Curly braces work also, although this is not very common practice:
# Get-ChildItem -Path "$PathRoot\*$CurrentClientLB_Number*" -File |
# Rename-Item -NewName { ($CurrentClientName + " " + $_.Name) }
}
Я использую параметр -File
с Get-ChildItem
, поэтому функция будет возвращать только файлы; не каталоги. Если вы используете PowerShell версии 2.0, его необходимо заменить на | Where-Object { !$_.PSIsContainer }
.