I am facing the error "Input string was not in a correct format." while using the variable in for loop.
$totalrows=$filepath |% {$n = $_; $c = 0; Get-Content -Path $_ -ReadCount 1000 |% { $c += $_.Count }; "$n; $c"}
echo $totalrows
Output is 8 and it is correct.
Used this variable in for loop:
For ($i = 0; $i -lt $totalrows; $i++) {
Write-host $i
}
but i get the error :
8" to type "System.Int32". Error: "Input string was not in a correct format."
So, I looked into SO for same questions so i found to typecast into integer:
$totalrows=$filepath |% {$n = $_; $c = 0; Get-Content -Path $_ -ReadCount 1000 |% { $c += $_.Count }; "$n; $c"}
$totalrowscast=[int]$totalrows
echo $totalrowscast
For ($i = 0; $i -lt $totalrowscast; $i++) {
Write-host $i
}
But still I am facing the same error.
write-host $totalrows.gettype()to see what type you are getting."$n; $c"that will give you a string that contains two things separated with a semicolon.$c, not"$n; $c"