Avatar of David Izen
David Izen
 asked on

PowerShell help - comparing folder and file names

Hi All

We have a server with a file structure where each client has their own folder, and the client/folder has a unique 5-digit reference, thus a client folder might be named '12345 Mr Aardvark'. This client folder likely contains many nested sub-folders. Every file within any of these folders is supposed to be prefixed with '12345', for example '12345 Welcome Letter.docx'

I'm trying to write a PowerShell script that can live at the top level of each client folder. When run, the script generates a list of every file in either the main client folder or any of its sub-folders (I'm good to here) then compares the first 5 characters of each file name with the 5 characters of the client folder name, and generates a report listing by location those files which have either been mis-filed or more likely have been saved without the 5 character identifier. This bit I am not so good at.

In a perfect world I guess the report would be run at a set time each night and a report listing every mismatch and their location placed in a single file in the root 'Clients' folder.

Any pointers gratefully accepted and I'll happily consider this a commercial project.

Thanks for any help,
Kind regards

Avatar of undefined
Last Comment

8/22/2022 - Mon

A commercial project for 8 lines of Powershell?
$Root = 'D:\Clients'
Get-ChildItem -Path $Root -Directory | Where-Object {$_.BaseName -match '\A(?<ID>\d{5})\s+'} | ForEach-Object {
	$ID = $Matches['ID']
	"Processing $($_.Name), ID '$($ID)' ..." | Write-Host
	Get-ChildItem -Path $_.FullName -File -Recurse |
		Where-Object {$_.BaseName.SubString(0, 5) -ne $ID} |
		Select-Object @{n='ID'; e={$ID}}, DirectoryName, Name, Length, LastWriteTime
} | Export-Csv -NoTypeInformation -Path "$($Root)\_Mismatch.csv"

Open in new window

David Izen

Fantastic - thank you so much! Love the fact you took the trouble to write in a check for the 5-digit folders - brilliant!
David Izen

Sorry to be a pain but trying to implement this and our setup is slightly different to what i thought
So for instance it may be something similar to;

So a letter followed by 5 numbers and not 5, and then the hyphen (-) so no spacing either
i have tried to edit the above script to accommodate this but cant quite get it working
Any suggestions?

Many thanks
Your help has saved me hundreds of hours of internet surfing.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Izen

thats done it, thank you very much!

Question answered.