PowerShell help - comparing folder and file names

David Izen
David Izen used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

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

Author

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

Author

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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

Commented:
thats done it, thank you very much!
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Question answered.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial