#1 25 Jan 2019 04:33

be
Member
Registered: 10 Sep 2018
Posts: 14

dir command : how to get plain (unembellished) output

In batch dir /b, wildcard ? unavoidably snags some unacceptable results;
therefore in batch I need to use instead an equivalent powershell command.
This line
   >powershell "dir -directory -include ???-?? |select fullname"
gives correct results.
However, they are preceded by two preamble lines

   FullName
   --------

bad for the further processing.
Question is how to get clean output
(not  how to clean the output).

Last edited by be (25 Jan 2019 04:44)

Offline

#2 25 Jan 2019 11:46

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,005
Website

Re: dir command : how to get plain (unembellished) output

You can remove those lines by piping to format-table -HideTableHeaders

so

PS C:\> dir -directory -include ???-?? | select fullname | format-table -HideTableHeaders

However it's important to realise that while the above is fine as a way to format things for display on the screen, in PowerShell everything is an object and that is a very very useful thing, once you pipe the output through format-table you have a bunch of strings with spaces and tabs and all kinds of crud that becomes difficult to work with, it becomes like going back to CMD.

A better approach - you can put the list of files into a variable with:

$files = dir -directory -include ???-?? | select fullname

And then display the list with:

$files

Even though this looks like one simple column of strings, it is not, we are still working with an object that can have multiple properties and methods, it just happens that by default it only displays the FullName property

To display the list without any header line, explicitly request just that one property:

$files.FullName

I think everyone starting with PowerShell thinks just give me the string I know what to do with that, but once you get used to working with objects you realise thats where all the "Power" in PowerShell is.

Offline

#3 25 Jan 2019 12:17

XPlantefeve
New Member
Registered: 12 Oct 2016
Posts: 2

Re: dir command : how to get plain (unembellished) output

To answer precisely your question : select -ExpandProperty fullname

But Simon is right: if you're trying to manipulate strings, you don't have the correct mindset. Forget about batch and jump on the PowerShell bandwagon.

Offline

#4 25 Jan 2019 15:27

be
Member
Registered: 10 Sep 2018
Posts: 14

Re: dir command : how to get plain (unembellished) output

Reply, everything in it, is excellent.

Offline

#5 25 Jan 2019 15:51

be
Member
Registered: 10 Sep 2018
Posts: 14

Re: dir command : how to get plain (unembellished) output

XPlantefeve wrote:

select -ExpandProperty fullname

Good.


XPlantefeve wrote:

if you're trying to manipulate strings

I seek to avoid having to manipulate


XPlantefeve wrote:

jump on the PowerShell bandwagon

On target nonetheless.  Taken to heart.

Offline

#6 25 Jan 2019 16:54

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,005
Website

Re: dir command : how to get plain (unembellished) output

XPlantefeve wrote:

To answer precisely your question : select -ExpandProperty fullname

Good point, I didn't realise that also removed the headers - learn something new every day!

Offline

Board footer

Powered by FluxBB