Windows Vista Forums

Pecursive Delete Implementation in PowerShell

  1. #1


    S. A. Gnezdov Guest

    Pecursive Delete Implementation in PowerShell

    I want to replace VBScript with PowerShell implementation. Here is VBScript
    content:

    ' Deletes all .svn folders recursively
    '
    ' To delete all .svn directories in current directory recursively execute
    command:
    ' cscript del-svn-recursively.vbs

    Set FSO = CreateObject("Scripting.FileSystemObject")
    ShowSubfolders FSO.GetFolder(".")

    Sub ShowSubFolders(Folder)
    For Each eachFolder in Folder.SubFolders
    ' FSO.DeleteFolder(eachFolder)
    if eachFolder.Name = ".svn" then
    WScript.Echo eachFolder.Path
    FSO.DeleteFolder eachFolder.Path, True
    else
    ShowSubFolders eachFolder
    end if
    Next
    End Sub


      My System SpecsSystem Spec

  2.   


  3. #2


    RichS Guest

    RE: Pecursive Delete Implementation in PowerShell

    You could try something like this

    $fso = New-Object -com "Scripting.FileSystemObject"
    $folder = $fso.GetFolder("C:\Test\")

    foreach ($subfolder in $folder.SubFolders)
    {
    If ($subfolder.Name -like "*.svn")
    {

    remove-item $subfolder.Path -Verbose


    }


    }
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "S. A. Gnezdov" wrote:

    > I want to replace VBScript with PowerShell implementation. Here is VBScript
    > content:
    >
    > ' Deletes all .svn folders recursively
    > '
    > ' To delete all .svn directories in current directory recursively execute
    > command:
    > ' cscript del-svn-recursively.vbs
    >
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > ShowSubfolders FSO.GetFolder(".")
    >
    > Sub ShowSubFolders(Folder)
    > For Each eachFolder in Folder.SubFolders
    > ' FSO.DeleteFolder(eachFolder)
    > if eachFolder.Name = ".svn" then
    > WScript.Echo eachFolder.Path
    > FSO.DeleteFolder eachFolder.Path, True
    > else
    > ShowSubFolders eachFolder
    > end if
    > Next
    > End Sub
    >


      My System SpecsSystem Spec

  4. #3


    Brandon Shell Guest

    Re: Pecursive Delete Implementation in PowerShell

    May I recommend the -whatif the first go

    "RichS" <RichS@discussions.microsoft.com> wrote in message
    news:C976E20F-9066-445F-AECB-2504B414C479@microsoft.com...
    > You could try something like this
    >
    > $fso = New-Object -com "Scripting.FileSystemObject"
    > $folder = $fso.GetFolder("C:\Test\")
    >
    > foreach ($subfolder in $folder.SubFolders)
    > {
    > If ($subfolder.Name -like "*.svn")
    > {
    >
    > remove-item $subfolder.Path -Verbose
    >
    >
    > }
    >
    >
    > }
    > --
    > Richard Siddaway
    > Please note that all scripts are supplied "as is" and with no warranty
    > Blog: http://richardsiddaway.spaces.live.com/
    > PowerShell User Group: http://www.get-psuguk.org.uk
    >
    >
    > "S. A. Gnezdov" wrote:
    >
    >> I want to replace VBScript with PowerShell implementation. Here is
    >> VBScript
    >> content:
    >>
    >> ' Deletes all .svn folders recursively
    >> '
    >> ' To delete all .svn directories in current directory recursively execute
    >> command:
    >> ' cscript del-svn-recursively.vbs
    >>
    >> Set FSO = CreateObject("Scripting.FileSystemObject")
    >> ShowSubfolders FSO.GetFolder(".")
    >>
    >> Sub ShowSubFolders(Folder)
    >> For Each eachFolder in Folder.SubFolders
    >> ' FSO.DeleteFolder(eachFolder)
    >> if eachFolder.Name = ".svn" then
    >> WScript.Echo eachFolder.Path
    >> FSO.DeleteFolder eachFolder.Path, True
    >> else
    >> ShowSubFolders eachFolder
    >> end if
    >> Next
    >> End Sub
    >>



      My System SpecsSystem Spec

  5. #4


    Shay Levi Guest

    Re: Pecursive Delete Implementation in PowerShell


    First try with -whatIf to find what folders will be deleted
    dir <path> -recurse | where {$_.PSIsContainer -and $_.Extension -eq ".svn"}
    | remove-item -force -whatIf

    Then remove -whatIf and run again


    Shay
    http://scriptolog.blogspot.com



    > May I recommend the -whatif the first go
    >
    > "RichS" <RichS@discussions.microsoft.com> wrote in message
    > news:C976E20F-9066-445F-AECB-2504B414C479@microsoft.com...
    >
    >> You could try something like this
    >>
    >> $fso = New-Object -com "Scripting.FileSystemObject" $folder =
    >> $fso.GetFolder("C:\Test\")
    >>
    >> foreach ($subfolder in $folder.SubFolders)
    >> {
    >> If ($subfolder.Name -like "*.svn")
    >> {
    >> remove-item $subfolder.Path -Verbose
    >>
    >> }
    >>
    >> }
    >> --
    >> Richard Siddaway
    >> Please note that all scripts are supplied "as is" and with no
    >> warranty
    >> Blog: http://richardsiddaway.spaces.live.com/
    >> PowerShell User Group: http://www.get-psuguk.org.uk
    >> "S. A. Gnezdov" wrote:
    >>
    >>> I want to replace VBScript with PowerShell implementation. Here is
    >>> VBScript
    >>> content:
    >>> ' Deletes all .svn folders recursively
    >>> '
    >>> ' To delete all .svn directories in current directory recursively
    >>> execute
    >>> command:
    >>> ' cscript del-svn-recursively.vbs
    >>> Set FSO = CreateObject("Scripting.FileSystemObject") ShowSubfolders
    >>> FSO.GetFolder(".")
    >>>
    >>> Sub ShowSubFolders(Folder)
    >>> For Each eachFolder in Folder.SubFolders
    >>> ' FSO.DeleteFolder(eachFolder)
    >>> if eachFolder.Name = ".svn" then
    >>> WScript.Echo eachFolder.Path
    >>> FSO.DeleteFolder eachFolder.Path, True
    >>> else
    >>> ShowSubFolders eachFolder
    >>> end if
    >>> Next
    >>> End Sub




      My System SpecsSystem Spec

  6. #5


    Kiron Guest

    Re: Pecursive Delete Implementation in PowerShell

    If speed matters, filter '.svn' through Get-ChildItem's -filter parameter,
    also use this Cmdlet's -force parameter if you want to retrieve hidden
    '.svn' folders:

    gci $folder -fil '*.svn' -r -fo | ? {$_.psIsContainer} | ri -fo

    --
    Kiron


      My System SpecsSystem Spec

  7. #6



    Newbie
    Join Date : Jul 2007
    Chicago
    Posts : 3
    XP 2003 SuSE
    Local Time: 03:09 PM


     

    Re: Pecursive Delete Implementation in PowerShell

    Kiron, your use of alias' is sweet. Rock on.
    I love how PS uses 3/4 of a line to do what used to take 15-20.

      My System SpecsSystem Spec

  8. #7


    RichS Guest

    Re: Pecursive Delete Implementation in PowerShell

    Aliases can be great for interactive work but they can intimidate people
    trying to learn PowerShell as they make it more difficult to understand
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "lrbell" wrote:

    >
    > Kiron, your use of alias' is sweet. Rock on.
    > I love how PS uses 3/4 of a line to do what used to take 15-20.
    >
    >
    > --
    > lrbell
    >


      My System SpecsSystem Spec

  9. #8


    S. A. Gnezdov Guest

    Re: Pecursive Delete Implementation in PowerShell

    I don't think aliases should be used in posts unless those are standard
    aliases.

    "RichS" <RichS@discussions.microsoft.com> wrote in message
    news:E078C64B-C649-4921-9270-2AE32C61FC81@microsoft.com...
    > Aliases can be great for interactive work but they can intimidate people
    > trying to learn PowerShell as they make it more difficult to understand
    > --
    > Richard Siddaway
    > Please note that all scripts are supplied "as is" and with no warranty
    > Blog: http://richardsiddaway.spaces.live.com/
    > PowerShell User Group: http://www.get-psuguk.org.uk
    >
    >
    > "lrbell" wrote:
    >
    >>
    >> Kiron, your use of alias' is sweet. Rock on.
    >> I love how PS uses 3/4 of a line to do what used to take 15-20.
    >>
    >>
    >> --
    >> lrbell
    >>



      My System SpecsSystem Spec

  10. #9


    Kiron Guest

    Re: Pecursive Delete Implementation in PowerShell

    You can find out more on aliases by typing:
    help about_alias

    PowerShell is indeed a great shell/language. One of its features is that all
    Cmdlets' parameters can be named by their first characters --instead of the
    full name-- as long as they disambiguate, or clearly identify, the parameter
    with the least amount of characters.

    There are also aliases for half of the ubiquitous parameters, better known
    as 'common' parameters.

    Ubiquitous parameter, Alias
    ---------------------------
    Confirm
    Debug
    ErrorAction, ea
    ErrorVariable, ev
    OutBuffer, ob
    OutVariable, ov
    Verbose
    WhatIf

    This function will get a sorted list of the regular and common parameters of
    a standard Cmdlet. With this list you can see which parameters can be named
    with just their first character, or if you need to type its second and third
    to clearly identify them. There is at least one other alias I'm aware of,
    for New-Item's -itemType regular parameter you can use -type; there may be
    more.

    function Get-ParameterList
    ([string]$cmdlet = $(throw "Specify a PowerShell Cmdlet"))
    {
    if (@(powershell -noProfile {get-command -type Cmdlet | %
    {$_.Name}}) -contains $cmdlet)
    {
    ((get-command $cmdlet).definition).split() -match '\[-[a-z]+' -replace
    '\[*-|\]' | sort -unique
    }
    else {"$cmdlet is not a standard Cmdlet"}
    }

    set-alias gpl Get-ParameterList

    # list all parameters, regular and common
    # note the use of the just created alias
    gpl Get-ChildItem
    Debug
    ErrorAction
    ErrorVariable
    Exclude
    Filter
    Force
    Include
    LiteralPath
    Name
    OutBuffer
    OutVariable
    Path
    Recurse
    Verbose

    According to the this list, and the implementation of common parameters'
    aliases, for Get-ChildItem you can use:
    -Debug or -d
    -ErrorAction or -ea
    -ErrorVariable or -ev
    -Exclude or -ex
    -Filter or -fi
    -Force or -fo
    -Include or -i
    -LiteralPath or -l
    -Name or -n
    -OutBuffer or -ob
    -OutVariable or -ov
    -Path or -p
    -Recurse or -r
    -Verbose or -v

    ---
    Kiron


      My System SpecsSystem Spec

  11. #10


    Kiron Guest

    Re: Pecursive Delete Implementation in PowerShell

    I agree that using aliases can intimidate some people learning PowerShell,
    but others will find them very useful. My suggestion was the fourth in the
    series, did explicitly referred to the Get-ChilItem Cmdlet and its
    parameters and was a modification of previous replies. I used aliases and
    parameter disambiguation in the example because the Cmdlets and parameters
    were mentioned before in the series and in my post --except for
    Remove-Item--
    I suggest that aliases can be used if our reply doesn't add new Cmdlets to
    previous replies or refers to the Cmdlet by name, same would apply to
    parameter disambiguation.
    What do you think?

    --
    Kiron


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast

Pecursive Delete Implementation in PowerShell
Similar Threads
Thread Forum
Looking to delete last 3 lines with powershell PowerShell
Delete Files using powershell PowerShell
How do I delete an OU in Active Directory from Powershell. PowerShell
How to delete a DFS node using Powershell PowerShell
delete temp dlls via powershell PowerShell