Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Only a part of an array is choosed

$
0
0

Hi Guys!

I've got  an array only a part of this array is choosed  but i want to choose all the array:

The code:

function Move-EmptyDir{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)][string]$Source,
        [Parameter(Mandatory = $true)][string]$Destination,
        [ValidateNotNullOrEmpty()][regex]$NoMatch
    )

    $EmptyFolders=@()
    $AllFolders= (Dir $Source -Force -Recurse) | Where-Object{$_.Attributes -match "Directory"}
    $ExistFolders= $AllFolders | Group-Object -Property Exists | Where-Object{$_.Name -match "True"} #verify
    $Inexisting= $AllFolders | Group-Object -Property Exists | Where-Object{$_.Name -match "False"}
    if($Inexisting.Count -gt 0){
        ForEach($Not in $Inexisting){
            Write-Warning "The Folder $Not is not existing"
        }
    }
    Foreach($Item in $ExistFolders.Group){
        $MeltinCount= $Item.GetFileSystemInfos().count #0= no element(s)
        $SimCount= (($ExistFolders.Group | Group-Object -Property Parent) | Where-Object{$_.Name -eq $Item.Name}).Count #0=no subfolders
        if($MeltinCount -eq $SimCount){
            $EmptyFolders+= $Item
        }
        else{
            Write-Warning "The Folder $Item is have file(s)"
        }
    }

    $Moved= @()
    $Moving= $EmptyFolders.Length -1
    
    While($Moving -ne -1){
        $OneFolder= $EmptyFolders[$Moving] #<---------Maybe the problem is near here
        Write-Host $OneFolder.name -BackgroundColor Red
        if($OneFolder.GetFileSystemInfos().count -gt 0){ #Parent have some directory(but 0 files)
            Write-Warning "This Folder $OneFolder is not empty"
            $Moving--
        }
        elseif($OneFolder.Name -match $NoMatch){
            Write-Warning "The Folder $OneFolder Match the Exceptions"
            $Moving--
        }
        else{
            if($OneFolder.parent.Name -match $NoMatch){
                Write-Host "The Parent Folder of $OneFolder Match the Exceptions" -BackgroundColor Green
            }
            $Moved += $OneFolder
            $Place = $OneFolder.FullName -replace "^$([regex]::Escape($Source))", $Destination
            if((Test-Path $Place) -eq $false){
                New-Item -Path $Place -ItemType Directory
                Remove-Item -Path $OneFolder.fullname -Force -ErrorAction SilentlyContinue
                Write-Verbose ('Moved folder "{0}" to "{1}"' -f $OneFolder.FullName, $Place)
                $Moving --
            }
            else{
                Remove-Item -Path $OneFolder.FullName -Force -ErrorAction SilentlyContinue
                $Moving --
            }
        }
    }
}

$Where= 'C:\temp'
$ToGo= 'C:\estinto'
$MatchList= '0','1','8','9','Scansion'
$Kind= 'Empty'
if(Test-Path $Where){
    #$TheMatch = Select-Match -FullList $MatchList
    $Folders = Move-EmptyDir -Source $Where -Destination $ToGo -NoMatch 'F' -Verbose
    $Drives = ForEach($Item in $Folders){
        Select-Object -Property @(
            @{ Name = 'Name'; Expression = { $Item.Name } }
            @{ Name = 'Percorso'; Expression = { $Item.Parent.FullName } }
        )
    }
    #$Drives
    #Get-Report $Drives $Kind
}
else{
    Write-Error 'Invalid Path' -Category ResourceUnavailable
}

My folder structure Temp>F1>F2>F3>E4 & Temp>G1>G2 F1& G1 have files, F2 have only F3,F3 have only E4, E4& G2 empty

Please create this structure and run the code one time: E4 is not choosed

Thanks


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>