Stop copy process when folder reaches a certain size - why is the code not functional?

1

I have written a code in batch that copies pictures to a certain folder

pic.bat

start temp\check.bat
for /r "%userprofile%" %%a in (*.jpg) do @copy /y "%%a" temp

And I would like to stop this process once the folder reaches a certain size, so I have tried to monitor the folder size using another batch, and taskkill the copy process once a certain size is reached

check.bat

:loop
setlocal enabledelayedexpansion
set /a value=0
set /a sum=0
for /r %1 %%i in (*) do (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
if %sum% gtr 150000 goto end
goto loop
:end
taskkill /im pic.bat

The check.bat is positioned within the temp folder, where the pictures are being copied + it runs at the same time as pic.bat - yet nothing happens when the limit is breached. Do you see something wrong here? I apologize if the answer is obvious, I'm new to all of this.

Daniel

Posted 2017-04-15T03:20:17.003

Reputation: 283

Try calling taskkill with /f (force) and/or /t (tree) flags. I suspect /t will do it. – ventsyv – 2017-04-15T03:37:58.113

If you run check.bat, do you see the commands being executed with the correct values displayed? Easiest way to do this would to ensure the folder is greater than the size limit, then run check and observe if it tries to execute the taskkill command – I say Reinstate Monica – 2017-04-15T04:57:57.073

Why don't you just check the limit in pic.bat after copying each file? ie combine the two batch files. Then instead of using taskkill just break out of the copy for loop. – DavidPostill – 2017-04-15T10:00:15.133

I solved the problem by getting the Process ID of the pic.bat process and then printing that Process ID to a text file, which check.bat read and used to kill the process (taskkill /pid %pid_here%). It seems that the issue was that only cmd.exe was recognized by taskkill, not pic.bat. – Daniel – 2017-04-15T14:04:52.337

Congratulations - you have successfully created a Rube Goldberg machine

– dbenham – 2017-04-16T12:23:51.297

No answers