-2

I am running a window script to delete files from folder which have around 10k files .I want that my script command which is:

forfiles /p "path" /s /d -30 /c "cmd /c del @file : date >= 30 days >NUL"

,should delete 10 files and then wait for 5 sec and then again start deleting and then again wait for 5 secons and so on? I want this feature because if 10k files are deleted in one go , it will create hamper the normal working of server

Gerhard
  • 22,678
  • 7
  • 27
  • 43
SUMIT ANAND
  • 1
  • 1
  • 1
  • 1
    write clearly you question. If you want to pause inside batch, use `timeout /t ` – DmaNP69 Jan 19 '18 at 09:03
  • 1
    The problem is probably the command you're using, `ForFiles`. When it runs it opens a new `cmd.exe` instance for each matching file. So you are effectively opening `cmd.exe`, deleting one file and closing `cmd.exe` 10000 times. Also for your information, I would have suggested you use `@path` instead of `@file` and remove everything after it except for the closing doublequote because it's not valid. – Compo Jan 19 '18 at 10:40
  • Try This : `timeout /T 5 /nobreak` –  Aug 11 '19 at 08:37

2 Answers2

0

It is simple to achieve

timeout /T 5

With older Windows OS', (pre-Vista), where timeout is not available, you can simply use ping to do the wait. There is a second delay between each echo request, so for 5 seconds we need 6 ping counts. So by running the below it will wait 5 seconds.

ping 127.0.0.1 -n 6 >nul

To give you an idea how to do the delete you can do something like below: (I am using echo, but you can loop a del instead).

:start
for /L %%G IN (1,1,10) DO echo "Some command"
timeout /T 5
goto start
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

You could probably use a different command from ForFiles and run the script without pauses or the need to split the number of files into smaller chunks.

This uses RoboCopy with its /Move option:

@Echo Off

Set "_source=C:\Users\Sumit-Anand\Folder"
Set "_daysage=30"

Set "_scratch=%TEMP%\~%random%"
MD "%_scratch%"
RoboCopy "%_source%" "%_scratch%" /E /Move /Create /MinAge:%_daysage%
RD /S/Q "%_scratch%"

All you need to change is the value strings for the variables on lines 3 and 4, making sure that you do not remove the closing doublequote or include any doublequotes with your new string.

Compo
  • 36,585
  • 5
  • 27
  • 39