power shell remove-item recurse子目录3级下来

时间:2016-02-05 17:03:10

标签: powershell recursion

我无法找到一种方法来递归删除除了名为“broadcast”的每个文件夹中的子子目录之外的所有文件夹

Dir
->subdir1
-->main
-->broadcast
->subdir2
-->main
-->broadcast
->test

我想保留所有子目录#\ broadcast文件夹和测试文件夹。 下面这段代码的问题是,这将删除下面几个递归中的subdir1和subdir2,这将删除子子目录

Get-ChildItem -Path  $copy_file -Recurse |
Select -ExpandProperty FullName |
Where {$_ -notlike '*broadcast\*'  -and $_ -notlike '*testing*'} |
sort length -Descending |
Remove-Item -force 

我不能使用-force / a,因为我在排序时得到的这个列表包含了subdir1和subdir2 ......

It does prompt me "This item has children..."
[Y] Yes [A] Yes to all... [L] No to all

我确实希望它自动选择L

有什么建议吗?提前谢谢你...我确实看了一下,但找不到我想要的确切解决方案。

2 个答案:

答案 0 :(得分:0)

这将排除'broadcast'文件夹(您可以替换为您),然后您可以将该对象传递给remove-item

private Location getBestLocation() {
        Location gpslocation = getLocationByProvider(LocationManager.GPS_PROVIDER);
        Location networkLocation =
                getLocationByProvider(LocationManager.NETWORK_PROVIDER);
        // if we have only one location available, the choice is easy
        if (gpslocation == null) {
            Log.d("", "No GPS Location available.");
            return networkLocation;
        }
        if (networkLocation == null) {
            Log.d("", "No Network Location available");
            return gpslocation;
        }
        // a locationupdate is considered 'old' if its older than the configured
        // update interval. this means, we didn't get a
        // update from this provider since the last check

        // both are old return the newer of those two
        if (gpslocation.getTime() > networkLocation.getTime()) {
            Log.d("", "Both are old, returning gps(newer)");
            return gpslocation;
        } else {
            Log.d("", "Both are old, returning network(newer)");
            return networkLocation;
        }
    }

    /**
     * get the last known location from a specific provider (network/gps)
     */
    private Location getLocationByProvider(String provider) {
        Location location = null;
        /*if (!isProviderSupported(provider)) {
            return null;
        }*/
        LocationManager locationManager = (LocationManager) getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
        try {
            if (locationManager.isProviderEnabled(provider)) {
                try {
                    location = locationManager.getLastKnownLocation(provider);
                }catch(SecurityException e){
                    info.setText("No permission: "+e);
                }
            }
        } catch (IllegalArgumentException e) {
            Log.d("", "Cannot acces Provider " + provider);
        }
        return location;
    }

答案 1 :(得分:0)

这将导航顶部路径token并拉出所有子目录。不是直接的孩子递归。对于每个孩子,我们将过滤掉名为broadcast的所有文件夹。剩下的那些,删除它们

$copyfile

您可能需要将Get-ChildItem -Path $copy_file -Directory | ForEach-Object{ Get-ChildItem -Directory -Path $_.FullName | Where-Object{$_.Name -ne "broadcast"} | Remove-Item -Force -WhatIf } 添加到-Recurse,然后我将Remove-Item留在那里,以便您进行测试。