Announcement

Collapse
No announcement yet.

Need to delete files from sub folders but not from the root

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to delete files from sub folders but not from the root

    Hi Folks,
    I've been requested by the powers that be to find away to clean up some files left by one of our software suites.

    So the scenario is:
    I have a folder
    This folder has a bunch of important files in it
    Within the folder are a number of sub folders
    Within these sub folders are a bunch of temp files that are not important.

    So I need to be able to remove all files within the sub folders but leave the files in the root of that folder intact.
    The sub folders don't need to be deleted either, they can stay put.

    So I was looking at something similar to:
    Code:
    del /S /Q /F "C:\folder"
    But I know that this would remove the files in the root of the folder.
    I thought this might have been a good way to go
    Code:
    del /S /Q /F "C:\folder\*\*"
    But del doesn't seem to like wildcards for directories.
    Same applies with robocopy. I thought I might be able to robocopy the files to a temp folder and then run the first del command above but I just got a parameter error.
    Code:
    robocopy "C:\Users\tlane\Desktop\New folder" "C:\Users\tlane\Desktop\newfolderpurge" /S /MOV /XF "C:\Users\tlane\Desktop\New folder" /R:1 /W:1
    or
    Code:
    robocopy "C:\Users\tlane\Desktop\New folder\*\*" "C:\Users\tlane\Desktop\newfolderpurge" /S /MOV /R:1 /W:1
    So that's about the extent of my ability to code.
    If someone has a better suggestion I'd be appreciative.

    If needed this could also be a vbscript if that works better.
    Appreciate the help.

    Trent

  • #2
    Re: Need to delete files from sub folders but not from the root

    You can try something like,
    Code:
    @echo off
    
    Set "sDir=C:\folder"
    
    If exist "%sDir%\." (
       for /f "tokens=*" %%! in (
       'dir /AD /B "%sDir%\."') do del /S /Q /F "%sDir%\%%!\*")
    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Need to delete files from sub folders but not from the root

      Thanks for that script Rems!

      So the goal posts had changed

      I needed to exclude a handful of sub directories within the root of that folder
      I did some further Googling and found a way to exclude those folders

      So the code ended up as this:

      Code:
      @echo off
      
      Set "sDir=C:\folder"
      
      If exist "%sDir%\." (
         for /f "tokens=*" %%! in (
         'dir /AD /B "%sDir%\." ^|Findstr /I /V /C:"ExcFolder1" /C:"ExcFolder2"') do del /S /Q /F "%sDir%\%%!\*")
      That seems to do what I need and not touch any of the sub directories that need to be kept.

      Appreciate the help and thanks for pointing me in the right direction!

      Trent

      Comment

      Working...
      X