Your for loop ends with goto :EOF thus telling to abort the for loop and leave the function. Similar as when you would write return inside a java program the function is exited immediately commands behind it will not be executed, loops aborted.

You probably expect for x in ( set ) do ( commands ) &command will only iterate the block, this is not the case. for assumes you want to loop block & command.

In this case proper syntax to make it do what you expect is ( for x in ( set ) do commands ) &command.

Your for loop ends with goto :EOF thus telling to abort the for loop and leave the function. Similar as when you would write return inside a java program the function is exited immediately commands behind it will not be executed, loops aborted.

You probably expect for x in ( set ) do ( commands ) &command will only iterate the block, this is not the case. for assumes you want to loop block & command.

In this case proper syntax to make it do what you expect is ( for x in ( set ) do commands ) &command.

Thank you, it was very helpful.

But I still unable to loop through, does that mean I need EnableDelayedExpansion for the Loop and escape variable with exclamation marks?

But what really happens is
Set "DeviceID[x]=%x%%x%"
Set "DeviceID[x]="

You've been mislead by the fact that you are using immediate expansion inside a block. So you'll get the unchanged value of DeviceID[x] inside the block but once you leave the block you'll find that DeviceID[x] is empty.

If you replace Echo %DeviceID[x]% with set DeviceID[x] you can test the current value and find that DeviceID[x] is empty.

But what really happens is
Set "DeviceID[x]=%x%%x%"
Set "DeviceID[x]="

You've been mislead by the fact that you are using immediate expansion inside a block. So you'll get the unchanged value of DeviceID[x] inside the block but once you leave the block you'll find that DeviceID[x] is empty.

If you replace Echo %DeviceID[x]% with set DeviceID[x] you can test the current value and find that DeviceID[x] is empty.

What this script suppose to do: Return all connected USB drives, so that I could use Gdisk.exe to manipulate, create partitions and so on.
Gdisk.exe part is not written yet as I'm stuck with iteration as mentioned on the subject of this thread.

The requirements to run the code are simple: The WMI command-line (WMIC) utility provides a command-line interface for WMI.
WMIC is enabled by default on Windows 7, Windows 8, Windows 8.1, Windows 10.
The only exceptions: Windows XP and below or hacker edited versions.

Everything else is the same and could be ran even on the early Windows xp.

@echo off
setlocal
:: Initialize driveCount to 0 in case no drives found
set "driveCount=0" REM Initialize to 0 in case no drives found
:: Get the array of USB drives
for /f "delims=" %%L in (
'wmic logicalDisk where "DriveType=2" get DeviceID^, VolumeName 2^>nul ^| findstr "^.:" ^| findstr /n "^"'
) do for /f "tokens=1,2* delims=: " %%A in ("%%L") do (
set "driveCount=%%A"
set "drive[%%A]=%%B:"
set "name[%%A]=%%C"
)
:: List the results
setlocal enableDelayedExpansion
if %driveCount% equ 0 echo No USB drives found.
for /l %%N in (1 1 %driveCount%) do echo !drive[%%N]! = !name[%%N]!
exit /b

If you wanted to add back the nonsensical ProviderName test, then the where clause would be WHERE "DriveType=2 and ProviderName>0"
WMIC uses SQL syntax for the WHERE clause. Enclosing the entire clause in double quotes avoids a number of escape issues. String values should be enclosed within single quotes when used in a WHERE clause.
Dave Benham

floppydisks are also drivetype 2 so you are really searching for any removableDrives.

I'm searching only for USB Flash devices, and my own portable HDD is not getting considered as drive type 2. Kind of Strange.
That would be great to come up with general easily extendable scripts such as in this thread and upload into Github/Gitlab for others to look up and use what looks interesting.

For a second i did though about writting everything in Java instead, as batch has no short concise guidelines on precisely understanding how to write code without getting into troubles.
Also, there is no way known way to easily copy files with progress bar without utility or hardcore batching. I need to copy files after the USB is found but xcopy, copy, robocopy do not have overall progression bar to show what percentage has been completed, which kind of annoying to not know how long it might take for copying task to be completed

For a second i did though about writting everything in Java instead, as batch has no short concise guidelines on precisely understanding how to write code without getting into troubles.

Java is really not designed for system programming, you will need additional libraries and it is one of the most complex languages in existence. VBScript on the other hand is designed for system programming, easy to learn, no batch like quirks, object oriented, very fast compared to batch and easy to learn.

I am noting this down, I didn't know you could do associations through batch, great
Do you know of an online microsoft document that explains in detail all these wmic sql dialect options ?
These things are not found in wmic help unfortunately.

For a second i did though about writting everything in Java instead, as batch has no short concise guidelines on precisely understanding how to write code without getting into troubles.

Java is really not designed for system programming, you will need additional libraries and it is one of the most complex languages in existence. VBScript on the other hand is designed for system programming, easy to learn, no batch like quirks, object oriented, very fast compared to batch and easy to learn.

This is what I've found using google by searching around google:

VBScript doesn't have a progress bar, and it would be difficult to multithread so that you could be copying a file and simultaneously looking at how much had been copied. I wonder if there's a way to invoke the "flying document" bar from Windows Explorer. It doesn't show progress, but at least it indicates that something is happening. Hmmm...

you will need additional libraries and it is one of the most complex languages in existence.

Basics are pretty strightforward, might take few try hard months to become at least basic level software architect.
And Java seems to give as much complexity as you want, or as needed. The only hate so far - it needs runtime to be installed which is at least 20-200mb
Although it might run on linux and apple computers, with little tweaks, I guess.

Anyways I might use wmic even in Java, as it seems to be trustworthy native tool for Windows.