Link to home
Start Free TrialLog in
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
David
Avatar of oBdA
oBdA

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

Avatar of David Izen

ASKER

Fantastic - thank you so much! Love the fact you took the trouble to write in a check for the 5-digit folders - brilliant!
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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thats done it, thank you very much!
Question answered.