The search for files – Part 2

It would be quite a lot to go through if I would go through the code itself.
So I’ve decided to provide some explanations and thoughts around the usage rather than the code. However if there are questions or suggestions on the code, please feel free to leave a comment. Again the link to the code can be found in part 3.

Get-FilteredFileList

As I mentioned at the end of Part 1, Get-FilteredFileList is the function that you will start with. From a modularization point of view this is the main function that then calls the other functions as needed.

Note:
If you check with Get-Help on this function you’ll notice that there is a default path for the QuickIO.Net library. You can use the QuickIOPath parameter to use a different path, otherwise the functions will assume the default path.

Lets start with a simple example.

Get-FilteredFileList -FilePath 'c:\temp' -RandomExtension 6 -Recursive

This will search recursively through the path ‘c:\temp’ and look for any file with an extension of 6 characters. The result is essentially what I started with in the original QuickIO.Net post.

Get-FilteredFileList -FilePath 'c:\temp' -SpecificExtension "aaa","ccc" -Recursive

Kind of the same type of search except we know which extensions to search for.
The SpecificExtension parameter accept a string array of extensions.

Lets look at a more complex example.

Get-FilteredFileList -FilePath 'c:\temp' -RandomExtension 3 -ExcludeExtension "txt","csv" -FileSignature "25504446"

In this example:

  • We are looking for files with an extension of 3 characters.
  • We exclude any files with an extension of “txt” or “csv”.
  • We will check each file that matches if they have the signature “25504446”.

If you want to search for any file with a specific file signature then just skip the Random/SpecificExtension paramters.

Get-FilteredFileList -FilePath 'c:\temp' -ExcludeExtension "txt","csv" -FileSignature "25504446"

I will go into more detail in regard to the file signatures in the next part.
But basically it’s the first 4 bytes of the file which most files (not all) use for storing the signature.
25 50 44 46 is the signature in HEX of a PDF file.

As you may have noticed the ExcludeExtension paramter also accepts a string array of extensions that you want to exclude. It’s worth mentioning that if you use the exclude option, those files will not be checked against the signature. E.g. if someone renames a PDF file to TXT, it will be skipped and not found in the list (using the example above of course).

Search-FileExtension

This function is a helper function that will accept the initial file list that get generated by Get-FilteredFileList.
Get-FilteredFileList will call Get-FilesQuickIO to get the files under the supplied path.
Search-FileExtension will process that list and filter it further.
As this is a helper function you don’t really call it manually as you would use Get-FilteredFileList.
You can of course review the code and examples but I won’t go into them in this post.

Test-FileMatch
The purpose of the function is really to see if the extension matches the selected criterias.
Originally this was part of the Search-FileExtension function.
But I decided to pull that code out to a seperate function to modularize the code further.

This conclude the second part of this topic.
In the third part we will look at the last two functions.

See you there.

Leave a Reply

Your email address will not be published. Required fields are marked *