Affinity.bat - Batch file to Start a Process & Assign CPUs to It


By: hidde663
December 13, 2017

@echo off
:: example start notepad.exe on with core 1 & 4 from cmd:
::affinity notepad.exe 1+4
::this will start notepad with cores CPU0 & CPU3 assigned to it
if '%1'=='/?' goto help
if '%1'=='' exit/b
if '%2'=='' exit/b
for /L %%1 in (1,1,%NUMBER_OF_PROCESSORS%) do set core%%1=0
set cnt=1
:loop
for /F "tokens=%cnt%-%NUMBER_OF_PROCESSORS% delims=+" %%a in ("%2") do set core%%a=1
if '%cnt%' LSS '%NUMBER_OF_PROCESSORS%' set/a cnt+=1&goto loop
:loop1
set/a ans=0x%core1%*0x1+0x%core2%*0x2+0x%core3%*0x4+0x%core4%*0x8
if '%ans%'=='10' set ans=A
if '%ans%'=='11' set ans=B
if '%ans%'=='12' set ans=C
if '%ans%'=='13' set ans=D
if '%ans%'=='14' set ans=E
if '%ans%'=='15' set ans=F
::0000
::1+4 = 1001 = 9 = 0xA = A
::core 1(0) = 1
::core 2(1) = 2
::core 3(2) = 4
::core 4(3) = 8
::sum values to use more cores
::0xf = 1111 – a mask allowing use of only the first four processors:
start /AFFINITY %ans% %1
exit/b
:help
echo exmaple: affinity notepad 1+3
echo.
echo parameter 1 = file to be executed
echo parameter 2 = which cores
exit/b


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question