【发布时间】:2017-10-02 12:19:39
【问题描述】:
我刚开始玩 powershell,所以请执行我的新手错误。 我正在搜索文件并将文件的路径存储在变量中,然后我想检查该路径中是否有特定文件,然后 删除他们。我在这方面找不到太多帮助,我认为 if 语句中存在问题,但我无法弄清楚。
代码如下:
$startDirectory = ".\somepath\"
$FindFileXml = Get-Childitem $startDirectory
$StorePaths = Get-Childitem $FindFileXml -include "file.xml" -recurse
if($StorePaths -eq "somefile.cs" -and "diffrentfile.cs")
{
Remove-Item "somefile.cs"
Remove-Item "diffrentfile.cs"
}
else{
Write-host "file not found!"
}
更新:
$StorePaths = Get-Childitem $FindFilterXml-Filter "file.xml" -recurse
$StorePaths | Select-Object -ExpandProperty Directory
| Get-ChildItem |
Where-Object {($_.Name -eq 'GeneratedCode.cs') -or ($_.Name -eq 'TypedEnums.cs')}
| Remove-Item
这会将 File.xml 的路径存储在 $StorePaths 中吗?如果是这样,我该如何复制它,例如 Copy-Item $StorePath.name .\somepath?
【问题讨论】:
-
是的,这会将file.xml的路径存储在$storePaths中。您可以通过管道将其复制到 copy-item cmdlet,例如。 G。 :
$StorePaths | Copy-Item -Destination 'your_path'
标签: shell powershell powershell-3.0