6

First of all: I'm totally new to batch.

I want to write a batch file which deletes files and folders which are min. 5 days or older. This code works perfect with all files:

FORFILES /p "C:\Users\rs\Desktop\testbatch" /s /m *.* /d -5 /c "cmd /c if not @isdir==TRUE del @path"

But my folders are still here and I don't get how to delete them too if they are min. 5 days old. Can someone please give me a hint?

Cheers

roemel
  • 3,247
  • 4
  • 29
  • 52
  • possible duplicate of [Batch file to delete files older than N days](http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – esac Oct 23 '13 at 07:56
  • Write a script below to the first line to delete the empty folders as well "for /f "delims=" %%d in ('dir /s /b /ad %1 ^| sort /r') do rd "%%d" 2>nul" . We assume that if a folder has 2 files , one is 2 days old and one is 6 days old, we just delete the file which is 6 days old. If all the files are more than 5 days old, delete the file and delete the directory. IF the first script works, use script mentioned to delete the empty folders. – Snake Eye Oct 23 '13 at 08:14
  • 1
    @esac: not a duplicate, this is about deleting folders, or more specifically asking why the folders themselves are not deleted, but the files are. – icabod Oct 23 '13 at 08:16
  • @esac It's not a duplicate. I asked why my folders still exists :) – roemel Oct 23 '13 at 08:16
  • This 'how do i delete files older than x days' question is asked quite a bit on here. I see 6 in the Related sidebar. There are many examples of how to accomplish it. Another one is not needed. In fact this one specifically says to use rmdir which is the answer to this same question. http://stackoverflow.com/questions/13517017/batch-file-to-delete-files-and-folder?rq=1 – esac Oct 23 '13 at 08:18
  • @esac Yes but I didn't found one that describes me why my folders still exists. I had a look at many of these posts but no one really helped me – roemel Oct 23 '13 at 08:21
  • @Roman : see this one http://stackoverflow.com/questions/13517017/batch-file-to-delete-files-and-folder?rq=1 it is the same question and answer. – esac Oct 23 '13 at 08:22

2 Answers2

2

del deletes the files, while you need to use rmdir for directories.

mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
  • With `rmdir` you mean `rd`? Is there a difference if I write `rmdir` or `rd`? – roemel Oct 23 '13 at 07:41
  • No, it's the same. I don't know which is an alias of the other, but I know both do the same thing. `rmdir` increases readability (IMO) to understand that you are removing directories, though, as with `mkdir` and `md`. – mavrosxristoforos Oct 23 '13 at 07:46
  • Ah okay, thank you. So I got now `if not @isdir==TRUE rd /s /q @path` instead of `if not @isdir==TRUE del @path`. This should be correct, am I right? It doesn't seem to work because folders are still here (for testing it I add `/d -0` to file). – roemel Oct 23 '13 at 07:49
  • You can change it to `echo "rd /s /q @path"` to see what is it actually trying to delete. – mavrosxristoforos Oct 23 '13 at 07:54
  • I tried this and there comes an error that there are no files to delete – roemel Oct 23 '13 at 08:07
  • 1
    You may want to change `*.*` to `*`. You're not looking for files. – mavrosxristoforos Oct 23 '13 at 08:13
  • Nice, thanks. What a mistake >.< I just looked to the last part of the command line and forgot the first part. Thank you guy :) – roemel Oct 23 '13 at 08:15
1
FORFILES /p "C:\Users\rs\Desktop\testbatch" /s /m *.* /d -5 /c "cmd /c if  @isdir==TRUE  rd /s /q  @path "
npocmaka
  • 55,367
  • 18
  • 148
  • 187