Reputation: 2825
I have a simple script which is used to delete files from a folder. The script accepts two parameters:
This is the script I have:
Param(
[Parameter(Mandatory=$True)]
[string]$pathDeleteFrom,
[Parameter(Mandatory=$True)]
[string[]]$excludeFiles
)
Remove-Item -path $pathDeleteFrom -exclude $excludeFiles -Recurse
I'm testing the script in a folder with the following structure:
Example (running the script for path "C:/test/files/*"):
If "*.txt"
is excluded, only the .json
files are removed.
If I want to delete all files except the folder and it's contents I tried excluding "folder"
and "folder/*"
, however it doesn't seem to work.
What I'm failing to understand is, whether there's a way for me to exclude the "folder"
and it's contents using wildcards.
I know that this is probably a simple question but I have tried searching online but failed to find an example similar to the scenario that I have.
Upvotes: 0
Views: 1319
Reputation:
This should do what you're trying to accomplish... just fill in the blank and modify the condition statements to what you need.
$items = Get-ChildItem -Path "Directory Path" -Recurse
foreach($item in $items)
{
if($item.name -like "*.TXT" -or $item.name -like "*.Json")
{
#CODE HERE
}
elseif(($item.GetType()).Name-eq "DirectoryInfo" -and $item.name -eq "Directory Name")
{
#CODE HERE
}
}
Upvotes: 1