
Common Issues? |
Post Reply
|
| Author | ||
howy
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 5 |
Quote Reply
Topic: Common Issues?Posted: 08 Feb 2010 at 10:40 |
|
|
Sorry to trouble but there's got to be a better mousetrap.
I LOVE Powershell by the way and I read a good many of Jeff's words on the topic but I'm no hot-shot code jock... so here's the issues. (sorry if not formatted how you all may prefer) Firstly are a few simple returns from get-childitem As a general rule I'll pull a file list like so: $FileList = Get-ChildItem ($SourcePath) -recurse -ErrorAction SilentlyContinue | Select Directory, BaseName, Length which gives a nice list Directory Basename Length so I can do $FileList.Directory.ToString or more to the point $FileList.Count.... this supposedly gives a count of contents. -- Not so!!! if there is only ONE file returned the .count is NULL not One.... so then it's piles of trapping... I used to: if ($FileList.Count) do something or perhaps index in a var: $TestFile = $FileList[$Count].Directory.ToString() But with one file it all blows up - cant index a null array -- Sure this is old news now is there some work around? --> Got several others like this Use the Com object folderbrowser? returns a slash \ on all roots but not so with subdirs so ya gotta sanitize with a foreach ($file in $FileList) { $Slash*t = $FileList[$i].Directory.ToString() if ($Slash*t[($Slash*t.length -1)] -ne "\") { $FileList[$i].Directory = $FileList[$i].Directory.ToString() + "\" } Just maddening.... however the index .count is how I built up a big script and I need a better solution than wrapping every indexing job with an if $List is null then do $Filelist.Directory else do $Filelist[INDEX].Directory Dont get me started with reserved chars in GetChilditem as in [ and ] -- escape them with ````[ just crazy! The question I guess is any ideas how a FileList returned from get-childitem will have a .count property when ONE file is returned (by the way 2 files offer a count of "2") so re-inserting the original single file back into the list gives a count of 2! Gotta be an easy one for you all! Thanks for the rant Guys and keep up the fine work over here |
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
Posted: 08 Feb 2010 at 12:48 |
|
|
Handing the file list to the pipeline always works. This is how it is intended to be used. Using it manually or in a linear fashion requires that you discover if it is a colection or just a simple objext. This may require discovery of its' type.
It is far easier to write teh object / colletion to a pipeline and let PowerSehll do the heavy lifting for you.
By th e way - thi is all by design. PowerSehll gets a bit of getting used to as it is not a linearly conceived system. Old ways of doing thisng will not always work as expected.
(dir | measure).Count
OR
(dir c:\config.sys | measure).Count
Both work to return the count whether it is one object or a thousand.
In PoSh when a single object is returns it is not wrapped in a collection as is the case with most WSH object such as WMI objects. This is done as a convenience but requires some getting used to. Down the road teh sense of this will become more obvious.
|
||
![]() |
||
howy
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 5 |
Quote Reply
Posted: 08 Feb 2010 at 14:57 |
|
|
Thanks for taking the time to respond
You're correct I'm not piping thru' but looping on .COUNT to process the file list.... so one file in the collection returning zero or null is an issue. so (get-childitem PATH | measure).count indeed returns 1 but that's not an index value into my collection but may provide a workaround. Perhaps a lesson from Jeff's measure object... ($list | measure).count -- works as well Sure wish the collection would simply return the number of elements like any sane language -- ONE member exists one is the 'measure' but simply calling the .Count from the collection with one member returns NULL? Sorry for not better understanding the pipeline but to this old dawg this is a MAJOR glitch! A null array/collection has no members at all - not one hiding quietly in the void! Again thanks for at least the response you got me moving but it's gonna be a kludge for a rare occurrence of a one file in the directory. running CTP3 by the way Thanks a TON for the help! HowY |
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
Posted: 08 Feb 2010 at 16:23 |
|
|
Do not run CTP3. It may have bugs in it but I don't believe this is one.
Your code:
Has many basic coding erros in it. It is hard to see what you really want. What is - $Slash*t?
What is wrong withthis?
It takes one or many files with no issue. Why would you want to subscript? YOur subscript is invalid anyway.
Perhaps if we knew what you were trying to do we could show you how it should be done in PowerShell.
|
||
![]() |
||
howy
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 5 |
Quote Reply
Posted: 08 Feb 2010 at 16:28 |
|
|
GOT IT!
Bonehead...
I simply was not "forcing" a returned array!
Some clarity
Example has only ONE file in the directory
Let's build a $list of files by dir name length...
>$List = get-childitem | select directory, name, length
>$List.count >- is null -
>$List = @(get-childitem | select directory, name, length) >$List.count >1
Now if get-childitem would just tag a slash at
the end of directory output consistantly!
[My earlier grouse about it returning a "\" on root and not others]
Hope you all didnt mind me dropin' by to kick around
some powershell code!
Cheers!
|
||
![]() |
||
howy
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 5 |
Quote Reply
Posted: 08 Feb 2010 at 16:41 |
|
|
Ohh
the filelist is sanitized with a trailing slash
codereads ...
"pull directoryname from filelist"
if it's got a trailing slash skip it [root folder]
elsewise tag a slash backi nto the filelist
example only missed the $i indexer....
invalid? more like lazy on my part!
the star in the variable name is an error
posting up here! its a capital i -I- ??
million ways to skin a cat just my
current answer to sanitize the return
from get-childitem -- Edited by howy - 08 Feb 2010 at 16:50 |
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
Posted: 08 Feb 2010 at 16:50 |
|
|
Yes. YOu can force a collection or array but it is not usually needed. WHen you do a slect-object you are returning a subset of properties. This changes nothing. You cannot suscript in your case as it will never work the way you are doing it even with a collection.
In case you want a trailing slash here is one solution:
Note that "dir" or Get-ChildItem never gets teh root so it is not an issue.
Edited by jvierra - 08 Feb 2010 at 17:01 |
||
![]() |
||
howy
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 5 |
Quote Reply
Posted: 08 Feb 2010 at 16:53 |
|
|
Sweet!
Thanks!
|
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
Posted: 08 Feb 2010 at 17:02 |
|
|
Should be:
dir 'c:\program files' |where{$_.Attributes -eq 'Directory'}|%{"$($_.Fullname)\"}
|
||
![]() |
||
Post Reply
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |