Is there a way to do something similar to what is listed below:
Try{
if(<#conditional here#>)
}
catch[ArgumentOutOfRangeException]{
}
Finally{
#Set if statement to false, and run else statement
}
This is what the code currently looks like, imagine this inside of a standard runspacePool:
try {
$substring = $fileString.Substring(19, 2)
}
catch [System.ArgumentOutOfRangeException] {
$errorMessage = $_.Exception.Message
$errorOccurred = $true
Copy-Item -Path "$($copyFilePath)\$($_)" -Destination $copyFileBadNameDestination -Recurse -Force -Verbose
continue
}
if($substring -match '[0-9][0-9]'){
if(<#Filters that rely on the above if statement#>){
Copy-Item -Path "$($copyFilePath)\$($_)" -Destination $copyFileBadNameDestination -Recurse -Force -Verbose
}
}
else{
Copy-Item -Path "$($copyFilePath)\$($_)" -Destination $copyFileBadNameDestination -Recurse -Force -Verbose
}
The problem is I was previously using an if statement to do this and it made the code extremely inefficient, this in theory would make it so instead of looping through every file before managing them anyways, I could just pick out files that don't meet the file name length requirement that would cause an error with a substring, and still cast them to the else statement where they belong, instead of them getting skipped over.