Powershell Split a file name

3.8k views Asked by At

I have to copy the files of diferent directories in their directory changing their name. I have some problems with the command split.

The variable $nom contains all the files in a directory: example: a.out b.out c.out Then I want to cut the first word. "a" -> a.out

But when I try to do this it stores also a " ".

Because when I execute write-host $nomArxiu it prints: a _directory1.out and my final resultat has to be a_directory1.out

Can anyone tell me what I am doing wrong. Thank you

1

There are 1 answers

2
Etan Reisner On BEST ANSWER

The result of -split is an array.

You only want one field not both (which you get when you use $test0.

Try $test0[0].

Or use a more appropriate function for this:

$shortnom = $_ -replace ".out$",""

or (since $_ is an IO.FileInfo object just use its BaseName property):

write-host $_.basename