PowerShell 3:
Get-ChildItem -Path "C:\Users\myname\Desktop\project1\english\" -File -Recurse |
Where-Object { $_.Name -ne "file1.txt" -and $_.Parent -notin ("folder1","folder2") } |
Remove-Item
Situação:
Eu tenho várias pastas e arquivos, o que é assim:
C:\Users\myname\Desktop\project1\english\folder1\file1
C:\Users\myname\Desktop\project1\english\folder1\file2
C:\Users\myname\Desktop\project1\english\folder1\file3
C:\Users\myname\Desktop\project1\english\folder2\file4
C:\Users\myname\Desktop\project1\english\folder2\file5
C:\Users\myname\Desktop\project1\chinese\folder1\file1
C:\Users\myname\Desktop\project1\chinese\folder1\file2
C:\Users\myname\Desktop\project1\chinese\folder1\file2
C:\Users\myname\Desktop\project1\chinese\folder2\file4
C:\Users\myname\Desktop\project1\chinese\folder2\file5
Meta:
Gostaria de excluir todas as pastas e arquivos, exceto "pasta1" e "arquivo1", para que eu fique com isto:
C:\Users\myname\Desktop\project1\english\folder1\file1
C:\Users\myname\Desktop\project1\chinese\folder1\file1
Pergunta:
Isso pode ser feito com o Powershell?
Eu fiz uma pergunta semelhante antes, e um usuário brilhante sugeriu a solução abaixo, no entanto, ela exclui apenas arquivos, não pastas.
$toKeep = "File1", "File3"
gci startFolder -Recurse | ? { !$_.PSIsContainer } | ? { $toKeep -notcontains $_.Name } | remove-item -WhatIf
PowerShell 3:
Get-ChildItem -Path "C:\Users\myname\Desktop\project1\english\" -File -Recurse |
Where-Object { $_.Name -ne "file1.txt" -and $_.Parent -notin ("folder1","folder2") } |
Remove-Item