使用文件名列表删除文件

使用文件名列表删除文件

本文介绍了powershell - 使用文件名列表删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从堆栈溢出中得到以下代码,并且运行良好.

I got the the following code from stack overflow and it works fine.

$TargetFolder = "Pathofyourfolder"
$Files = Get-ChildItem $TargetFolder -Exclude (gc List.txt)  -Recurse
foreach ($File in $Files)
{
    write-host "Deleting File $File" -foregroundcolor "Red";
    Remove-Item $File | out-null
}

现在我想删除文件名在列表中的文件.我尝试了上述的一些变体,例如用 Include 替换 Exclude 但没有成功.请问有人可以帮忙吗?

Now I want to delete the files with file names on the list. I tried some variations of the above such as replacing Exclude with Include but without success. Can anyone help please?

推荐答案

$targetFolder = "D:\TEST_123"
$fileList = "D:\DeleteList.txt"

Get-ChildItem -Path "$targetFolder\*" -Recurse -Include @(Get-Content $fileList) | Remove-Item -Verbose

要使 -Include 工作,您应该在删除列表中的文件夹名称和文件名的末尾指定 \*.上面的代码对我有用,只删除文件夹及其所有子文件夹中的指定文件.

For -Include to work you should specify \* at the end of a folder name and filename with extension in your deletion list. The code above works for me, deleting only specified files in folder and all of its subfolders.

我还使用了 -Verbose 而不是 foreachWrite-Host.

I also used -Verbose instead of foreach and Write-Host.

这篇关于powershell - 使用文件名列表删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:17