Powershell and regex: List of Notepad++ "backup on save" files. Edit name, sort by lastwritetime
I'd use a RegEx with zero length lookbehind assertion to remove everything after html
from $_.Name
This can be done with a calculated property either in a Select-Object
or also in a Format-*
Get-ChildItem -File |
Format-Table @{n='foo';e={$_.Name -replace '(?<=^.*\.html).*$'}},Name -HideTableHeaders
Sample output:
available13.html available13.html.2019-03-26_081523.bak
index.html index.html.2019-03-26_081538.bak
You can add a new property with Add-Member
like this
$time = (Get-Date).AddDays(-4)
$files = gci * -include index*,avail* | where {$_.LastWriteTime -gt $time}
foreach ($f in $files) {
$f | Add-Member noteproperty newName -Value `
$f.Name.Substring(0, $f.Name.Length - ".yyyy-mm-dd_iiiiii.bak".Length)
}
$files | Format-Table -HideTableHeaders newName,Length,LastWriteTime
Note that the above snippet assumes that your names always end with .yyyy-mm-dd_iiiiii.bak
. If they have some other format then you must include that information in the question, and you may need to use other string methods like replace, substring... to remove the unnecessary part