Reputation: 5
I have a pdf file test.pdf and a TXT/CSV file CSVTest.txt. I am trying to take the values from the TXT/CSV ("1234", "this", "file") and within PowerShell rename the test.pdf to 1234_this_file_test.pdf. I cannot figure out how to get this to work. Here is what I have so far
$files = "C:\Work\SouthParchment"
$temp = Import-Csv "C:\Work\SouthParchment\CSV\*.txt"
#$files - ls*.csv
$files |ForEach-Object{
$fullname = $_.FullName
Write-Output $fullname
#$temp = Import-Csv $_.FullName -Delimiter ";"
$temp = Import-Csv "C:\Work\SouthParchment\CSV\*.txt" -Delimiter ";"
$newname = ($temp|select -first 1)."Document Type" + ".pdf"
#Rename-Item -Path $fullname -newname $newname -WhatIf
Rename-Item $fullname -newname $newname -WhatIf
}
Upvotes: 1
Views: 138
Reputation: 4694
To answer your immediate question, concatenating the columns using -join
will give you the new name you're after based off the imported csv:
$path = "C:\Work\SouthParchment"
Import-Csv -Path "$path\*.txt" -Header "one","two","three" |
ForEach-Object -Process {
$newName = $_.PSObject.Properties.Value -join "_"
Rename-Item -Path "$path\*.pdf" -NewName $newName -WhatIf
}
The issue comes with the following...
Randomness - the way you're calling your csv, and then renaming your pdf, you're assuming there is only one csv, and one pdf in your current directory.
The next would be the rows in your csv. You say you plan on doing this for more pdf's but, how will it know which one to do next?
Would suggest looking into my bullet points and coming up with a better solution on how to implement this.
Upvotes: 1