Can this be made?

I have something i want to make with powershell. I’ll try to explain. I have a folder with 70000 files, named 5 numbers 12345 or 54327 ect ect (in fact they are .doc .xls of .pdf but i can’t see).

I also have 1 .csv with 4 columns. “name”, “client-number” ,“the number” of the corresponding file from the folder i mentioned and the “extention”. So i would like to make a script that looks in the .cvs to match the numbers with the files from the folder. On a match it should rename the filename with “bsn+name”. It also has to change the extention of the file with the corresponding extention mentioned in the .csv

So \folderxx\123456 becomes \folderx\123456798clientname.doc
Can this be done? And how do i do this?

https://social.technet.microsoft.com/Forums/windows/en-US/60fbdab5-ccab-4d03-982d-2187b1653d52/can-this-be-made?forum=winserverpowershell :wink:

I have attempted your problem with limited time available but enjoy something new in PowerShell. Some very experienced scripters who contribute here may pull this apart / modify / improve. I am still learning my way but wanted to try and contribute - or give you something to possibly start with. The below would need testing in your environment and need some error checking wrapped around it. I have made some assumptions on your file format. I have used a C:\temp\files as a location for the files, and a dummy CSV file.

$fileList=Get-ChildItem -Path $FolderPath

$FolderPath=‘C:\temp\Files’
$CSVPath=‘c:\temp\files\csv.csv’

$DB = Import-Csv $CSVPath

$DBCount=($db.Count)

for($i=0; $i -lt $DBCount; $I++){

$targetNumber = $db[$i].number

if($targetNumber){

    Get-ChildItem -Path $FolderPath -Filter "$targetNumber.*" | Rename-Item -NewName ($($DB[$i].name)+ ($($db[$i].clientnumber) + $($db[$i]).extension ))
             
    }
}

Thank you Andrew. Your answer really helpt me started. I’m new at programming but i got most of it working. But
I also have a column category in my csv. After renaming the file i’d like to move the file to Basefolder$category. I can not get it to work. Can it be done with piping after the rename-item?

example here

Get-Item C:\111 | Rename-Item -NewName "222" -PassThru | Move-Item -Destination D:\333\

the key is -PassThru

Hello Max,
Thank you for your help.

My problem is that the destination folder is in the csv file.

Name,cltnr,volnaam,categorie,extension
84897,123456789,example-file,file-folder,pdf

Rename-Item -NewName ($($DB[$i].name)+ ($($db[$i].cltnr) + “.” + $($db[$i]).extension )) -PassThru | Move-Item -Destination $FolderPath + "" + ($($DB[$i].categorie)) does not work

I also tried Join-Path but this does not work

you just need to take destination into brackets. and not need to use variables with $() - its need only inside strings

#instead of
$FolderPath + "\" + ($($DB[$i].categorie))
#use
($FolderPath + "\" + $DB[$i].categorie)
#or
(Join-Path $FolderPath $DB[$i].categorie)
#here I show $() usage but i think join-path is better. note the double quotes outside
"$FolderPath\$($DB[$i].categorie)"

I’d like to thank everyone for there support and suggestions
My script is done :slight_smile:
Thanx

Would you mind sharing the finished script? I’m having a similar situation and would love to have a look.

This is the script i ended up with… :slight_smile:

$fileList=Get-ChildItem -Path $FolderPath

$FolderPath=‘C:\temp\Files’
$CSVPath=‘c:\temp\files\csv3.csv’

$DB = Import-Csv C:\temp\files\csv3.csv -delimiter ‘;’

$DBCount=($db.Count)

for($i=0; $i -lt $DBCount; $I++){

$targetNumber = $db[$i].number
#write-host = $targetnumber
$path1 = $DB[$i].bsn
$path4 = $DB[$i].naam
$path5 = $DB[$i].categorie
$path3 = ($path1) + (“@”) + ($path4) + "" + ($path5)
$Path2 = (Join-Path $FolderPath $DB[$i].categorie)
#write-host = $path1
#write-host = $path4
write-host = $path3
#write-host = $path2

if($targetNumber){

Get-ChildItem -Path $FolderPath -Filter “$targetNumber.*” | Rename-Item -NewName ($($DB[$i].volnaam)+ ($($db[$i].cltnr) + “.” + $($db[$i]).extentie )) -PassThru |Move-Item -Destination $path3
}
}