![특정 파일이 포함된 폴더를 삭제하고 다른 파일은 유지](https://rvso.com/image/1618362/%ED%8A%B9%EC%A0%95%20%ED%8C%8C%EC%9D%BC%EC%9D%B4%20%ED%8F%AC%ED%95%A8%EB%90%9C%20%ED%8F%B4%EB%8D%94%EB%A5%BC%20%EC%82%AD%EC%A0%9C%ED%95%98%EA%B3%A0%20%EB%8B%A4%EB%A5%B8%20%ED%8C%8C%EC%9D%BC%EC%9D%80%20%EC%9C%A0%EC%A7%80.png)
삭제하고 싶은 폴더 모음이 있습니다. 이 모든 폴더에는 index.html
.
예를 들어 이 폴더에는 다음 파일이 포함되어 있으므로 제거해야 합니다 index.html
.c:\folder2\folder3\index.html
그러나 동일한 루트 폴더 아래에는 다음과 같은 허용된 파일이 포함된 폴더가 있습니다.c:\folder2\folder1\software.rar
그래서 모든 파일을 찾아 index.html
상위 폴더를 변수로 유지하고 나중에 삭제하는 솔루션이 필요합니다.
이것을 테스트했는데 좋은 목록이 만들어졌지만 삭제가 작동하지 않습니다.
dir /ad /b /s %1 >list.txt for /f %%g in (list.txt) do if exist %%g\index.html rd /s /q %%g
답변1
윈도우 10 64비트. 파워셸 5.1
다음을 포함하는 디렉토리만 삭제index.html
테스트를 위해 데스크탑을 정리하세요. folder2
데스크탑에는 이름이 지정된 디렉토리가 없어야 합니다 . folder2
데스크탑에 이름의 디렉토리가 생성됩니다.
pushd $HOME\Desktop;ni -itemtype directory "folder2\folder3", "folder2\folder1">$null;ni -itemtype file "folder2\folder3\index.html", "folder2\folder1\software.rar">$null;explorer .\folder2;cls
Read-Host "
Test files have been created on your desktop. See explorer.
Press enter key to continue"
Get-ChildItem .\folder2\*\index.html | ForEach-Object {
remove-item $_.directoryname -recurse
}
explorer .\folder2;cls
Read-Host "
Directories containing index.html have been deleted. See explorer.
Press enter key to delete all test files"
ri (".\folder2") -recurse
popd
#
답변2
파워셸:
말 수가 많은:
$root = 'c:\TopLevel'
Get-ChildItem -path $root -filter index.html -File -Recurse | Select -expand Directory | Remove-Item -Recurse
짧은 버전:
$root = 'c:\TopLevel'
gci $root index.html -Recurse | select -expand directory | ri -Recurse