Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

powershell to append file extension from a CSV

I have a csv file that has one column that includes the path and file name of a file.

None of these files have extensions.

I am trying to append an extension...

I am trying this..but its not changing...

Please help


Import-Csv -path myfile.csv | ForEach-Object { Write-Host $_.FullName + ".ext" }

In this line of code I put the complete path to my csv file...that is all I changed.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Robb, the Command is not going to work. you need a Rename-item Cmdlet

try the following

Set-location "E:\TESTING" #location of the CSV Files
$files = import-csv -path "E:\TESTING\name.csv" #CSV File must have the first column as name

foreach ($file in $files)
{
    Write-host $file.name
    $newname = $file.name + ".csv"
    rename-item $file.name $newname -force
}
Hi, in the above code, i assumed the location of the files to be in one directory and no error validation.

here is a slightly improved code with host messages on the file that is being changed. Please change the path as per your requirements.

Set-location "e:\TESTING" #location of the CSV Files
$files = import-csv -path "E:\TESTING\name.csv" #CSV File must have the first column as name

foreach ($file in $files)
{
    Write-warning "This file is going to be changed $file.name"
    $newname = $file.name + ".csv"
    rename-item $file.name $newname -force
    Write-host "The following file has changed $newname"
}