![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Mysterious empty line in DateTime formatting Can somebody explain why there is an empty line added in the first example? ######################### # Get-DateTimeFormatBug.ps1 ######################### " `n`n`n example 1" "---" [DateTime]"03-mar-2006" "---" [DateTime]"03-mar-2006" [DateTime]"03-mar-2006" "---" " `n`n`n example 2" "---" [DateTime]::Now "---" [DateTime]::Now [DateTime]::Now "---" ######################### -- greetings dreeschkind |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Mysterious empty line in DateTime formatting To be more clear, this is the output that I see when running that script: Note the added empty line in example one: example 1 --- <<<<<< this is the added empty line that should not be there Freitag, 3. März 2006 00:00:00 --- Freitag, 3. März 2006 00:00:00 Freitag, 3. März 2006 00:00:00 --- example 2 --- Samstag, 2. September 2006 22:37:15 --- Samstag, 2. September 2006 22:37:15 Samstag, 2. September 2006 22:37:15 --- -- greetings dreeschkind "dreeschkind" wrote: > Can somebody explain why there is an empty line added in the first example? > > ######################### > # Get-DateTimeFormatBug.ps1 > ######################### > " `n`n`n example 1" > "---" > [DateTime]"03-mar-2006" > "---" > [DateTime]"03-mar-2006" > [DateTime]"03-mar-2006" > "---" > > > " `n`n`n example 2" > "---" > [DateTime]::Now > "---" > [DateTime]::Now > [DateTime]::Now > "---" > ######################### > > > > -- > greetings > dreeschkind |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Mysterious empty line in DateTime formatting dreeschkind wrote: > Can somebody explain why there is an empty line added in the first example? > > ######################### > # Get-DateTimeFormatBug.ps1 > ######################### > " `n`n`n example 1" > "---" > [DateTime]"03-mar-2006" > "---" > [DateTime]"03-mar-2006" > [DateTime]"03-mar-2006" > "---" > > > " `n`n`n example 2" > "---" > [DateTime]::Now > "---" > [DateTime]::Now > [DateTime]::Now > "---" > ######################### > > > > -- > greetings > dreeschkind Interesting. In PowerShell Analyzer output window there is no extra line, but straight from the shell I am seeing the same as you. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Mysterious empty line in DateTime formatting I'm pretty sure this was talked about a long time ago, and has to do with the way that the formatter does it's business. What's really happening is that the formatter has been given a couple of different types of objects, and it does its best to format them. Let's take a closer look at this example: We actually only need the first three lines: "---" get-date "---" so, this gets sent to the formatter as an array of three objects, [string],[datetime],[string]. The formatter lets strings and numbers straight through, so we get: --- next, we get a datetime object and since it's the first non-value type through the formatter, an attempt is made to format it (which adds a CR upfront to make it "pretty"), so we get <empty line> Monday, September 04, 2006 1:55:57 PM lastly, we have another string, which just goes on through --- <empty line> <empty line> so, that extra newline at the beginning is a result of the formatter trying to make it "pretty". This behavior is even more noticable when different non-value type objects are piped. The reason that the "pretty" 2 empty lines don't follow the date is because another object is thrown at the formatter (that last string), but you can see the 2 empty lines after all the "real" output. The reason that you don't get the same newline before the other date objects is that it's the same type that comes at the formatter - we can show this: Take the following (which you could either script or put on the same command line as I'm about to do): PS> get-date;ps idle Monday, September 04, 2006 2:00:34 PM Id : 0 Handles : 0 CPU : Name : Idle The pipeline is grabbed by the host and the formatter (out-default) is added to the entire command line (*not* formatting for each element in the pipeline - the host doesn't know anything about the language parsing, so doesn't even really know what was typed). The formatting is done by the host when the user doesn't specify a format and the first object wins the battle on what the formatting should be. That's why the process object is formatted as a list - the shell doesn't want to throw the data away, but it can't format the process object like a date, so you get a list view _and_ another blank line (since the formatter stepped in). There are a couple of ways to solve this, they require turning the output into strings and you either have to manage your own formatting, or convert the shell formatting into strings and use those, here's an example: "---" get-date|out-string -stream|?{$_} "---" when run, you get: PS> c:\temp\t5 --- Monday, September 04, 2006 2:09:45 PM --- The interesting line is the pipeline, out-string -stream converts the formatting into an array of strings and the ?{$_} only allows those strings that are non-empty through. hope that helps -- James Truher[MSFT] Program Manager - Windows PowerShell Microsoft Corporation This posting is provided "AS IS" with no warranties, no confers rights. Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scr.../hubs/msh.mspx "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message news:6C12FF3A-7C79-4770-BA34-122FCE2BF611@microsoft.com... > Can somebody explain why there is an empty line added in the first > example? > > ######################### > # Get-DateTimeFormatBug.ps1 > ######################### > " `n`n`n example 1" > "---" > [DateTime]"03-mar-2006" > "---" > [DateTime]"03-mar-2006" > [DateTime]"03-mar-2006" > "---" > > > " `n`n`n example 2" > "---" > [DateTime]::Now > "---" > [DateTime]::Now > [DateTime]::Now > "---" > ######################### > > > > -- > greetings > dreeschkind |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Mysterious empty line in DateTime formatting "James Truher" wrote: > I'm pretty sure this was talked about a long time ago, and has to do with > the way that the formatter does it's business. We had a little discussion about this issue in the IRC channel #powershell on freenode yesterday. And even though Jeffrey dedicated two of his articles on that topic on the blog, it still seems that formatting is not always comprehensible. > What's really happening is > that the formatter has been given a couple of different types of objects, > and it does its best to format them. Let's take a closer look at this > example: We actually only need the first three lines: > > "---" > get-date > "---" > > so, this gets sent to the formatter as an array of three objects, > [string],[datetime],[string]. The formatter lets strings and numbers > straight through, so we get: > --- > > next, we get a datetime object and since it's the first non-value type > through the formatter, an attempt is made to format it (which adds a CR > upfront to make it "pretty"), so we get > <empty line> > Monday, September 04, 2006 1:55:57 PM > lastly, we have another string, which just goes on through > --- > <empty line> > <empty line> > > so, that extra newline at the beginning is a result of the formatter trying > to make it "pretty". This behavior is even more noticable when different > non-value type objects are piped. The reason that the "pretty" 2 empty > lines don't follow the date is because another object is thrown at the > formatter (that last string), but you can see the 2 empty lines after all > the "real" output. The reason that you don't get the same newline before > the other date objects is that it's the same type that comes at the > formatter - we can show this: If I understand you correctly, the same thing is happening in the following example. PowerShell adds table header etc. to make the first file object "pretty". What I don't understand, why doesn't PowerShell repeat the "pretty" header (or a single newline in case of the datetime object) after the string ("---"). PS> gci; "----foo---"; gci Directory: Microsoft.PowerShell.Core\FileSystem::C:\test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 04.09.2006 22:57 46 test.bak -a--- 04.09.2006 23:06 42 test.txt ----foo--- -a--- 04.09.2006 22:57 46 test.bak -a--- 04.09.2006 23:06 42 test.txt > Take the following (which you could either script or put on the same command > line as I'm about to do): > > PS> get-date;ps idle > > Monday, September 04, 2006 2:00:34 PM > > Id : 0 > Handles : 0 > CPU : > Name : Idle > > The pipeline is grabbed by the host and the formatter (out-default) is added > to the entire command line (*not* formatting for each element in the > pipeline - the host doesn't know anything about the language parsing, so > doesn't even really know what was typed). The formatting is done by the host > when the user doesn't specify a format and the first object wins the battle > on what the formatting should be. That's why the process object is > formatted as a list - the shell doesn't want to throw the data away, but it > can't format the process object like a date, so you get a list view _and_ > another blank line (since the formatter stepped in). I'm sure there is a lot of thought behind they way the formatter works, but I'm not sure wheather this is the best solution. The workaround that you recommend using may work, but IMHO plain text formatting should be made more simple and not be that complicated and obscuring. > There are a couple of ways to solve this, they require turning the output > into strings and you either have to manage your own formatting, or convert > the shell formatting into strings and use those, here's an example: > > "---" > get-date|out-string -stream|?{$_} > "---" > > when run, you get: > PS> c:\temp\t5 > --- > Monday, September 04, 2006 2:09:45 PM > --- > > The interesting line is the pipeline, out-string -stream converts the > formatting into an array of strings and the ?{$_} only allows those strings > that are non-empty through. > > hope that helps I hope user feedback helps... -- greetings dreeschkind > -- > James Truher[MSFT] > Program Manager - Windows PowerShell > Microsoft Corporation > This posting is provided "AS IS" with no warranties, no confers rights. > Visit the Windows PowerShell Team blog at: > http://blogs.msdn.com/PowerShell > Visit the Windows PowerShell ScriptCenter at: > http://www.microsoft.com/technet/scr.../hubs/msh.mspx > "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message > news:6C12FF3A-7C79-4770-BA34-122FCE2BF611@microsoft.com... > > Can somebody explain why there is an empty line added in the first > > example? > > > > ######################### > > # Get-DateTimeFormatBug.ps1 > > ######################### > > " `n`n`n example 1" > > "---" > > [DateTime]"03-mar-2006" > > "---" > > [DateTime]"03-mar-2006" > > [DateTime]"03-mar-2006" > > "---" > > > > > > " `n`n`n example 2" > > "---" > > [DateTime]::Now > > "---" > > [DateTime]::Now > > [DateTime]::Now > > "---" > > ######################### > > > > > > > > -- > > greetings > > dreeschkind > > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Mysterious empty line in DateTime formatting James Truher wrote: > ... next, we get a datetime object and since it's the first non-value type > through the formatter, an attempt is made to format it (which adds a CR > upfront to make it "pretty"), so we get ... Hmm, I tend to think that it should do less guessing and be more consistent. Then, the user can insert that blank line if he/she wants it... |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Mysterious empty line in DateTime formatting the reason that powershell doesn't add the new line in this case gci;"---foo---";gci is that the string "---foo---" doesn't affect the state of the formatter, it just gets passed through. The formatter knows that it was emitting file/dir info objects so when it gets another one of those (after the "---foo---") no new formatting is needed - it just takes up where it left off. jim -- James Truher[MSFT] Program Manager - Windows PowerShell Microsoft Corporation This posting is provided "AS IS" with no warranties, no confers rights. Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scr.../hubs/msh.mspx "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message news:B2DB5C2A-267C-4ED5-81BD-7B2D14582607@microsoft.com... > "James Truher" wrote: > >> I'm pretty sure this was talked about a long time ago, and has to do with >> the way that the formatter does it's business. > > We had a little discussion about this issue in the IRC channel #powershell > on freenode yesterday. And even though Jeffrey dedicated two of his > articles > on that topic on the blog, it still seems that formatting is not always > comprehensible. > >> What's really happening is >> that the formatter has been given a couple of different types of objects, >> and it does its best to format them. Let's take a closer look at this >> example: We actually only need the first three lines: >> >> "---" >> get-date >> "---" >> >> so, this gets sent to the formatter as an array of three objects, >> [string],[datetime],[string]. The formatter lets strings and numbers >> straight through, so we get: >> --- >> >> next, we get a datetime object and since it's the first non-value type >> through the formatter, an attempt is made to format it (which adds a CR >> upfront to make it "pretty"), so we get >> <empty line> >> Monday, September 04, 2006 1:55:57 PM >> lastly, we have another string, which just goes on through >> --- >> <empty line> >> <empty line> >> >> so, that extra newline at the beginning is a result of the formatter >> trying >> to make it "pretty". This behavior is even more noticable when different >> non-value type objects are piped. The reason that the "pretty" 2 empty >> lines don't follow the date is because another object is thrown at the >> formatter (that last string), but you can see the 2 empty lines after all >> the "real" output. The reason that you don't get the same newline >> before >> the other date objects is that it's the same type that comes at the >> formatter - we can show this: > > If I understand you correctly, the same thing is happening in the > following > example. > PowerShell adds table header etc. to make the first file object "pretty". > What I don't understand, why doesn't PowerShell repeat the "pretty" header > (or a single newline in case of the datetime object) after the string > ("---"). > > PS> gci; "----foo---"; gci > > Directory: Microsoft.PowerShell.Core\FileSystem::C:\test > > Mode LastWriteTime Length Name > ---- ------------- ------ ---- > -a--- 04.09.2006 22:57 46 test.bak > -a--- 04.09.2006 23:06 42 test.txt > ----foo--- > -a--- 04.09.2006 22:57 46 test.bak > -a--- 04.09.2006 23:06 42 test.txt > >> Take the following (which you could either script or put on the same >> command >> line as I'm about to do): >> >> PS> get-date;ps idle >> >> Monday, September 04, 2006 2:00:34 PM >> >> Id : 0 >> Handles : 0 >> CPU : >> Name : Idle >> >> The pipeline is grabbed by the host and the formatter (out-default) is >> added >> to the entire command line (*not* formatting for each element in the >> pipeline - the host doesn't know anything about the language parsing, so >> doesn't even really know what was typed). The formatting is done by the >> host >> when the user doesn't specify a format and the first object wins the >> battle >> on what the formatting should be. That's why the process object is >> formatted as a list - the shell doesn't want to throw the data away, but >> it >> can't format the process object like a date, so you get a list view _and_ >> another blank line (since the formatter stepped in). > > I'm sure there is a lot of thought behind they way the formatter works, > but > I'm not sure wheather this is the best solution. The workaround that you > recommend using may work, but IMHO plain text formatting should be made > more > simple and not be that complicated and obscuring. > >> There are a couple of ways to solve this, they require turning the output >> into strings and you either have to manage your own formatting, or >> convert >> the shell formatting into strings and use those, here's an example: >> >> "---" >> get-date|out-string -stream|?{$_} >> "---" >> >> when run, you get: >> PS> c:\temp\t5 >> --- >> Monday, September 04, 2006 2:09:45 PM >> --- >> >> The interesting line is the pipeline, out-string -stream converts the >> formatting into an array of strings and the ?{$_} only allows those >> strings >> that are non-empty through. >> >> hope that helps > > I hope user feedback helps... > > -- > greetings > dreeschkind > >> -- >> James Truher[MSFT] >> Program Manager - Windows PowerShell >> Microsoft Corporation >> This posting is provided "AS IS" with no warranties, no confers rights. >> Visit the Windows PowerShell Team blog at: >> http://blogs.msdn.com/PowerShell >> Visit the Windows PowerShell ScriptCenter at: >> http://www.microsoft.com/technet/scr.../hubs/msh.mspx >> "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message >> news:6C12FF3A-7C79-4770-BA34-122FCE2BF611@microsoft.com... >> > Can somebody explain why there is an empty line added in the first >> > example? >> > >> > ######################### >> > # Get-DateTimeFormatBug.ps1 >> > ######################### >> > " `n`n`n example 1" >> > "---" >> > [DateTime]"03-mar-2006" >> > "---" >> > [DateTime]"03-mar-2006" >> > [DateTime]"03-mar-2006" >> > "---" >> > >> > >> > " `n`n`n example 2" >> > "---" >> > [DateTime]::Now >> > "---" >> > [DateTime]::Now >> > [DateTime]::Now >> > "---" >> > ######################### >> > >> > >> > >> > -- >> > greetings >> > dreeschkind >> >> >> |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Mysterious empty line in DateTime formatting "James Truher" wrote: > the reason that powershell doesn't add the new line in this case > gci;"---foo---";gci > is that the string "---foo---" doesn't affect the state of the formatter, it > just gets passed through. Then maybe it should affect the state of the formatter? > The formatter knows that it was emitting file/dir > info objects so when it gets another one of those (after the "---foo---") no > new formatting is needed - it just takes up where it left off. How can the formater be sure that no new formatting is needed? As Adam Milazzo put it: "I tend to think that it should do less guessing and be more consistent." Can't the formatter just switch it's formatting state when new objects arive? I understand that printing the String object "---" doesn't need a pretty object header, but when the next group of File or Process objects arives it should probably get it's individual pretty formatting again regardless of what was before. Same as in the next example, the formatter can't print the Process objects as a table, just because a group of File objects have been printed before. This is odd. PS> gci; gps; gci But never mind, PowerShell seriously is a great product so far and I hope V2 will get an big overhaul so it will be even better than that. -- greetings dreeschkind > jim > > -- > James Truher[MSFT] > Program Manager - Windows PowerShell > Microsoft Corporation > This posting is provided "AS IS" with no warranties, no confers rights. > Visit the Windows PowerShell Team blog at: > http://blogs.msdn.com/PowerShell > Visit the Windows PowerShell ScriptCenter at: > http://www.microsoft.com/technet/scr.../hubs/msh.mspx > "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message > news:B2DB5C2A-267C-4ED5-81BD-7B2D14582607@microsoft.com... > > "James Truher" wrote: > > > >> I'm pretty sure this was talked about a long time ago, and has to do with > >> the way that the formatter does it's business. > > > > We had a little discussion about this issue in the IRC channel #powershell > > on freenode yesterday. And even though Jeffrey dedicated two of his > > articles > > on that topic on the blog, it still seems that formatting is not always > > comprehensible. > > > >> What's really happening is > >> that the formatter has been given a couple of different types of objects, > >> and it does its best to format them. Let's take a closer look at this > >> example: We actually only need the first three lines: > >> > >> "---" > >> get-date > >> "---" > >> > >> so, this gets sent to the formatter as an array of three objects, > >> [string],[datetime],[string]. The formatter lets strings and numbers > >> straight through, so we get: > >> --- > >> > >> next, we get a datetime object and since it's the first non-value type > >> through the formatter, an attempt is made to format it (which adds a CR > >> upfront to make it "pretty"), so we get > >> <empty line> > >> Monday, September 04, 2006 1:55:57 PM > >> lastly, we have another string, which just goes on through > >> --- > >> <empty line> > >> <empty line> > >> > >> so, that extra newline at the beginning is a result of the formatter > >> trying > >> to make it "pretty". This behavior is even more noticable when different > >> non-value type objects are piped. The reason that the "pretty" 2 empty > >> lines don't follow the date is because another object is thrown at the > >> formatter (that last string), but you can see the 2 empty lines after all > >> the "real" output. The reason that you don't get the same newline > >> before > >> the other date objects is that it's the same type that comes at the > >> formatter - we can show this: > > > > If I understand you correctly, the same thing is happening in the > > following > > example. > > PowerShell adds table header etc. to make the first file object "pretty". > > What I don't understand, why doesn't PowerShell repeat the "pretty" header > > (or a single newline in case of the datetime object) after the string > > ("---"). > > > > PS> gci; "----foo---"; gci > > > > Directory: Microsoft.PowerShell.Core\FileSystem::C:\test > > > > Mode LastWriteTime Length Name > > ---- ------------- ------ ---- > > -a--- 04.09.2006 22:57 46 test.bak > > -a--- 04.09.2006 23:06 42 test.txt > > ----foo--- > > -a--- 04.09.2006 22:57 46 test.bak > > -a--- 04.09.2006 23:06 42 test.txt > > > >> Take the following (which you could either script or put on the same > >> command > >> line as I'm about to do): > >> > >> PS> get-date;ps idle > >> > >> Monday, September 04, 2006 2:00:34 PM > >> > >> Id : 0 > >> Handles : 0 > >> CPU : > >> Name : Idle > >> > >> The pipeline is grabbed by the host and the formatter (out-default) is > >> added > >> to the entire command line (*not* formatting for each element in the > >> pipeline - the host doesn't know anything about the language parsing, so > >> doesn't even really know what was typed). The formatting is done by the > >> host > >> when the user doesn't specify a format and the first object wins the > >> battle > >> on what the formatting should be. That's why the process object is > >> formatted as a list - the shell doesn't want to throw the data away, but > >> it > >> can't format the process object like a date, so you get a list view _and_ > >> another blank line (since the formatter stepped in). > > > > I'm sure there is a lot of thought behind they way the formatter works, > > but > > I'm not sure wheather this is the best solution. The workaround that you > > recommend using may work, but IMHO plain text formatting should be made > > more > > simple and not be that complicated and obscuring. > > > >> There are a couple of ways to solve this, they require turning the output > >> into strings and you either have to manage your own formatting, or > >> convert > >> the shell formatting into strings and use those, here's an example: > >> > >> "---" > >> get-date|out-string -stream|?{$_} > >> "---" > >> > >> when run, you get: > >> PS> c:\temp\t5 > >> --- > >> Monday, September 04, 2006 2:09:45 PM > >> --- > >> > >> The interesting line is the pipeline, out-string -stream converts the > >> formatting into an array of strings and the ?{$_} only allows those > >> strings > >> that are non-empty through. > >> > >> hope that helps > > > > I hope user feedback helps... > > > > -- > > greetings > > dreeschkind > > > >> -- > >> James Truher[MSFT] > >> Program Manager - Windows PowerShell > >> Microsoft Corporation > >> This posting is provided "AS IS" with no warranties, no confers rights. > >> Visit the Windows PowerShell Team blog at: > >> http://blogs.msdn.com/PowerShell > >> Visit the Windows PowerShell ScriptCenter at: > >> http://www.microsoft.com/technet/scr.../hubs/msh.mspx > >> "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message > >> news:6C12FF3A-7C79-4770-BA34-122FCE2BF611@microsoft.com... > >> > Can somebody explain why there is an empty line added in the first > >> > example? > >> > > >> > ######################### > >> > # Get-DateTimeFormatBug.ps1 > >> > ######################### > >> > " `n`n`n example 1" > >> > "---" > >> > [DateTime]"03-mar-2006" > >> > "---" > >> > [DateTime]"03-mar-2006" > >> > [DateTime]"03-mar-2006" > >> > "---" > >> > > >> > > >> > " `n`n`n example 2" > >> > "---" > >> > [DateTime]::Now > >> > "---" > >> > [DateTime]::Now > >> > [DateTime]::Now > >> > "---" > >> > ######################### > >> > > >> > > >> > > >> > -- > >> > greetings > >> > dreeschkind > >> > >> > >> > > > |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Mysterious empty line in DateTime formatting >Can't the formatter just switch it's formatting state when new >objects >arive? >I understand that printing the String object "---" doesn't need a pretty >object header, but when the next group of File or Process objects arives it >should probably get it's individual pretty formatting again regardless of >what was before. the grouping is the important thing here. where there is a different type, not expected as part of the grouping, its going to close the group, then go onto format whatever it is getting in the pipeline from then on. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| find last non-empty line | VB Script | |||
| Rounding of a [datetime] | PowerShell | |||
| add an empty line to txt file every two rows. | PowerShell | |||
| How to use [datetime]::now properly? | PowerShell | |||
| Formatting a line | PowerShell | |||