Formating file name

Callum Treadwell 21 Reputation points
2022-09-15T10:29:25.103+00:00

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,504 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Anshul Soni 1 Reputation point Microsoft Employee
    2022-09-15T11:00:01.48+00:00
    0 comments No comments

  2. SChalakov 10,376 Reputation points MVP
    2022-09-15T11:01:31.11+00:00

    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 46,476 Reputation points
    2022-09-15T15:21:19.76+00:00

    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,206 Reputation points
    2022-09-21T08:57:23.223+00:00

    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.