0

I want to write a batch script that will check a network location for folders that are older than X days and delete them. I tried to use forfiles with /d -x, but I get an error that I can't use UNC paths. I've also tried using robocopy /move with /minage:x, but it's looking for files rather than folders.

Is there a way to do this?

indy-pc
  • 27
  • 1
  • 6
  • If any of the answers (including previous questions of yours) helped to solve your problem make sure to check them as accepted! This will not only help those who answered and the site that would know it does not have to bring attention to the question but also rewards you! – geisterfurz007 Dec 20 '16 at 15:17
  • You also should specify what you feel determines the age of a folder. I have a forty year old filing cabinet, even if I put a one day old document in it, the filing cabinet is still forty years old. So are you looking for a folder of any age which contains only files which have an age older than x days, _(if so, you may not need to get the folder age at all)!_ – Compo Dec 20 '16 at 15:44
  • Related: [Batch file to delete files older than N days](http://stackoverflow.com/q/51054); query whether `@isdir` is `TRUE` within the command line after the `/C` switch of `forfiles` to operate on directories rather than files, and use `rmdir` rather than `del` to actually delete directories... – aschipfl Dec 20 '16 at 16:08

1 Answers1

3

but I get an error that I can't use UNC paths.

True! But there is a workaround:

With the command pushd you can push a network location to a stack and automatically map a network drive. With popd you pop the directory, and unmap the drive again:

pushd "\\Server\Your Folder\"
REM do your thing with forfiles
popd
REM done

If there are questions left, feel free to ask :)

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54