• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Pecursive Delete Implementation in PowerShell

S

S. A. Gnezdov

#1
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 Computer

R

RichS

#2
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 Computer

B

Brandon Shell

#3
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 Computer

S

Shay Levi

#4
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 Computer

K

Kiron

#5
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 Computer

R

RichS

#7
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 Computer

S

S. A. Gnezdov

#8
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 Computer

K

Kiron

#9
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 Computer

K

Kiron

#10
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 Computer

K

Kiron

#11
The 'speed' part of my suggestion doesn't have to do with less typing, is
just that by filtering through Get-ChildItem's -filter less objects are
passed down the pipeline to Where-Object, therefore, enhancing performance
The aliases used in my suggestion are standard, and the brief name for the
parameters is a PowerShell feature sometimes referred as "parameter
disambiguation", that later you might find very useful. See my reply to
lrbell for more on that. Besides you can always ask for clarification, lots
of smart --myself NOT included-- and helpful people here that can help out.

--
Kiron
 

My Computer

S

Shay Levi

#12
I wrote a function that calculates the minimum paramater length you can type
something like:

PS C:\Scripts>get-params select-string

-----------------
- Select-String -
-----------------

name alias position
---- ----- --------
caseSensitive -c named
exclude -e named
include -inc named
inputObject -inp named
list -l named
path -path 2
pattern -patt 1
quiet -q named
simpleMatch -s named
text -t 2


You can find it here:
http://scriptolog.blogspot.com/2007/07/what-dat-parameter.html


Shay
http://scriptolog.blogspot.com



> 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 Computer

K

Kiron

#13
Thanks Shay, the parameter 'short' name generator is great.
Get-Help is a good source of information on each parameter, be aware that
some commonly used Cmdlets' help contents are missing parameters.
This script gets Set-Content's, Add-Content's and Get-Content's parameters
found through Get-Command and Get-Help, then compares the lists to see which
parameters, if any, are missing from either list:
I commented other $cmdlets assignments in case anyone is interested in
looking at other parameter discrepancies or comparing all standard Cmdlets.

<Get-MissingParameters.ps1>
$ubiq = 'Debug|ErrorAction|ErrorVariable|OutBuffer|OutVariable|Verbose'

# parameters missing in Get-Help
$cmdlets = 'Set-Content', 'Add-Content', 'Get-Content'

# parameters missing in Get-Command's Definition
# $cmdlets = 'Set-TraceSource', 'Trace-Command'

# parameters listed in Get-Help but are not functional
# $cmdlets = 'Get-ChildItem', 'Select-String'

# compare all standard Cmdlets
# $cmdlets = (powershell -noProfile {get-command -type Cmdlet | %
{$_.Name}})

foreach ($cmdlet in $cmdlets)
{
$thruGetCommand = ((gcm $cmdlet).definition).split() -match
'\[-[a-z]+|^\-[a-z]+' -replace '\[*-|\]' -notMatch $ubiq | sort -unique
$thruGetHelp = get-help $cmdlet -parameter * 2>$null| % {$_.name} | sort
if ($thruGetHelp -ne $null)
{
if ($thruGetCommand.length -ne $thruGetHelp.length)
{
"Differences found in $cmdlet's parameters:"
compare-object $thruGetCommand $thruGetHelp | ft -hide -auto
}
}
}
<Get-MissingParameters.ps1>

PowerShell's scalar/array treatment --as in your CSV post-- seems to be the
reason why your function doesn't support Cmdlets that have one or zero
regular parameter, but all support common parameters. Get-Command's
Definition property will list all functional parameters, except
the -Debugger for Set-TraceSource and Trace-Command.

Thanks also for updating the common parameters' aliases.

Debug, db
Verbose, vb

--
Kiron
 

My Computer

S

Shay Levi

#14
Thanks, kiron. Cool stuff! Cheers all PowerShell enthusiasts.


Shay
http://scriptolog.blogspot.com



> Thanks Shay, the parameter 'short' name generator is great.
> Get-Help is a good source of information on each parameter, be aware
> that
> some commonly used Cmdlets' help contents are missing parameters.
> This script gets Set-Content's, Add-Content's and Get-Content's
> parameters
> found through Get-Command and Get-Help, then compares the lists to see
> which
> parameters, if any, are missing from either list:
> I commented other $cmdlets assignments in case anyone is interested in
> looking at other parameter discrepancies or comparing all standard
> Cmdlets.
> <Get-MissingParameters.ps1>
> $ubiq =
> 'Debug|ErrorAction|ErrorVariable|OutBuffer|OutVariable|Verbose'
> # parameters missing in Get-Help
> $cmdlets = 'Set-Content', 'Add-Content', 'Get-Content'
> # parameters missing in Get-Command's Definition
> # $cmdlets = 'Set-TraceSource', 'Trace-Command'
> # parameters listed in Get-Help but are not functional # $cmdlets =
> 'Get-ChildItem', 'Select-String'
>
> # compare all standard Cmdlets
> # $cmdlets = (powershell -noProfile {get-command -type Cmdlet | %
> {$_.Name}})
> foreach ($cmdlet in $cmdlets)
> {
> $thruGetCommand = ((gcm $cmdlet).definition).split() -match
> '\[-[a-z]+|^\-[a-z]+' -replace '\[*-|\]' -notMatch $ubiq | sort
> -unique
> $thruGetHelp = get-help $cmdlet -parameter * 2>$null| % {$_.name} |
> sort
> if ($thruGetHelp -ne $null)
> {
> if ($thruGetCommand.length -ne $thruGetHelp.length)
> {
> "Differences found in $cmdlet's parameters:"
> compare-object $thruGetCommand $thruGetHelp | ft -hide -auto
> }
> }
> }
> <Get-MissingParameters.ps1>
>
> PowerShell's scalar/array treatment --as in your CSV post-- seems to
> be the reason why your function doesn't support Cmdlets that have one
> or zero regular parameter, but all support common parameters.
> Get-Command's Definition property will list all functional parameters,
> except the -Debugger for Set-TraceSource and Trace-Command.
>
> Thanks also for updating the common parameters' aliases.
>
> Debug, db
> Verbose, vb
> --
> Kiron
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)