Reputation: 115
I have some code that loops through a CSV and renames files, but I'm unable to get the Move-Item part to work. Any help would be appreciated. Thanks
$lines = Import-Csv $extractFile
foreach($line in $lines){
get-childitem C:\Docs\* | where {$_.name -eq $line.OldFileName} | Rename-Item -NewName $line.NewFileName | Move-Item -Destination C:\Renamed
}
Upvotes: 0
Views: 51
Reputation: 2001
assuming your csv file looks similar to mine, this worked for me. you can remove my $fileinfo section and un-comment the first line.
#$fileinfo = Import-Csv $extractFile
$fileinfo = @'
oldfilename,newfilename
test1.txt,newtest1.txt
test2.txt,newtest2.txt
'@ | convertfrom-csv
$source = 'c:\docs'
$dest = 'c:\renamed'
foreach($line in $fileinfo) {
Get-ChildItem $source -Filter $line.OldFileName | % {
Move-Item $_.FullName -Destination $(Join-Path $dest $line.NewFileName)
}
}
not tested this part, but you may be able to shorten the main line like so:
Get-ChildItem $source -Filter $line.OldFileName | Move-Item -Destination $(Join-Path $dest $line.NewFileName)
Upvotes: 1