RegEx question

This topic contains 5 replies, has 4 voices, and was last updated by  Andy 3 weeks, 5 days ago.

  • Author
    Posts
  • #84508

    Andy
    Participant

    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

    Olaf Soyk
    Participant

    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 ...
    https://en.wikipedia.org/wiki/Regular_expression
    https://www.regular-expressions.info/
    https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
    https://regexr.com/

  • #84526

    random commandline
    Participant
    $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

    js
    Participant

    Here's an interactive regex tool: https://regex101.com/

  • #84650

    Andy
    Participant

    Thank you everyone. Greatly appreciated.

  • #84652

    Andy
    Participant

    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..

You must be logged in to reply to this topic.