Modify current powershell script not to output a file based on a condition

I would like to modify the current script shown below so that if it does not see 96587 in the file, then there is no need to save the script with the rev append.





[code][/$inFolder = 'C:\Temp'
$outSuffix = '_rev'
$replace_96587 = @{
      'CT' = '01-EAST'
      'FL' = '01-EAST'
      'GA' = '01-EAST'
      'IA' = '01-EAST'
      'IN' = '01-EAST'
      'NH' = '01-EAST'
      'PA' = '01-EAST'
      'NY' = '01-EAST'
      'WI' = '01-EAST'
      'CA' = '01-WEST'
      'CO' = '01-WEST'
      'TX' = '01-WEST'
      'WA' = '01-WEST'
}

$referenceCount = 0
$row = 0
Get-ChildItem -Path $inFolder -Filter *.csv | Where-Object {$_.BaseName -notmatch "$($outSuffix)\Z"} | ForEach-Object {
      $outFile = Join-Path -Path $_.DirectoryName -ChildPath "$($_.BaseName)$($outSuffix)$($_.Extension)"
      Write-Host "Processing '$($_.Name)' ..." -NoNewline
      Get-Content -Path $_.FullName | ForEach-Object {
            $row++
            $fields = $_.Split(',')
            If ($referenceCount -eq 0) {$referenceCount = $fields.Count}
            If ($fields.Count -ne $referenceCount) {
                  Throw "Field count in line $($row) is incorrect (is: $($fields.Count), expected: $($referenceCount))!"
            }
            $old3 = $fields[3]
            $old99 = $fields[99]
            If ($fields[3] -eq '96587') {
                  If ($replace_96587.ContainsKey($fields[27])) {
                        $fields[3] = $replace_96587[$fields[27]]
                  }
            }
            If ($fields[99] -match '\AAC(?<Number>0012[3-6])\Z') {
                  $fields[99] = "$($Matches['Number'])C"
            }
            "line $($row): $($old3) [$($fields[27])], $($old99) --> $($fields[3]), $($fields[99])" | Write-Host
            $fields -join ','
      } | Set-Content -Path $outFile
      Write-Host " --> '$($outFile)'"
}
code]
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
This will only write if either 96587 or AAC0012x was found and replaced:
$inFolder = 'C:\Temp'
$outSuffix = '_rev'
$replace_96587 = @{
	'CT' = '01-EAST'
	'FL' = '01-EAST'
	'GA' = '01-EAST'
	'IA' = '01-EAST'
	'IN' = '01-EAST'
	'NH' = '01-EAST'
	'PA' = '01-EAST'
	'NY' = '01-EAST'
	'WI' = '01-EAST'
	'CA' = '01-WEST'
	'CO' = '01-WEST'
	'TX' = '01-WEST'
	'WA' = '01-WEST'
}

$referenceCount = 0
$row = 0
Get-ChildItem -Path $inFolder -Filter *.csv | Where-Object {$_.BaseName -notmatch "$($outSuffix)\Z"} | ForEach-Object {
	$outFile = Join-Path -Path $_.DirectoryName -ChildPath "$($_.BaseName)$($outSuffix)$($_.Extension)"
	$saveFile = $false
	Write-Host "Processing '$($_.Name)' ..." -NoNewline
	$content = Get-Content -Path $_.FullName | ForEach-Object {
		$row++
		$fields = $_.Split(',')
		If ($referenceCount -eq 0) {$referenceCount = $fields.Count}
		If ($fields.Count -ne $referenceCount) {
			Throw "Field count in line $($row) is incorrect (is: $($fields.Count), expected: $($referenceCount))!"
		}
		$old3 = $fields[3]
		$old99 = $fields[99]
		If ($fields[3] -eq '96587') {
			If ($replace_96587.ContainsKey($fields[27])) {
				$fields[3] = $replace_96587[$fields[27]]
				$saveFile = $true
			}
		}
		If ($fields[99] -match '\AAC(?<Number>0012[3-6])\Z') {
			$fields[99] = "$($Matches['Number'])C"
			$saveFile = $true
		}
		"line $($row): $($old3) [$($fields[27])], $($old99) --> $($fields[3]), $($fields[99])" | Write-Host
		$fields -join ','
	}
	If ($saveFile) {
		"Changes written to $($outFile)." | Write-Host
		Set-Content -Path $outFile
	} Else {
		"No changes f." | Write-Host
	}
	Write-Host " --> '$($outFile)'"
}

Open in new window

Edit: Fixed duplicate copy and paste
0
100questionsAuthor Commented:
Thanks very much, however now it produces this type of error:

Processing '.....csv' ...line 1: …..
Changes written to ……..

cmdlet Set-Content at command pipeline position 1
Supply values for the following parameters:
Value[0]:

Can we not change it so that if it does not see 96587 then it does not need to check anything else in the file?
Really, that's the only condition at this point... thanks so much.
0
oBdACommented:
Sorry, that somehow got mangled during pasting.
New attempt:
$inFolder = 'C:\Temp'
$outSuffix = '_rev'
$replace_96587 = @{
	'CT' = '01-EAST'
	'FL' = '01-EAST'
	'GA' = '01-EAST'
	'IA' = '01-EAST'
	'IN' = '01-EAST'
	'NH' = '01-EAST'
	'PA' = '01-EAST'
	'NY' = '01-EAST'
	'WI' = '01-EAST'
	'CA' = '01-WEST'
	'CO' = '01-WEST'
	'TX' = '01-WEST'
	'WA' = '01-WEST'
}

$referenceCount = 0
$row = 0
Get-ChildItem -Path $inFolder -Filter *.csv | Where-Object {$_.BaseName -notmatch "$($outSuffix)\Z"} | ForEach-Object {
	$outFile = Join-Path -Path $_.DirectoryName -ChildPath "$($_.BaseName)$($outSuffix)$($_.Extension)"
	$saveFile = $false
	Write-Host "Processing '$($_.Name)' ..."
	$content = Get-Content -Path $_.FullName | ForEach-Object {
		$fieldChanged = $false
		$row++
		$fields = $_.Split(',')
		If ($referenceCount -eq 0) {$referenceCount = $fields.Count}
		If ($fields.Count -ne $referenceCount) {
			Throw "Field count in line $($row) is incorrect (is: $($fields.Count), expected: $($referenceCount))!"
		}
		$old3 = $fields[3]
		$old99 = $fields[99]
		If ($fields[3] -eq '96587') {
			If ($replace_96587.ContainsKey($fields[27])) {
				$fields[3] = $replace_96587[$fields[27]]
				$fieldChanged = $true
			}
		}
		If ($fields[99] -match '\AAC(?<Number>0012[3-6])\Z') {
			$fields[99] = "$($Matches['Number'])C"
			$fieldChanged = $true
		}
		If ($fieldChanged) {
			Write-Host "    line $($row): $($old3) [$($fields[27])], $($old99) --> $($fields[3]), $($fields[99])"
			$saveFile = $true
		}
		$fields -join ','
	}
	If ($saveFile) {
		$content | Set-Content -Path $outFile
		Write-Host "Changes written to $($outFile)."
	} Else {
		Write-Host "No changes found."
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
Perfect, this worked very well.  Many thanks,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.