如果文件名与 CSV 中的单词匹配,则删除目录中的项目
Remove items in directory if filename matches words from CSV
我一整天都在为此苦苦挣扎。如果文件名与 CSV 列表中的单词匹配,我将尝试删除给定目录中的所有文件。
我已将 CSV 列表作为文本导入,然后尝试将单词与目标目录中的文件名进行匹配,但我不确定要使用哪个运算符或如何进行此操作。显然 'Match' 不是 cmdlet。
#store csv file as variable
$CSVList = Get-content -Delimiter (',') -path "C:\Users\Leeds TX 11\Desktop\Test folder\Watchfolder\DAZN Production Numbers - purgelist.csv"
write-host $CSVList
#delete if word matches .csv entry
$targetdirectory = "C:\Users\Leeds TX 11\Desktop\Test folder\AVMedia"
$Files = Get-childItem $targetdirectory | Match ("\w") $CSVList | remove-item -whatif
write-host $Files
只需在检查 Name
属性 是否为 -In
或 $CSVList
:
的管道中添加一个 Where-Object
# ...
$Files = Get-ChildItem $targetdirectory | Where-Object Name -In $CSVList | Remove-Item -whatif
注意:我认为您的 CSV 中的文件名包含文件扩展名。如果不是,你想将 Where-Object Name
更改为 Where-Object BaseName
。
编辑。 由于您使用的是 PowerShell 版本 2,因此必须使用 -contains
并交换变量:
$Files = Get-ChildItem $targetdirectory | Where-Object {$CSVList -contains $_.BaseName} | Remove-Item -whatif
试试这样的东西
#store csv file as variable and take first column
$CSVList = (import-csv "c:\temp\missing.csv" -Header col1).col1
#delete if word matches .csv entry
$targetdirectory = "C:\temp2"
$Files = Get-childItem $targetdirectory -Recurse | Where-Object {$CSVList -contains $_.BaseName } | remove-item -whatif
write-host $Files
我一整天都在为此苦苦挣扎。如果文件名与 CSV 列表中的单词匹配,我将尝试删除给定目录中的所有文件。
我已将 CSV 列表作为文本导入,然后尝试将单词与目标目录中的文件名进行匹配,但我不确定要使用哪个运算符或如何进行此操作。显然 'Match' 不是 cmdlet。
#store csv file as variable
$CSVList = Get-content -Delimiter (',') -path "C:\Users\Leeds TX 11\Desktop\Test folder\Watchfolder\DAZN Production Numbers - purgelist.csv"
write-host $CSVList
#delete if word matches .csv entry
$targetdirectory = "C:\Users\Leeds TX 11\Desktop\Test folder\AVMedia"
$Files = Get-childItem $targetdirectory | Match ("\w") $CSVList | remove-item -whatif
write-host $Files
只需在检查 Name
属性 是否为 -In
或 $CSVList
:
Where-Object
# ...
$Files = Get-ChildItem $targetdirectory | Where-Object Name -In $CSVList | Remove-Item -whatif
注意:我认为您的 CSV 中的文件名包含文件扩展名。如果不是,你想将 Where-Object Name
更改为 Where-Object BaseName
。
编辑。 由于您使用的是 PowerShell 版本 2,因此必须使用 -contains
并交换变量:
$Files = Get-ChildItem $targetdirectory | Where-Object {$CSVList -contains $_.BaseName} | Remove-Item -whatif
试试这样的东西
#store csv file as variable and take first column
$CSVList = (import-csv "c:\temp\missing.csv" -Header col1).col1
#delete if word matches .csv entry
$targetdirectory = "C:\temp2"
$Files = Get-childItem $targetdirectory -Recurse | Where-Object {$CSVList -contains $_.BaseName } | remove-item -whatif
write-host $Files