# Sort files in folders by version

**URL:** <https://forums.powershell.org/t/sort-files-in-folders-by-version/20086>\
**Category:** PowerShell Help\
**Created:** [August 5, 2022, 9:06am UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086 "2022-08-05T09:06:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![KRustam72](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krustam72/32/2618_2.png) [@KRustam72](https://forums.powershell.org/u/KRustam72)\
**Post date:** [August 5, 2022, 9:06am UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/1 "2022-08-05T09:06:43Z")

</div>

Hi All.  
I am PowerShell beginner , kindly ask you to help me with this non-trivial task.  
I have full backup of the SharePoint Libraries including all documents revisions and versions.  
For example:

LoDs TM1\_Project and Engineering.8.0.xlsx  
LoDs TM1\_Project and Engineering.9.0.xlsx  
LoDs TM1\_Project and Engineering.10.0.xlsx  
LoDs TM1\_Project and Engineering.11.0.xlsx

The version value may be a 1, 2, 3 or 4 digit  
How to select and copy only the latest version of each file?  
It would be LoDs TM1\_Project and Engineering.11.0.xlsx from example.  
Sorting by date not applicable.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 5, 2022, 9:19am UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/2 "2022-08-05T09:19:24Z")

</div>

K Rustam72,  
Welcome to the forum. 👋🏽

That’s actually a very ~~non~~ -trivial task. There are in fact more than enough examples out there.

What have you tried so far? We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Assuming the latest version is the one with last write access you should be able to sort for the property `LastWriteTime` and pick the first or the last depending on the sorting order. 😉

---

<div class="post-metadata">

**Author:** ![KRustam72](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krustam72/32/2618_2.png) [@KRustam72](https://forums.powershell.org/u/KRustam72)\
**Post date:** [August 5, 2022, 11:31am UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/3 "2022-08-05T11:31:01Z")

</div>

Yes, you are right.  
This is my script

```auto
Get-ChildItem -Path "\\path" -Recurse |
Sort-Object -Property LastWriteTime -Descending | 
Export-Csv -Path "E:\Apps\TM.csv" -Encoding UTF8

```

but is still have just list of files sorted by time, millions files 😵‍💫  
So how can I select and copy to another folder only the last modified files?

---

<div class="post-metadata">

**Author:** ![KRustam72](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krustam72/32/2618_2.png) [@KRustam72](https://forums.powershell.org/u/KRustam72)\
**Post date:** [August 5, 2022, 11:46am UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/4 "2022-08-05T11:46:42Z")

</div>

Piping Select-Object -first 1

```auto
Get-ChildItem -Path "\\path" -Recurse |
Sort-Object -Property LastWriteTime | Select-Object -first 1 | Export-Csv -Path "E:\Apps\TM.csv" -Encoding UTF8

```

returns only one the latest file from whole directory and also csv report becames unreadable

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 5, 2022, 12:12pm UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/5 "2022-08-05T12:12:52Z")

</div>

You have to search for duplicates. But it is up to you to decide what’s a _valid_ duplicate. If your files follow a naming convention it should be easy.

Assumed your files names all look similar to the ones you posted you could split the file names on the first period and use the first part of the file name to group the result. Then you can pick only the newest file from each group and you’re done. 😉

You may read more about

> **[Group-Object (Microsoft.PowerShell.Utility) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object?view=powershell-7.2)**
>
> The Group-Object cmdlet displays objects in groups based on the value of a specified property. Group-Object returns a table with one row for each property value and a column that displays the number of items with that value. If you specify more than...

You should always read the complete help topic including the examples to learn how to use it.

---

<div class="post-metadata">

**Author:** ![KRustam72](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krustam72/32/2618_2.png) [@KRustam72](https://forums.powershell.org/u/KRustam72)\
**Post date:** [August 5, 2022, 12:20pm UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/6 "2022-08-05T12:20:40Z")

</div>

The problem is that all files doesn’t locates in one folder and doesn’t have similar filenames.  
The root directory contains thousands child folders. My example is just short view from one folder.  
TM\Construсtion\Documents\Documents\Project Control\LIST OF PROJECTS 2021-2023.1706.0.xlsx ( **1705 files stays before** 🙃)  
TM\Construсtion\Documents\Documents\Project Control\Weekly Status Report of Key Projects\_May\_week\_3.6.0.xlsx  
And so on

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 5, 2022, 12:34pm UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/7 "2022-08-05T12:34:33Z")

</div>

> [@KRustam72](#):
>
> The problem is that all files doesn’t locates in one folder

That shouldn’t be a problem …

> [@KRustam72](#):
>
> and doesn’t have similar filenames

That could be a problem … 😉

At the end of the day you have to find a way to distinguish the different groups of _versioned_ files. But it will be hard if they don’t have a common attribute you can rely on. You will have to find the unique or actually the unversal pattern what separates the actual file name from the version info. For the file names you poasted last it is the first period in the first file name and the last underline for the second file name.

---

<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:17pm UTC](https://forums.powershell.org/t/sort-files-in-folders-by-version/20086/8 "2024-05-16T20:17:38Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


