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

100questions
100questions used Ask the Experts™
on
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]
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

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.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

Commented:
Perfect, this worked very well.  Many thanks,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial