Home > Back-end >  Powershell - Need Regex to get folders with number in names
Powershell - Need Regex to get folders with number in names

Time:03-22

I'm new in Regex

I have following folder structure:

Directory: C:\Backup\Pictures

Mode                 LastWriteTime         Length Name                                                                                                          
----                 -------------         ------ ----                                                                                                          
d-----        22.3.2022.     13:17                2020                                                                                                          
d-----        22.3.2022.     13:17                2021                                                                                                          
d-----        22.3.2022.     13:17                2022                                                                                                          
d-----        22.3.2022.     13:24                archives                                                                                                      
d-----        22.3.2022.     13:18                listing   

Inside of each folder with year as name i have subfolders which represents months and days, like below:

C:\Backup\Pictures\2020\07\24
C:\Backup\Pictures\2020\08\24
C:\Backup\Pictures\2020\09\24
C:\Backup\Pictures\2021\07\24
. . . . .  . . . .  . . .  .

I need expression to look only in subfolders with days, for example to look on subfolder 24 in C:\Backup\Pictures\2020\07\

I have expression which recognizes if folder name contains 4 numbers but don't know how to expand it to search for 2 subfolders which have 2 digits in name

C:\Backup\Pictures\XXXX - done
C:\Backup\Pictures\XXXX\XX\XX - need expression


Get-ChildItem 'C:\Backup\Pictures\' -recurse | where {{ $_.DirectoryName -match '^\d4$' }}

But need expression which needs to search folders with following patterns: C:\Backup\Pictures\4 digits\2 digits\2 digits

CodePudding user response:

Instead of -Recurse, simply call Get-ChildItem with a wildcard path that resolves to the target paths:

Get-ChildItem -Path .\20[0-9][0-9]\[0-9][0-9]\[0-9][0-9]
  • Related