Due to huge interest in this workflow (Benjamin asked a question about it
), here is an updated version.
This Version allows one to deliver one source file that matches a pattern to multiple destinations.
Content of file c:\temp\EMISSIONS.csv (please use Excel or such to modify the csv!):
Code: Select all
IncludePattern,ExcludePattern,OutputPath1,OutputPath2,OutputPath3,OutputPath4,OutputPath5,OutputPath6
*.*,foo,C:\temp\default
*3*|*4*,,C:\temp\3,c:\temp\4
*5* | *6*,,C:\temp\5,c:\temp\6
Explaination: You could add as many OutputPath's as you like, just make sure each one has a unique number, e.g. OutputPath7
Content of file c:\temp\checkpattern.ps1
Code: Select all
$debug = 0 # set to 1 in order to see logdebug messages
$filename = $args[0]
$csvfile = $args[1]
$Outputpath = @() # outputpath remains empty unless filename matches the include pattern but does not match exclude pattern
function logdebug($linenum,$what){
#in production we must print the outputpath only, so debug can just be enabled for testing on commandline
if ($debug){
Write-Host("csv line $linenum $what")
}
}
$linenumber = 2;
$havematch = 0
Import-Csv $csvfile | ForEach-Object {
logdebug $linenumber,":"
$includeArray = $_.IncludePattern.Split("|")#IncludePattern is a headline in the csv, as well as ExcludePattern and OutputPath
#iterate include patterns
foreach ($e_ptrn in $includeArray){
if ($e_ptrn.Trim() -eq ""){
logdebug $linenumber,"Include Pattern was empty"
continue
}
if ($filename -like $e_ptrn.Trim()){
if ($havematch -eq 1){
#reset output path if another line already matched - ensure only last matching line defines output
$Outputpath = @()
}
$havematch = 1 # make sure only the last matched line in the csv counts
logdebug $linenumber,"Filename matches include pattern $pattern"
$csvline = $_
foreach ($property in $csvline.PSObject.Properties){
if($property.Name -like 'OutputPath*'){
if ($property.Value){
if ($property.Value.Trim() -ne ""){ #only add if after removing whitespaces, it is not empty
$Outputpath += ($property.Value)
}
}
}
}
}
}
$excludeArray = $_.ExcludePattern.Split("|")
#iterate exclude patterns
foreach ($e_ptrn in $excludeArray){
if ($e_ptrn.Trim() -eq ""){
logdebug($linenumber,"Exclude pattern was empty")
continue
}
if ($filename -like $e_ptrn.Trim()){
if ($havematch -eq 1){
$Outputpath = @()
$havematch = 0;
}
logdebug($linenumber,"Excluding File because it matches exclude pattern $e_ptrn")
}
}
$linenumber = $linenumber+1
}
if ($Outputpath.length -eq 1){
$Outputpath[0] = $Outputpath[0] | ConvertTo-Json
$Outputpath[0] = '[' + $Outputpath[0] + ']'
Write-Host $Outputpath #finally write the found path to stdout. if nothing was found this will print just empty
}else{
$outputjson = $Outputpath | ConvertTo-Json -Compress
Write-Host $outputjson #finally write the found path to stdout. if nothing was found this will print just empty
}
NOTE: to run this workflow, you must download and install the Foreach Processor from here.
https://ffastrans.com/wiki/doku.php?id= ... processors
(sidenote, in future release 1.3, a foreach processor is part of ffastrans but for this workflow you would still need to install the plugin, it has a different nodeid)
Finally, the corresponding Workflow:
Please make sure you use other paths than C:\temp for the final productive workflow, all you need to do is to change the path of the ps1 and csv file in the command executor processor.