# Update CSV files with the part of the file name itself.

**URL:** <https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061>\
**Category:** PowerShell Help\
**Created:** [February 15, 2017, 11:15am UTC](https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061 "2017-02-15T11:15:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![theelk81](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@theelk81](https://forums.powershell.org/u/theelk81)\
**Post date:** [February 15, 2017, 11:15am UTC](https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061/1 "2017-02-15T11:15:32Z")

</div>

Hi guys,

first time poster, any help would be great.

I’m looking for a script that can update a number of CSV files in a folder, inserting part of the name of the file itself (which is a date).

For example the files are called:-  
Load-01-Feb-2017.csv  
Load-31-Jan-2017.csv  
Load-30-Jan-2017.csv

The contents of the files contain:-  
Load1,Clientname,Servername  
Load2,ClientName,Servername

I would like to be able to insert the date somehow into the current format so it looks like the following for the file created on the 01-Feb-2017:-  
Load1,Clientname,Servername,01-Feb-2017  
Load2,ClientName,Servername,01-Feb-2017

I then plan to merge them which is easy enough I just need the dates added before I do.

Thanks for any tips or pointers.

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [February 15, 2017, 11:45am UTC](https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061/2 "2017-02-15T11:45:16Z")

</div>

Hi Barry,

I’ve created a quick example which could work for you.

```
param (
    [System.String]
    $Path = 'C:\Input\Load-*',

    [System.String]
    $OutputFilePath = 'C:\Output\Combined.csv'
)

Get-ChildItem -Path $Path -File -PipelineVariable loadFile | 
    ForEach-Object {
        # Split the filename (basename) without the extension into two items
        $fileDate = ($loadFile.BaseName.Split('-', 2)[1])
        
        Get-Content -Path $loadFile.FullName | ForEach-Object { "$PSItem,$fileDate" }
    } | Out-File -FilePath $OutputFilePath
```

It gets a list of files (Get-ChildItem), iterates over the list (ForEach-Object), splits the filename (basename) into two parts (Load and xx-xxx-xxxx), loads each file into an array lines (Get-Content), iterates over the array of lines (2nd ForEach-Object), appends the file date extracted earlier to each line and outputs everything into one merged/combined file (Out-File).

---

<div class="post-metadata">

**Author:** ![theelk81](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@theelk81](https://forums.powershell.org/u/theelk81)\
**Post date:** [February 15, 2017, 12:10pm UTC](https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061/3 "2017-02-15T12:10:28Z")

</div>

Legend, thanks Daniel. Works perfectly.

Again thanks for the quick reply!

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [February 15, 2017, 2:12pm UTC](https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061/4 "2017-02-15T14:12:16Z")

</div>

I’m glad my reply was helpful :-). Thank you.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/update-csv-files-with-the-part-of-the-file-name-itself/8061/5 "2024-05-16T20:39:16Z")

</div>


