Advertisement
0utsidethebox

PowerShell with BingGPT: delete folders matching archive names

Oct 29th, 2023
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# 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 #>
  2.  
  3. # Get the list of files and folders in c:\test
  4. $items = Get-ChildItem -Path "c:\test"
  5.  
  6. # Loop through each item
  7. foreach ($item in $items) {
  8.     # Check if the item is a folder
  9.     if ($item.PSIsContainer) {
  10.         # Get the folder name
  11.         $folderName = $item.Name
  12.         # Check if there is a file with the same name and .rar extension
  13.         $rarFile = "c:\test\" + $folderName + ".rar"
  14.         if (Test-Path -Path $rarFile) {
  15.             # Delete the folder
  16.             Remove-Item -Path $item.FullName -Recurse -Force
  17.         }
  18.     }
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement