Powershell:获取文件夹的内容,删除特定文件,验证文件是否已删除/存在

时间:2017-07-28 20:58:48

标签: powershell

所有工作都必须记录,我试图列出文件夹的内容,删除某些文件,在我的例子中我想要删除的文件夹中有3个文件。然后列出每个文件以查看文件是否存在(未删除)或不存在(已删除)。

这是我到目前为止所能做的:

    $ErrorActionPreference = "SilentlyContinue"; 
    $mindump = gci c:\test1 -recurse -Include Minidump*.dmp 
    remove-item $mindump -force -whatif

当我想验证哪些文件被删除时:

    $mindump | % { $a =$_; test-path $_ | where {$_ -eq $True} | %{ write-host $a File still exists or a new file with the same name was created}} 

它可以找出文件是否仍然存在,但是如果我尝试这样的话:

    $mindump | % { $a =$_; test-path $_ | where {$_ -eq $True} | %{ write-host $a File still exists or a new file with the same name was created} | % else { write-host $a File was deleted/does not exists} }

根本不起作用。我还能做些什么?

1 个答案:

答案 0 :(得分:2)

您有一个简单的语法错误。您无法使用else作为对ForEach-Object循环的回复,您需要使用If语句。

$mindump | % { 
    $a =$_
    If(test-path $_){ 
        write-host $a File still exists or a new file with the same name was created
    } else { 
        write-host $a File was deleted/does not exists
    } 
}