Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

PowerShell: Compare two directories with slighly different naming conventions.

Tags: powershell

Quick Copy / Paste Answer

This works for a specific use case in which the two directories are src and test\unit, and in which the suffix to ignore is .spec.

(dir .\src\ | % { $_.BaseName } ) | compare (dir .\test\unit\ | % { $_.BaseName -replace ".spec", "" } )

Situation:

Two directories. One contains source files, the other contains test files.

./src/

client-model.ts
http-discovery-service.ts
id-token-model.ts
id-token-parser.ts
implicit-flow-service.ts
placeholder.ts

./test/unit

client-model.spec.ts
http-discovery-service.spec.ts
id-token-model.spec.ts
id-token-parser.spec.ts
implicit-flow-service.spec.ts

Approach:

Compare the base names of both directories, replacing the ".spec" suffix in the test directory. Note: in PowerShell >> represents a new line of the same command.

PS (dir .\src\ | % { $_.BaseName } ) | 
>> compare (dir .\test\unit\ | 
>> % { $_.BaseName -replace ".spec", "" } )

Output:

The placeholder exists in one directory but not the other. Good.

InputObject SideIndicator
----------- -------------
placeholder =>