JK1993
JK1993

Reputation: 138

Powershell to loop over csv's and place them into a new folder using its original name

I've wrote out the below powershell:

$Headers = @(
 "Purchase Order"
  "SKU"
  "Markdown"
  "Landed Cost"
  "Original Price"
  "Current Sale Price"
  "Free Stock"
  "OPO"
  "ID Style"
  "Supplier Style No")


$Location = "\\dc1\shared\BI\HisRms"
$Destination = "C:\Users\jonathon.kindred\Desktop\RMM\"
$files = Get-ChildItem $location -Recurse -Filter "*.csv"


Foreach ($file in $files) { Select $Headers | Export-Csv Join-Path "C:\Users\jonathon.kindred\Desktop\RMM\" $_.FullName -Force -NoTypeInformation}

The issue i have is that it fails on all files because i am not hard coding the file path to have the .csv at the end. How do it do it so i Join-Path the $Destination and the original file name?

Upvotes: 0

Views: 29

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

Within your foreach loop, you can use the following:

Export-Csv -Path (Join-Path $Destination $file.Name) -NoType

Get-ChildItem returns FileInfo objects when querying for files. $files contains those objects in your code. Each object contains properties FullName, BaseName, and Name. FullName contains the file name, the path to the file, and the extension. BaseName contains the name of the file without the path and extension. Name contains the name of the file with the extension but excludes the path.

Upvotes: 1

Related Questions