Home > other >  fastboot flash %%i in (*.img) batch file
fastboot flash %%i in (*.img) batch file

Time:10-24

im looking for the right way to flash multiple *.img files in the same folder without duplicating "fastboot flash xxxxx xxxxx.img" command on all lines I want the output to be like this

flash boot... OKAY
flash recovery... OKAY
flash fastboot... OKAY
flash fastboot... FAILED

I was using

for %%i in (*.img) do fastboot flash %%i %%i do @echo flash %%i ...OKAY

but the output is not what i want

Start Flashing ....
target reported max download size of 805306368 bytes
sending 'boot.img' (131072 KB)...
OKAY [  3.751s]
target reported max download size of 805306368 bytes
sending 'boot_b.img' (131072 KB)...
OKAY [  3.755s]
writing 'boot_b.img'...
FAILED (remote: (boot_b.img_a) No such partition) ::: fail because partition name contain file extension

target reported max download size of 805306368 bytes
sending 'frp.img' (512 KB)...
OKAY [  0.023s]
writing 'frp.img'...
FAILED (remote: (frp.img_a) No such partition)

target reported max download size of 805306368 bytes
sending 'modem.img' (262144 KB)...
OKAY [  7.520s]
writing 'modem.img'...
FAILED (remote: (modem.img_a) No such partition)

it shows FAILED No such partition in output Because the partition name contain the file extension and It should be "fastboot flash filename filename.img" for works correctly

for %%i in (*.img) do fastboot flash %%i %%i do @echo flash %%i ...ok

any help or suggestion to fix command ?

CodePudding user response:

for %%i in (*.img) do SET "writing="&for /f "tokens=1,2delims=' %%u in ('fastboot flash "%%~ni" "%%i"') do (
 IF DEFINED writing FOR /f %%y IN ("%%u") DO CALL ECHO %%writing%% %%%%y&SET "writing="
 IF "%%u"=="writing " SET "writing=%%~nv"
)

should do what I believe you want.

%%i gets the name of each file assigned to it in turn.

Having set writing to nothing (which makes writing undefined) the fastboot utility is then executed with the parameters flash, the name part of the filename and the full filename. fastboot's output is then "tokenised" using ' as a delimiter, so any line is split - any part before a ' is assigned to %%u and the part before the next ' is assigned to %%v.

writing starts undefined. If we find the word writing (importantly including the space) in %%u then we set writing to the part between the first and second 's in %%v; treated as a filename (which it is) and removing the extension (%%~nv).

The next line processed will be the "FAILED/OKAY" report of which we want the first word, so we take %%u and use the default "token=1delims=, " to grab just that word to %%y; then use call echo to echo the required data in a subshell, which reparses %%writing%% %%%%y to %writing% %%y.

No guarantees since I have to simulate fastboot's output for testing.

See for /? from the prompt for documentation about tokenising - or choose from thousands of examples on SO

CodePudding user response:

Solved using

for %%i in (*.img) do fastboot flash %%~ni %%i  >nul 2>&1 && (
      echo - Writing %%~ni ... OKAY
     ) || (
     echo - Writing %%~ni ... FAILED
)

the output

- Writing boot_a ... OKAY
- Writing boot_b ... OKAY
- Writing dip ... OKAY
- Writing dsp_a ... OKAY
- Writing dtbo_a ... OKAY
- Writing dtbo_b ... OKAY
- Writing ffu ... OKAY
- Writing frp ... OKAY
- Writing fsc ... FAILED
- Writing fsg ... FAILED
- Writing imagefv_a ... OKAY
- Writing imagefv_b ... OKAY
- Writing mdtp_a ... OKAY
- Writing mdtp_b ... OKAY
  • Related