![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #2 (permalink) |
| 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 -- |
| | #3 (permalink) |
| 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 > > -- > > |
| | #5 (permalink) |
| 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* |
| | #7 (permalink) |
| Guest
Posts: n/a
| Re: Difference between string and array "IJuan" <IJuan@discussions.microsoft.com> wrote in message news AF1DAF6-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 |
| |
| |
![]() |
| 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 |