Share via

Command - find all files with oldest sufix and move it to _backup subdirectory

Salka, Milan 111 Reputation points
2026-05-26T15:06:57.1533333+00:00

Hello,

exist any possibility how to create in cmd/powershell command to find (in one directory) all pdf files, and all with oldest sufix move to _backup subdirectory.

Example. Content of directory

Name1_20260101-1010.pdf

Name1_20260301-1511.pdf

Name1_20260401-1512.pdf

Name1_20260501-1515.pdf

Name2_20240101-1009.pdf

Name2_20250301-1505.pdf

Name2_20260401-1501.pdf

Name2_20260415-1502.pdf

Name3_20220908-0914.pdf

After run command wil remain in directory

Name1_20260501-1515.pdf

Name2_20260415-1502.pdf

Name3_20220908-0914.pdf

and all of those will be moved to _backup subdirectory

Name1_20260101-1010.pdf

Name1_20260301-1511.pdf

Name1_20260401-1512.pdf

Name2_20240101-1009.pdf

Name2_20250301-1505.pdf

Name2_20260401-1501.pdf

Thank you.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. HLBui 6,350 Reputation points Independent Advisor
    2026-05-26T15:59:19.3233333+00:00

    Hi Salka, Milan

    You can handle this with PowerShell. The idea is to group files by their "NameX" prefix, sort them by the timestamp in the filename, keep the newest one in place, and move the rest into a _backup folder. Here’s a quick script outline you can try:

    $dir = "C:\Your\Directory\Path"

    $backup = Join-Path $dir "_backup"

    if (!(Test-Path $backup)) { New-Item -ItemType Directory -Path $backup }

    Get-ChildItem $dir -Filter *.pdf | ForEach-Object {

    $prefix = ($_ -split "_")[0]
    
    [PSCustomObject]@{
    
        Prefix = $prefix
    
        File   = $_
    
        Date   = ($_ -split "_")[1].Split(".")[0]
    
    }
    

    } | Group-Object Prefix | ForEach-Object {

    $sorted = $_.Group | Sort-Object Date
    
    $sorted[0..($sorted.Count-2)] | ForEach-Object {
    
        Move-Item $_.File.FullName $backup
    
    }
    

    }

    This way, for each prefix (like Name1, Name2, etc.), only the newest file stays in the main directory, and all older ones get moved to _backup. You can tweak the path and backup folder name as needed.

    Give it a spin it should match the example you gave. And if this does the trick for you, don’t forget to hit **“**accept answer” so we know it helped!

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-05-26T15:07:10.94+00:00

    Use PowerShell to group the files by the name prefix and keep only the newest (largest suffix), moving the rest to a _backup subfolder.

    Example script (run in the target folder):

    # Ensure backup folder exists
    $backup = Join-Path (Get-Location) '_backup'
    New-Item -Path $backup -ItemType Directory -Force | Out-Null
    
    # Get all PDFs in current directory
    $files = Get-ChildItem -Path . -Filter '*.pdf' -File
    
    # Group by the part before the first underscore (Name1, Name2, ...)
    $groups = $files | Group-Object {
        ($_ .BaseName -split '_', 2)[0]
    }
    
    foreach ($group in $groups) {
        # Sort descending by the suffix (the part after the underscore)
        $sorted = $group.Group | Sort-Object {
            ($_ .BaseName -split '_', 2)[1]
        } -Descending
    
        # Skip the newest (first), move the rest to _backup
        $toMove = $sorted | Select-Object -Skip 1
        foreach ($file in $toMove) {
            Move-Item -Path $file.FullName -Destination $backup
        }
    }
    

    How it works:

    • Get-ChildItem lists all PDF files in the directory.
    • Group-Object groups them by the prefix before _ (Name1, Name2, Name3).
    • Each group is sorted descending by the suffix (e.g. 20260501-1515), so index 0 is the newest.
    • All other files in the group are moved to the _backup folder, leaving only the newest per name in the original directory.

    To run it in another folder, either cd into that folder first or replace . in Get-ChildItem -Path . with the full path.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.