1
votes

I have a text file that has something like this:

 Year= YYYY
 Month= MM

I then read the file in, select the line I want, split the line so I only get what is to the right of the "=" sign, trim blank spaces, and put it all into a variable with this code:

 $Year=Get-Content -Path .\test.txt | Where-Object {$_ -match 'Year='}
 $Year=($Year -Split 'Year=').trim()

The above code is repeated for each setting I have (i.e. Year, Month, Day, Time).

When I put $Year into the command prompt I get "YYYY" (No leading space); but when I use Write-Host I get " YYYY" (has a leading space).

It is important that I be able to put $Year $Month next to eathother without a space for filepath and file naming reasons. (i.e. .\YYYY\MM or YYYYMM.txt).

I have tried:

 Write-Host "Example: $Year\$Month"
 Write-Host "Example: " $Year "\" $Month
 Write-Host ("Example: $($Year)\$($Month)")
 Write-Host "Example: $Year\" + $Month"
 Write-Host "Example: $Year\" -f $Month"

 ....and several ways of implementing .trim() with no success.

If the problem is not in the Write-Host line, then the only other thing I can think of is that it must be with how the data is written into the variables. Please keep in mind, this is only an example, my actual problem is larger. This is just a simple example of what I need to accomplish.

QUICK UPDATE: I found a long workaround like this:

Write-Host "$Year\" -nonewline
Write-Host "$Month\".trim() -nonewline
Write-Host "$Day\".trim() -nonewline

BUT there has to be a better way to do this. I am using about 8 variables in my actual code. So to give 1 example of a filename and path would be 8 lines long...which would be dumb.

1
Did you try to convert the date into a DateTime object and then just select the year?Alex_P
I am using the text document as a configuration file to 1) show the format of folders and file names and 2) to set date/time formatting (so I'm taking the inverse approach).Ken

1 Answers

2
votes

The problem is that $Year -Split 'Year=' returns 2 elements, not just the string after Year=.

An immediate fix would be:

$Year = ($Year -Split 'Year=')[1].Trim()

More generally, you could write:

$Year = ($Year -split '=', 2)[1].Trim()

As for what you tried:

If the -split operator finds the separator string at the very start of the string too, it assumes that what precedes it is the first substring to return, which is the empty string in this case.

In your case, 'Year= YYYY' -split 'Year=' effectively yields array '', ' YYYY', and calling .Trim() on it trims the individual elements, so you end up with '', 'YYYY'.

If you print that to the console via PowerShell's implicit output, you can get an empty line, followed by YYYY, which is also what you would get if you used Write-Output $Year.

By contrast, if you use Write-Host, PowerShell stringifies the array by concatenating its elements with spaces (by default, can be changed with $OFS), so you got  YYYY.

To put it differently:

PS> Write-Host ('', 'YYYY')
 YYYY  # concatenation of: empty string, space, 'YYYY'

is by default implicitly the same as:

PS> Write-Host (('', 'YYYY') -join ' ')
 YYYY