RunOnAllCores.ahk

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
Hacker
Moderator
Moderator
Posts: 13064
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

RunOnAllCores.ahk

Post by *Hacker »

Hi all,
I created a small AHK_L script that runs a specified program on each of the selected files in TC sequentially (like lst2run) but using all available cores. Say you have a quad core and have 200 images to resize using one command. Either you select 50, run the command, select the next 50, run the command, etc., or you select 200 and wait four times longer. With this script you just select them all and run the command.

RunOnAllcores.ahk

Code: Select all

EnvGet, Cores, NUMBER_OF_PROCESSORS
Threads = %Cores%
FileRead, List, %1%
StringSplit, File, List, `n, `r`n

CurFileIndex = 1

Loop, %Threads%
	PID%A_Index% = 0

Loop
{
	Loop, %Threads%
	{
		Process, Exist, % PID%A_Index%
		IfEqual, ErrorLevel, 0
		{
			CmdLine = %2%
			StringReplace, CmdLine, CmdLine, #####, % File%CurFileIndex%, All
			Run, %CmdLine%, , , PID%A_Index%
			CurFileIndex++
		}
		IfEqual, CurFileIndex, %File0%
			Break

	}

	Sleep, 250

} Until CurFileIndex = File0
ExitApp

Pause::Pause

^!#+NumpadSub::
		IfNotEqual, Threads, 1
			Threads--
Return

^!#+NumpadAdd::
	IfNotEqual, Threads, %Cores%
		Threads++
Return
Usage:
Put on a button, hotkey, menu entry or alias, specify:

Code: Select all

%L <your_command_here>
as the Parameters, and put a ##### placeholder where you want to have the file name of the file to be processed inserted.
The Pause key pauses the script execution (the running of the commands).

Example:

Code: Select all

Command: RunOnAllCores.ahk
Parameters: %L "C:\Programs\XnView\nconvert.exe -out jpeg -q 80 -v. -opthuff -rexifthumb -rtype lanczos -resize 25%% 25%% -overwrite -keep_icc -dct 2 -keepfiledate #####"
If you are annoyed by windows popping up, you can hide them entirely by replacing the line

Code: Select all

Run, %CmdLine%, , , PID%A_Index%
with

Code: Select all

Run, %CmdLine%, , Hide, PID%A_Index%
If you wish to lower the amount of cores used (default is to use all cores), press Ctrl-Alt-Win-Shift-NumMinus. If you want to increase the number again, press Ctrl-Alt-Win-Shift-NumPlus.

HTHS
Roman

[EDIT: Version 2, allowing to decrease/increase number of cores used.]
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Post by *white »

2Hacker
I have made some small improvements to prevent an endless loop when no files are selected and to only pause (sleep) when all threads are busy. It's my first ahk code, so I hope I have it right.

Code: Select all

EnvGet, Cores, NUMBER_OF_PROCESSORS 
Threads = %Cores% 
FileRead, List, %1% 
StringSplit, File, List, `n, `r`n 

IfEqual, File0, 0, ExitApp              ; Prevent endless loop when no files are selected
 
CurFileIndex = 1 

Loop, %Threads% 
   PID%A_Index% = 0 

outer_loop:
Loop
{ 
   Loop, %Threads% 
   { 
      Process, Exist, % PID%A_Index% 
      IfEqual, ErrorLevel, 0 
      { 
         StringReplace, CmdLine, 2, #####, % File%CurFileIndex%, All 
         Run, %CmdLine%, , , PID%A_Index% 
         CurFileIndex++ 
         IfEqual, CurFileIndex, %File0% ; Last file name is empty, so stop at last file
            Break outer_loop
      } 

   } 
   IfEqual, CurFileIndex, %OldIndex%    ; All threads busy, so sleep
      Sleep, 250 
   else
      OldIndex := CurFileIndex

}
ExitApp 

Pause::Pause 

^!#+NumpadSub:: 
      IfNotEqual, Threads, 1 
         Threads-- 
Return 

^!#+NumpadAdd:: 
   IfNotEqual, Threads, %Cores% 
      Threads++ 
Return
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: RunOnAllCores.ahk

Post by *white »

Hacker wrote:Usage:
Put on a button, hotkey, menu entry or alias, specify:

Code: Select all

%L <your_command_here>
as the Parameters, and put a ##### placeholder where you want to have the file name of the file to be processed inserted.
The Pause key pauses the script execution (the running of the commands).

Example:

Code: Select all

Command: RunOnAllCores.ahk
Parameters: %L "C:\Programs\XnView\nconvert.exe -out jpeg -q 80 -v. -opthuff -rexifthumb -rtype lanczos -resize 25%% 25%% -overwrite -keep_icc -dct 2 -keepfiledate #####"
That would fail on file names with spaces in them. Perhaps usage could be better described as:

Usage:
Put on a button, hotkey, menu entry or alias. For parameters specify:

Code: Select all

%L "<your_command>"
Put a ##### placeholder where you want to have the file name of the file to be processed inserted and add an extra double quote for each double quote in the command. Example:

Code: Select all

Command: RunOnAllCores.ahk
Parameters: %L """C:\Programs\XnView\nconvert.exe"" -out jpeg -q 80 -v. -opthuff -rexifthumb -rtype lanczos -resize 25%% 25%% -overwrite -keep_icc -dct 2 -keepfiledate ""#####"""
User avatar
Hacker
Moderator
Moderator
Posts: 13064
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Post by *Hacker »

white,
Indeed, it's always a good idea to quote a command line parameter if it could contain a space and also the filename placeholder. Thanks for the note.

Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
Post Reply