Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# There is a folder c:\test that contains multiple files and folders. Create powershell code that deletes only the folders with names that match rar archives located in the same folder c:\test #>
- $folderPath = "C:\test"
- $rarFiles = Get-ChildItem -Path $folderPath -Filter "*.rar" -File
- foreach ($rarFile in $rarFiles) {
- $folderName = $rarFile.BaseName
- $folderToDelete = Join-Path -Path $folderPath -ChildPath $folderName
- if (Test-Path -Path $folderToDelete -PathType Container) {
- Remove-Item -Path $folderToDelete -Recurse -Force
- Write-Host "Deleted folder: $folderToDelete"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement