RegEx question

Welcome Forums General PowerShell Q&A RegEx question

This topic contains 5 replies, has 4 voices, and was last updated by

1 year, 4 months ago.

  • Author
  • #84508

    Points: 0
    Rank: Member

    Hello everyone,

    I have two file formats below that I need to copy to their respective folders 8323434 and 869343434.1.

    What is the RegEx expression for just the number portion (ie. 8323434 and 869343434.1)?

    file 1: SOMEWORD 8323434.pdf
    file 2: SOMEWORD 869343434.1.pdf

    Also, if you could point me to to a good resource on building RegEx, that would be greatly appreciated. Thank you in advance.

  • #84515

    Points: 1,165
    Helping Hand
    Rank: Community Hero

    You should try to ask your favorite internet search engine. Regular expression are not secret science. There are tons of information to find in internet ...

  • #84526

    Points: 58
    Rank: Member
    $folder = Get-ChildItem .\path\to\folder -Filter *.pdf |
    Select-Object fullname,basename
    # Get number from filename then create folder
    # New folders are created in files' original location
    foreach ($f in $folder){
    $f.basename -match '\d+.*' | Out-Null
    $newfolder = Join-Path -Path ($f.FullName | Split-Path) -ChildPath $Matches[0]
    If (-NOT(Test-Path $newfolder)) {New-Item $newfolder -ItemType Directory | Out-Null}
    Move-Item -Path $f.FullName -Destination $newfolder
  • #84554

    Points: 741
    Helping Hand
    Rank: Major Contributor

    Here's an interactive regex tool:

  • #84650

    Points: 0
    Rank: Member

    Thank you everyone. Greatly appreciated.

  • #84652

    Points: 0
    Rank: Member

    The above script works great. Just a small thing. How do I have it so the files SOMEWORD 8323434.pdf go to a folder 8323434.1 instead of 8323434?

    All files SOMEWORD 8323434.pdf >> folder 8323434.1

    All files SOMEWORD 869343434.1.pdf >> folder 869343434.1

    All files SOMEWORD 565643434.2.pdf >> folder 565643434.2

    so on....

    Many thanks..

The topic ‘RegEx question’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort