Share via

Formating file name

Callum Treadwell 21 Reputation points
Sep 15, 2022, 10:29 AM

Hello,
can you kindly advise if this is possible?

We need to print in number order rather than name order.
The file name is currently in the format <Name> <Store number>. We'd like it to be in <Store number> <Name>. Can you kindly support?241418-screenshot-2022-09-15-112832.png

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,605 questions
0 comments No comments
{count} votes

4 answers

Sort by: Oldest
  1. Anshul Soni 1 Reputation point Microsoft Employee
    Sep 15, 2022, 11:00 AM
    0 comments No comments

  2. SChalakov 10,571 Reputation points MVP
    Sep 15, 2022, 11:01 AM

    Hi @Callum Treadwell ,

    this should do the job:

    $AllFiles = Get-ChildItem "C:\Temp" -Filter *.PDF  
    Foreach ($File in $AllFiles) {  
    $FileName = $File.Name  
    $StoreNumber = $FileName -replace "[^0-9]"  
    $StoreName = ($FileName).Substring(0,$FileName.IndexOf($StoreNumber))  
    $NewFileName = $StoreNumber + " " + $Storename + ".pdf"  
    Rename-Item -Path $File.Fullname -NewName $NewFileName  
    }  
    

    This is of course under the consideration that all files follow the same naming convention - "StoreName StoreNumber".

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you!)
    Regards
    Stoyan Chalakov

    0 comments No comments

  3. Rich Matheisen 47,506 Reputation points
    Sep 15, 2022, 3:21 PM

    You can try this:

    # just some test data  
    $x = [PSCustomObject]@{BaseName='Anlaby 4688';FullName='c:\junk\Anlaby 4688'},  
    [PSCustomObject]@{BaseName='Archway 4028';FullName='c:\junk\Archway 4028'},  
    [PSCustomObject]@{BaseName='Arnos Grove 4952';FullName='c:\junk\Arnos Grove 4952'}  
      
    $x |        # Replace this with your choice of file selection  
        ForEach-Object{  
            [PSCustomObject]@{  
                SortBy = ($_.BaseName -replace "^(.+?)\s(\d{4})$", '$2$1')  
                Orig   = $_  
            }  
        } |  
            Sort-Object -Property SortBy |  
                Select-Object -Expand Orig  
    
    0 comments No comments

  4. Limitless Technology 44,566 Reputation points
    Sep 21, 2022, 8:57 AM

    Hello there,

    You can use the -replace operator together with a regular expression pattern to remove all strings from the start of the file name:

    Get-ChildItem -Path C:\All -Filter *.pdf |
    Rename-Item -NewName {$_.Name -replace '^\d+'}

    A Windows PowerShell script for renaming files. https:// gist.github.com/csharpforevermore/94e6c1c4ca0ca1c2a91916fcd704dc92

    -----------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–


Your answer

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