首页 > 解决方案 > Powershell查找文件夹/子文件夹,然后写入if条件以仅在文件夹A大于文件夹B时执行操作

问题描述

基本上需要写给我子目录列表的powershell。在我的子目录中,我的文件夹结构类似于 101.10.1 、 101.10.2 、 101.10.3 现在想写 if 条件从文件夹 (101.10.3) 中获取更高的目录并删除 ms sql 2005 应用程序。

像这样的东西

    ****$path = "C:\abc\xyzh\Versions"
$contents = Get-ChildItem -Path $path | sort | Select-Object -Last 10
$contents.Name
$condition = $contents.Name
if ( $condition -gt "$path/$condition" )
{
        # do something"
    }****

标签: powershellif-statement

解决方案


假设你有:

    101.10.1
    101.10.2
    101.10.211
    101.10.3
    101.10.323
    101.10.4
    101.10.5
    101.10.610

如果您运行此命令:

gci |  sort -property name | ?{$_.name -gt '101.10.3'} | %{ echo "dosomething $_"  }

你得到:

dosomething 101.10.323
dosomething 101.10.4
dosomething 101.10.5
dosomething 101.10.610

现在,您可以将 echo "dosomething $_" 更改为另一个有用的命令。


推荐阅读