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
David
David IzenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

1
David IzenAuthor Commented:
Fantastic - thank you so much! Love the fact you took the trouble to write in a check for the 5-digit folders - brilliant!
0
David IzenAuthor Commented:
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;
A1234-Alice
A2345-Adam

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
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

oBdACommented:
For a letter followed by four digits and a hyphen, you need this:
$Root = 'D:\Clients'
Get-ChildItem -Path $Root -Directory | Where-Object {$_.BaseName -match '\A(?<ID>[A-Z]\d{4})-'} | 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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David IzenAuthor Commented:
thats done it, thank you very much!
0
oBdACommented:
Question answered.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.