0

how's it going?

I'm new on Powershell and I'm trying to simplify my code in order that I need to perform the same action in two files, the only thing that changes is the File Name and ReadCount size (15000 for the first file and 50000 for the second one).

When I run it the error shows:

Get-Content : An object at the specified path C:\Folder\08_configuration_items 11_CI-Contract-new[0].csv does not exist, or has been filtered by the -Include or -Exclude parameter. At line:2 char:7 + $i=0; Get-Content "C:\Folder\$fileArray[$len].csv" -ReadCount $sizeA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : An object at the specified path C:\Folder\08_configuration_items 11_CI-Contract-new[0]_1.csv does not exist, or has been filtered by the -Include or -Exclude parameter. At line:3 char:20 + ... bookContent = Get-Content "C:\Folder\$fileArray[$len]_1.csv" | Selec ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

That's the code, not sure if I'm accessing the Array using the right way on Powershell.

$sizeArray = @(15000,50000)
$fileArray = @("08_configuration_items", "11_CI-Contract-new")
for($len=0; $len -le 1; $len++) {
    $i=0; Get-Content "C:\Folder\$fileArray[$len].csv" -ReadCount $sizeArray[$len] | %{$i++; $_ | Out-File "C:\Folder\$fileArray[$len]_$i.csv" -Encoding "UTF8"}
    $WorkbookContent = Get-Content "C:\Folder\$fileArray[$len]_1.csv" | Select -Index 0
    for($j=2; $j -le $i; $j++) {
        $CurrentFileContent = Get-Content "C:\Folder\$fileArray[$len]_$j.csv"
        @($WorkbookContent, $CurrentFileContent) | Set-Content "C:\Folder\$fileArray[$len]_$j.csv"
    }
}

Any ideias?

Thanks a lot

1 Answer 1

2

The problem here is with string interpolation. A variable name within a string will expand up until it reaches a special character in that name. Then it will append the remainder of the string and any interpolated strings afterwards. This commonly happens with the . character when accessing a property of an object within a string. A simple solution is to use the subexpression operator ($()).

Get-Content "C:\Folder\$($fileArray[$len]).csv"

An alternative is to build the path string another way and then pass it into the command. The method below uses the format operator (-f).

$Path = "C:\Folder\{0}.csv" -f $fileArray[$len]
Get-Content $Path

Your code with the subexpression operator added will look like the following:

$sizeArray = @(15000,50000)
$fileArray = @("08_configuration_items", "11_CI-Contract-new")
for($len=0; $len -le 1; $len++) {
    $i=0; Get-Content "C:\Folder\$($fileArray[$len]).csv" -ReadCount $sizeArray[$len] | %{$i++; $_ | Out-File "C:\Folder\$($fileArray[$len])_$i.csv" -Encoding "UTF8"}
    $WorkbookContent = Get-Content "C:\Folder\$($fileArray[$len])_1.csv" | Select -Index 0
    for($j=2; $j -le $i; $j++) {
        $CurrentFileContent = Get-Content "C:\Folder\$($fileArray[$len])_$j.csv"
        @($WorkbookContent, $CurrentFileContent) | Set-Content "C:\Folder\$($fileArray[$len])_$j.csv"
    }
}

You can see this behavior on a simpler scale using your $fileArray variable.

$filearray
08_configuration_items
11_CI-Contract-new

# Notice how the [0] gets appended to the string-cast $fileArray
"$filearray[0]"
08_configuration_items 11_CI-Contract-new[0]

$filearray[0]
08_configuration_items

"$($filearray[0])"
08_configuration_items

Since $fileArray is an array of strings, you have another unintended effect. With "$fileArray[0]", $fileArray will be interpolated and converted to a string output rather than an array. PowerShell by default will join array elements by a single space when casting as a string. So the resulting output format is arrayItem1 arrayItem2 arrayItem3[0]. [0] is not included as part of the variable evaluation.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked well after adding the subexpression operator. Thanks a lot for all details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.