Powershell - remove space between two variables

I have two arrays that contain a selection of lines with information taken from a text file. Then I use the For Loop loop to loop through both arrays and print the lines together to create the destination folder and file name.

Get-Content .\PostBackupCheck-TextFile.txt | ForEach-Object { $a = $_ -split ' ' ; $locationArray += "$($a[0]):\$($a[1])\" ; $imageArray += "$($a[2])_$($a[3])_VOL_b00$($a[4])_i000.spi" } 

The above text contains a text file, splits it into separate parts, saves some in locationArray and other information in imageArray , for example:

locationArray[0] will be L:\Place\

imageArray[0] will be SERVERNAME_C_VOL_b001_i005.spi

Then I run the for loop:

 for ($i=0; $i -le $imageArray.Length - 1; $i++) {Write-Host $locationArray[$i]$imageArray[$i]} 

But it puts a space between L:\Place\ and SERVERNAME_C_VOL_b001_i005.spi

So it becomes: L:\Place\ SERVERNAME_C_VOL_b001_i005.spi

Instead, it should be: L:\Place\SERVERNAME_C_VOL_b001_i005.spi

How can i fix this?

+4
source share
1 answer

Option number 1 - for better readability:

 {Write-Host ("{0}{1}" -f $locationArray[$i], $imageArray[$i]) } 

Option number 2 is a bit confusing, less readable:

 {Write-Host "$($locationArray[$i])$($imageArray[$i])" } 

Option number 3 is more readable than # 2, but with more lines:

 { $location = $locationArray[$i]; $image = $imageArray[$i]; Write-Host "$location$image"; } 
+3
source

Source: https://habr.com/ru/post/1446694/


All Articles