Home > Net >  Powershell: Compare filenames to a list of formats
Powershell: Compare filenames to a list of formats

Time:11-19

I am not looking for a working solution but rather an idea to push me in the right direction as I was thrown a curveball I am not sure how to tackle.

Recently I wrote a script that used split command to check the first part of the file against the folder name. This was successful so now there is a new ask: check all the files against the naming matrix, problem is there are like 50 file formats on the list.

So for example format of a document would be ID-someID-otherID-date.xls So for example 12345678-xxxx-1234abc.xls as a format for the amount of characters to see if files have the correct amount of characters in each section to spot typos etc.

Is there any other reasonable way of tackling that besides Regex? I was thinking of using multiple splits using the hyphens but don't really have anything to reference that against other than the amount of characters required in each part.

As always, any (even vague) pointers are most welcome ;-)

CodePudding user response:

Although I would use a regex (as also commented by zett42), there is indeed an other way which is using the ConvertFrom-String cmdlet with a template:

$template = @'
{[Int]Some*:12345678}-{[String]Other:xxxx}-{[DateTime]Date:2022-11-18}.xls
{[Int]Some*:87654321}-{[String]Other:yyyy}-{[DateTime]Date:18Nov2022}.xls
'@

'23565679-iRon-7Oct1963.xls' | ConvertFrom-String -TemplateContent $template
Some       : 23565679
Other      : iRon
Date       : 10/7/1963 12:00:00 AM
RunspaceId : 3bf191e9-8077-4577-8372-e77da6d5f38d
  • Related