Author Posts

November 17, 2017 at 10:50 pm

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.

November 17, 2017 at 11:24 pm

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/

November 18, 2017 at 3:24 am

$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
}

November 19, 2017 at 4:19 pm

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

November 20, 2017 at 8:37 pm

Thank you everyone. Greatly appreciated.

November 20, 2017 at 8:46 pm

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