繁体   English   中英

无法在 PowerShell 中使用 Remove-Item 从列表中删除文件

[英]Unable to delete files from the list using Remove-Item in PowerShell

我正在尝试使用 Remove-Item cmdlet 从变量“$exclude_files”中删除存储为 InputObject 名称的文件列表。

由于它是一个列表,我遍历列表并获取 InputObject 文件名。

下面是代码:

$source_dir ="C:\Files"

#Files are in below variable $exclude_files

$exclude_files

InputObject      SideIndicator
-----------      -------------
Credentials.xml  =>
EC2_Ubuntu.pem   =>
file2.png        =>
file3.txt        =>
Terminals.config =>

# tried with giving path and without giving path
foreach ($i in $exclude_files){ Remove-Item -Path $source_dir  $i.InputObject }

但是,我收到以下错误:

删除项目:找不到路径“C:\\Files\\file3.txt”,因为它不存在。 在 line:1 char:31 + foreach($i in $exclude_files){Remove-Item $i.InputObject} + ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ + CategoryInfo : ObjectNotFound: (C:\\Files...file3.txt:String) [Remove-Item], ItemNotFoundException +fullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

请尝试以下操作:

$exclude_files.InputObject | Remove-Item -Path {Join-Path $source_dir $_ }

看来, $i.InputObject用作输入-Filter参数(因为这是在第一个位置参数Path参数集),这可能不是预期的想法。

我得到了解决方案......正如你所建议的,当用 InputObject 遍历列表时,它的工作

foreach($i in $exclude_files)
            {
                $i.InputObject | Remove-Item -Path {Join-Path $source_dir $_}
    }

谢谢你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM