Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell – Need Regex to get folders with number in names

I’m new in Regex

I have following folder structure:

Directory: C:\Backup\Pictures

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading