2

How can I remotely delete subfolders older than x days? Was able to find a syntax like this, but it deletes all of the subfolders in xFOLDER. Any idea how I can modify the syntax and delete only subfolders in xFOLDER older than 7 days?

FOR /D %i IN (\\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\*) DO RD /S /Q "%i"
DEL /Q \\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\*.*
user2784400
  • 41
  • 1
  • 2
  • 9
  • 3
    Look at the FORFILES command – Squashman Nov 02 '15 at 03:07
  • 1
    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) – SomethingDark Nov 02 '15 at 03:19
  • @Squashman tried FORFILES. But it doesnt work for UNC paths. I'll be remotely deleting files. – user2784400 Nov 02 '15 at 03:20
  • 2
    @user2784400 then use PUSHD. – Squashman Nov 02 '15 at 03:21
  • If `pushd` does not work directly you can try to map with `net use` first – npocmaka Nov 02 '15 at 09:46
  • 1
    [This question](http://stackoverflow.com/questions/5497211/batch-file-to-delete-folders-older-than-10-days-in-windows-7) resembles yours, and has answers you should find useful. – Bloodied Nov 02 '15 at 16:25
  • Tried PUSHD, but it does not delete the folders older than 7 days, it loops through the subfolders and deletes the file older than 7 days, Any idea how to tweak this? : PushD "\\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\" &&("forfiles.exe" /s /m "*.*" /d -7 /c "cmd /c del @file") & PopD – user2784400 Nov 05 '15 at 05:52

1 Answers1

0

I manage to delete directory and subdirectory using this command

RD remove directory

/S use to remove directory tree

/Q quiet mode

forfiles /D -7 /C "cmd /c rd /s /q @path"

change the code accordingly to your needs.

Tengku Fathullah
  • 1,279
  • 1
  • 18
  • 43