0

I have a question about batch files. For instance,

forfiles /p "C:\Users\ ..." /s /m . /D -150 /C "cmd /c del @path"

Is it possible to run this, then change the -150, to -130, -110 automatically instead of writing multiple commands like

TITLE DELETE OLD FILES

forfiles /p "C:\Users\ ..." /s /m . /D -150 /C "cmd /c del @path"

forfiles /p "C:\Users\ ..." /s /m . /D -130 /C "cmd /c del @path"

forfiles /p "C:\Users\ ..." /s /m . /D -110 /C "cmd /c del @path"

PAUSE

Rationale: The system stores data about a semiconductor related equipment and holds files which are about more than 2 years old. However, deleting these files immediately will cause a crash. Hence, I am deleting these files periodically.

I will also appreciate if there is a command which allows the user to choose when to display the current storage space available, and then allows the user to choose whether to delete the next batch of files.

I have asked the same question, "https://stackoverflow.com/questions/72428986/how-to-delete-batch-files-cmd-from-the-oldest-date-first-then-iterate-till-a" and answered the responses. However, I am still stuck and unfamiliar on how to proceed.

Thank you.

  • 1
    The simplest method would be `for %%d in (-150 -130 -110) do forfiles /p "C:\Users\ ..." /s /m . /D %%d /C "cmd /c del @path"` but that would apply the three values successively, and be equivalent to one pass of `-110`, so I don't really see what your batch would achieve... – Magoo Jun 08 '22 at 03:32
  • Great! That's what I need. I actually add > nul 2> nul after the path directory to remove error messages. It would be nice if for every deletion, it shows how much data is removed too. To Magoo: I would assume that your method would delete the files older than -150 days, then -130 days, then -110 days, which most probably won't crash as the next deletion would delete files between -150days to -130 days, which is considerably less files than let's say -150 days to -110 days. Wondering if I could optimize the interval to delete the files accordingly. – Flora Trace Jun 08 '22 at 06:30
  • 1
    You could use `for /L %%d in (-150,5,-130) do...` which would start at -150, repeat for -145, etc. down to -130. (start,step,end). Perhaps you could add `&dir ?: |find "bytes free"` to your `forfiles` command, which should show you the resultant free space on drive `?` after each iteration – Magoo Jun 08 '22 at 07:07
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jun 08 '22 at 14:05

0 Answers0