Moving the highest name a file to a different folder

Good day,

I want to be able to pull the highest version file name and place it on another folder.

abrt-2.1.11-12.el7.x86.rpm

abrt-2.1.11-19.el7.x86.rpm

abrt-2.1.11-22.el7.x86.rpm

abrt-2.1.11-34.el7.x86.rpm (to new folder)

abrt-addon-ccpp-2.1.11-12.el7.x86.rpm

abrt-addon-ccpp-2.1.11-19.el7.x86.rpm

abrt-addon-ccpp-2.1.11-45.el7.x86.rpm (to new folder)

There are several thousand file names so I cannot do it by specific name.

-Cheers

 

 

Your post is extremely vague. I’ll make a few assumptions.

  • Your list is created with Get-ChildItem
  • Each filename is identical up to the version number
  • You want to copy the files to new folder instead of move
Based on these assumptions, something like this may work.
Get-ChildItem |
    Group-Object -Property {$_.name -replace '\d.*'} |
        ForEach-Object {
            $_.group | Select-Object -Last 1
        } | Copy-Item -Destination $somedestination
If any of these assumptions are incorrect, please update your post with relevant, clarifying details.

What have you tried so far? I think my approach would be:

  1. Get a list of the filenames
  2. Use a regular expression to pull out the version numbers
  3. Cast the captured version numbers to type [System.Version] for easy comparison
  4. Select the highest version number(s)
  5. Copy the file(s), using the version number with wildcards

Assuming this data is plain text, you will probably have to use regex to parse. I’m not 100% on your filename formatting but if my assumptions are right, here is how I would approach it. 1. Use regex (-replace operator) to parse each filename and break it up into 3 properties, filename, version, fullname. Then 2. Group on the Filename, and finally 3. Sort on the version selecting only the latest.

I’m pretty weak on regex, and I know there are some regex superstars in this forum so maybe someone will post a more elegant solution, but this works at least with the data set you provided.

$testdata = @'
abrt-2.1.11-12.el7.x86.rpm
abrt-2.1.11-19.el7.x86.rpm
abrt-2.1.11-22.el7.x86.rpm
abrt-2.1.11-34.el7.x86.rpm
abrt-addon-ccpp-2.1.11-12.el7.x86.rpm
abrt-addon-ccpp-2.1.11-19.el7.x86.rpm
abrt-addon-ccpp-2.1.11-45.el7.x86.rpm
'@
$testfile = New-TemporaryFile
$testdata | Set-Content -Path $testfile.FullName


$files = Get-Content -Path $testfile.FullName |
    Select-Object @{n="FileName";e={$_ -replace "-[0-9].+"}},
                  @{n="Version" ;e={$_ -replace "[a-zA-Z]+[\-]*"}},
                  @{n="Fullname";e={$_}}

$files |
    Group-Object -Property Filename | 
        ForEach-Object {$_.group | Sort-Object -Property Version -Descending | Select-Object -First 1}

 

Doug, you sir are a Genius. I have to continually update offline Red Hat clients and when pulling down a local repository it pulls down everything from the beginning of the OS release. As of last month it was over 31,000 files and roughly 64 gb. This will make everything so mush easier and I really do appreciate the help!