Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Difference between string and array

Closed Thread
 
Thread Tools Display Modes
Old 05-04-2007   #1 (permalink)
Flomo Togba Kwele
Guest
 
Posts: n/a

Difference between string and array

I used the following statements:


--

 
Old 05-04-2007   #2 (permalink)
Flomo Togba Kwele
Guest
 
Posts: n/a

Re: Difference between string and array

Sorry about the previous post - finger slipped.

I have the following statements:

$files = get-childitem -name $dir/*$date*

which returns the names of files in a directory. When there is only one file
returned, $files.length returns the length of the string. If the command
returns more than a single file name, $files acts like an array and returns the
number of elements in the array for $files.length.

So the next statement will not be what I expect when there is a single file
name. How can I make it so that $files is only an array?

$(for ($i=0; $i -lt $files.length; $i++) {'"' + $dir + $files[$i] + '"'}) |
set-variable -name list

Thanks, Flomo

--

 
Old 05-04-2007   #3 (permalink)
IJuan
Guest
 
Posts: n/a

Re: Difference between string and array

Make $files an array first and then add your objects to that. Like this:

$files = @()
$files += get-childitem -name $dir/*$date*

That should work no matter how many results you get.


-= IJuan =-


"Flomo Togba Kwele" wrote:

> Sorry about the previous post - finger slipped.
>
> I have the following statements:
>
> $files = get-childitem -name $dir/*$date*
>
> which returns the names of files in a directory. When there is only one file
> returned, $files.length returns the length of the string. If the command
> returns more than a single file name, $files acts like an array and returns the
> number of elements in the array for $files.length.
>
> So the next statement will not be what I expect when there is a single file
> name. How can I make it so that $files is only an array?
>
> $(for ($i=0; $i -lt $files.length; $i++) {'"' + $dir + $files[$i] + '"'}) |
> set-variable -name list
>
> Thanks, Flomo
>
> --
>
>

 
Old 05-04-2007   #4 (permalink)
Flomo Togba Kwele
Guest
 
Posts: n/a

Re: Difference between string and array

Thanks, IJuan. That worked perfectly.



--

 
Old 05-04-2007   #5 (permalink)
Marco Shaw
Guest
 
Posts: n/a

Re: Difference between string and array

IJuan wrote:
> Make $files an array first and then add your objects to that. Like this:
>
> $files = @()
> $files += get-childitem -name $dir/*$date*


Give that a try, but keep in mind:

Actually, I've had something like this *not* work once when only one
line was written to $files, and it caused me major headaches.

I've had to go with something more like this instead:

[array]$files += get-childitem -name $dir/*$date*
 
Old 05-04-2007   #6 (permalink)
Flomo Togba Kwele
Guest
 
Posts: n/a

Re: Difference between string and array

Marco,

I tried to determine how to declare a type from the docs, but was not
successful. Thanks for this.

> [array]$files += get-childitem -name $dir/*$date*


Flomo


--

 
Old 05-04-2007   #7 (permalink)
Keith Hill [MVP]
Guest
 
Posts: n/a

Re: Difference between string and array

"IJuan" <IJuan@discussions.microsoft.com> wrote in message
newsAF1DAF6-57D9-40E2-B7AC-C26D0FD9D6B8@microsoft.com...
> Make $files an array first and then add your objects to that. Like this:
>
> $files = @()
> $files += get-childitem -name $dir/*$date*


You could simplify that to:

$files = @(get-childitem -name $dir/*$date*)

--
Keith


 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Byte Array into String for Delimiting? coconet .NET General 3 05-24-2008 02:35 AM
Passing a string array as a parameter from one script to another Brillig PowerShell 5 09-27-2007 04:16 AM
convert array of strings to string Hal Rottenberg PowerShell 16 08-30-2007 02:30 PM
Using a WMI string array in Powershell GordT. PowerShell 1 01-11-2007 12:33 PM
String to Array Singee PowerShell 3 05-31-2006 11:58 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49