header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

deleting files
Last Post 09 Jan 2008 03:10 AM by halr9000. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
NiroUser is Offline
New Member
New Member
Posts:20
Avatar

--
09 Jan 2008 02:03 AM  

I'm trying to get a list of certain files from a directory and then delete them.  I'm sure I'm doing something very basic wrong...but I can't seem to figure out what.  Here is what I did:

 

 $file = Get-ChildItem -recurse | where-object {$_ -match "HTTP*.PDF"}

I ran $file | gm to make sure it has the delete method, which it does.  And I checked $file to make sure it has the files I want to delete.  Then I ran this:

 

$file.delete()

Method invocation failed because [System.Object[]] doesn't contain a method 'delete'

how come it shows a delete method when I run $file | gm then?

Just to see I tried $file | foreach-object {$_.delete()}, which didn't return an error but didn't actually delete anything.

NiroUser is Offline
New Member
New Member
Posts:20
Avatar

--
09 Jan 2008 02:54 AM  
Ok never mind...apparently it did work. I guess I was expecting the objects to disappear from $file also, but I guess they don't...
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
09 Jan 2008 03:09 AM  
Posted By Niro on 01/08/2008 6:03 PM

 $file = Get-ChildItem -recurse | where-object {$_ -match "HTTP*.PDF"}

Method invocation failed because [System.Object[]] doesn't contain a method 'delete'

 

Easier way to do it: gci http*.pdf -rec | remove-item

how come it shows a delete method when I run $file | gm then?

Just to see I tried $file | foreach-object {$_.delete()}, which didn't return an error but didn't actually delete anything.

Get-ChildItem (and get-anything for that matter) will usually return a collection of objects, in this case [System.IO.FileInfo].  The collection itself is a...wait for it...[System.Object[]].  What happens is when you send stuff through the pipeline, the collection is broken down.  Get-Member is smart enough to guess what you wanted to know, namely, what methods does the fileinfo object have, instead of what methods does the System.Object have.

As you later found out, it really did delete the files.  Then it tried to delete the object and as it says, that object doesn't contain the delete method.

 

Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
09 Jan 2008 03:10 AM  
Not sure if this bit was clear...the quoting mechanism ate it:

Easier way to do it: gci http*.pdf -rec | remove-item
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer