Process files one by one

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Process files one by one

#16 Post by karaziki » 14 Feb 2015 22:43

What about this?

Now I have a solution. Got the main code and just modified a bit for flexible usage on different paths etc. Tested and working. But I have no idea comparing to dbenhams code. Which is better etc???

Note that, simply adding %NUMBER_OF_PROCESSORS% variable makes this code flexible between computers also.

This file is managing the multiple instances.

Code: Select all

@ECHO OFF
SETLOCAL

SET epath=%~dp0
CD /D %epath%


SET /a instances=%NUMBER_OF_PROCESSORS%
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
::
:loop
SET "nextfile=%~1"
IF NOT DEFINED nextfile (
 ECHO all done
 DEL "%tempfile%a*" >NUL 2>NUL
 GOTO :eof
)

FOR /L %%a IN (1,1,%instances%) DO (
 IF NOT EXIST "%tempfile%a%%a" (
  >"%tempfile%a%%a" ECHO.
  START "Instance %%a" oneconversion "%~1" "%tempfile%a%%a" %%a
  SHIFT
  GOTO loop
 )
)
timeout /t 1 >NUL
GOTO loop

GOTO :EOF


This one (called oneconversion.bat) does the action

Code: Select all

@ECHO OFF
SETLOCAL


SET fpath=%~dp1
   SET epath=%~dp0
CD /D %epath%

ECHO File Path  [ %fpath% ]
ECHO Exe  Path  [ %epath%bin ]
ECHO Core Count [ %NUMBER_OF_PROCESSORS% ]
ECHO ____________________________________________________________
COPY /Y "%~f1" "%fpath%\%~nx1.bak" >NUL

bin\truepng.exe /f4 /g0 /a0 /l q=4 m=1 /i0 /md remove all /zc9 /zm9 /zs1 /quiet /y /out "%~f1" %1 >NUL
timeout 1 >NUL
bin\pngwolf.exe --in="%~f1" --out="%~f1" --exclude-singles --exclude-heuristic --max-stagnate-time=2 --max-evaluations=1 --7zip-mpass=2 >NUL

timeout 1
DEL "%fpath%\*.bak" /F/Q

DEL "%~2" >NUL 2>NUL
cls
exit


Note: The line "DEL "%fpath%\*.bak" /F/Q" is deleting the backups. Simply comment it to keep them. And the commands on softwares (truepng and pngwolf) are the exact that I defined after tests. Anyone intereted in PNG optimize can try.

All bests and thanks to anyone gave a hand to help.

Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Process files one by one

#17 Post by Aacini » 16 Feb 2015 09:54

You may create an auxiliary file for each active instance and just count them, that is:

Code: Select all

@echo off
setlocal

CD /D "%~F0"

set maxInstances=%NUMBER_OF_PROCESSORS%

rem Delete all active-instance files
del instance*.txt 2> NUL
set instance=0
:loop

   :wait
      rem Count the number of active instances and check it
      for /F %%n in ('dir /B instance*.txt ^| find /C /V ""') do set activeInstances=%%n
      if %activeInstances% lss %maxInstances% goto startNew
      timeout /t 1 >NUL
   goto wait

   :startNew
   rem Start a new instance
   set /A instance+=1
   echo %time% > instance%instance%.txt
   start "Instance %instance%" oneConversion %instance% "%~1"

   shift
if "%~1" neq "" goto loop
echo all done
goto :EOF

oneConversion.bat:

Code: Select all

@echo off

do all required stuff here over file in %2

rem Delete the instance file of this instance before end
del instance%1.txt
exit /B

Antonio

Post Reply