Announcement

Collapse
No announcement yet.

DOS Help: Command forfiles

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

  • DOS Help: Command forfiles

    I am trying to remove old data from 100's of sub folders.
    EX: c:\subfolder\1
    c:\subfolder\2
    c:\subfolder\3
    c:\subfolder\4

    etc ....

    I have the following command and it works removing old files older then 15 days.

    forfiles /P "c:\subfolder" /S /M * /D -15 /C "cmd /c del @PATH"


    My problem is it prompts me for every sub folder statying

    c:\subfolder\1 are you sure Y/N


    My question - is there away to surpress the prmopting and do ALL sub folders?


    Thank you for your help in advance.

  • #2
    Re: DOS Help: Command forfiles

    It is the normal behavior for a DEL command when just the path or name of a folder is provided.
    With DEL you cannot delete folders, it will interpret a foldername in the command as: foldername\* -so it adds a wildcard this way all files in the folder will be deleted not the folder itself.
    The DEL command always ask for confimation when a wildcard is used instead of a name of a file.

    So you should answer with a N (No) or else all files in this folder will be deleted even the files not older than 15 days.
    echo.N | DEL "foldername"

    or better,
    What you should add to the Forfiles stament is the If @isDir==FALSE condition. Then it will only delete old files.
    Code:
    @echo off
    
    PushD "c:\subfolder"||goto:EOF
       forfiles.exe /P . /S /M * /D -15 /C "cmd /c if @isdir==FALSE (DEL @Path /F /A)"
    PopD

    And if you like you can afterwards delete the empty subfolders,
    Code:
    @echo off
    
    PushD "c:\subfolder"||goto:EOF
       forfiles.exe /P . /S /M * /D -15 /C "cmd /c if @isdir==FALSE (DEL @Path /F /A)"
       for /f "tokens=*" %%d in ('dir . /ad /b /s ^|sort /R') do rd "%%d"
    PopD
    /Rems
    Last edited by Rems; 19th October 2012, 12:58.

    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: DOS Help: Command forfiles

      Awesome - Thanks again for your help and explanation.

      Comment

      Working...
      X