このコードを使用して、このサイトでPSTファイルをコピーして名前を変更するスクリプトで見つけました。私の質問と私が問題を抱えているのは、.pstの名前を変更するときに番号が増え続けるということです。Powershell Copy-Itemファイルが存在する場合に名前を変更
たとえば、 "test.pst"という名前のファイルが見つかった場合はそのままコピーします。 "test.pst"という名前の別のファイルが見つかった場合は、そのファイルをコピーして "test-1.pst"という名前に変更します。ただし、 "test2.pst"という名前の2つのファイルが見つかると、最初のファイルを "test2.pst"としてコピーし、2番目のファイルを "test2-1.pst"ではなく "test2-2.pst"にコピーして名前を変更します。
コードを変更して新しい重複ファイルの番号を1(test3-1.pst、test4-1.pstなど)に変更する方法について提案がありますか?
$csv = import-csv .\test.csv
foreach ($line in $csv) {
New-Item c:\new-pst\$($line.username) -type directory
$dest = "c:\new-pst\$($line.username)"
$i=1
Get-ChildItem -Path $line.path -Filter *.pst -Recurse | ForEach-Object {
$nextName = Join-Path -Path $dest -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $dest ($_.BaseName + "_$i" + $_.Extension)
$i++
}
$_ | copy-Item -Destination $nextName -verbose
}
}
ああ、これは単純なものである:あなたのようなあなたの
ForEach
ループ内に$i = 1
線を移動する必要があります。 '$ i = 1'行を' ForEach'ループの中に移動します。 – TheMadTechnician