This snippet can be used to search for a file pattern inside a directory and then execute a script for matching files using multiple threads.
On Linux, I typically use fd to perform this action; the linux equivilent would be:
fdfind -j 8 -e xml -x echo {}
Get-ChildItem
is used to retrieve a list of files; ForEach-Object
is then used to loop over the files with a number of threads.
Get-ChildItem "D:\path\to\directory\*.xml" -Recurse | ForEach-Object -Parallel {
$file = $_.FullName
echo $file
} -ThrottleLimit 8
In my case, I needed to upload a large amount of XML files:
Get-ChildItem "D:\path\to\directory\*.xml" -Recurse | ForEach-Object -Parallel {
$file = $_.FullName
( curl.exe -X 'POST' 'http://192.0.2.1/api/v1/xml' -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -F "xml_file=@${file};type=text/xml" -s -f ) | out-null
} -ThrottleLimit 8