[Android General] larswaechter:
Write a batch script with ADB and Fastboot commands​

1. Introduction:

Hi guys,

First of all, I've some words to say:

I've decided to make a tutorial for beginners how to write a windows batch file with ADB and Fastboot commands. I think it's quite easy and everybody can learn it.

My target is, that all of you can write your own batch files to control your device.

You don't need any knowledges about batch files or similar things. It's just helpful to know the adb commands and how they work.

I hope you'll understand every step, I make. The guide is splitted in several parts. :)

If you want, I'll add some more tips and tricks. :)

2. Requirements:

Windows Computer

Notepad++

ADB Tools

Just search on the internet for those files. They are easy to find.

Create a folder and place your adb/fastboot files in it. Make sure that they are working

Install Notepad++ on any place on your computer

Open Notepad++ and create a new file

Go to Languages -> Batch and activate it

Save the file as batch file in your adb folder

Well, I think now we can start with the main commands. :)

3. Main batch commands:

In this section, I'll explain you important main commands for your batch file.

Don't enter the quotationmarks in your file.

"@echo off" - Is always at the beginning of your file. This command blocks the output of your command in the cmd

"echo TEXT" - Here you enter "echo" and your text at the beginning of the line. This text appears in the cmd. E.g. : "echo Hello this is my first file"

"echo." - Will create a blank line

"pause" - Will create a pause in your file. The file continues by pressing any key. A message for that appears in the cmd

"pause>nul" - Has the same function like "pause" but you won't get the output in the cmd, that you need to press any key to continue

":TEXT" - Creates a certain section in your script

"goto TEXT" - This command brings you to the section "TEXT" in your code. "TEXT" can e.g. include any certain command, you want to execute

Declaration of variables:

Now I'll show you, how to declare a variable

"set test= Hello XDA-Members"

The variable "test" includes the string "Hello XDA-Members"

To access this variable, you need to enter %test%

"set /p input= Enter your variable:"

Here, the user needs to enter something in the cmd. This input will be saved in the variable "input".

To access this variable, you need to enter %input%

You can select the variable name as you want.

Now we have already some commands, so let's try them out.

In my example, I'll add comments to the commands, which are written in blue. Please don't add them to yours.

The second "@echo off" is not necessary

So, now we save the file and run it with a double-click.

Your script should look like this:

(Not everything is in mine included)

Making a selection menu:

Now, we make a selection meu where the user has several options

Step by step:

Create a list with "echo" commands

Create an input variable

Create an if statement

Let's take a look at my example code.

You should already understand the old commands. ;)

As you can see, this is still quite simple

If everything worked right, you should see this

Well, for the beginning that's enough. For the next step, you should know all these commands and how they work. :)

4. Including ADB/Fastboot commands:

Okay, now we'll start to add ADB and Fastboot commands to our batch file for controling our device with our tool.

The previous steps are the base for the following commands

First of all, we start with a quite easy command.

We want a batch file, which reboot our phone into Recovery,Bootloader or normal.

Now we need the adb files in our folder

Step by step:

Create a selection menu

Include the ADB/Fastboot commands

Let's take a look at my example code.

Our Reboot-Tool should look like this

These commands are still easy. You just need to enter your adb command

E.g.: adb.exe reboot recovery

Now we'll execute our file

You can see, that this tool is already very useful.

So, Congratulation to everybody who came so far

Are you ready for the next step? Let's go!

5. Including advanced ADB/Fastboot commands:

Okay, now we will add some more difficult commands to our script.

You need to know the old commands and how the work

Step by step:

Reboot device in different modes

Flash a custom recovery

Do a Factory Reset

Install Apps

Ok, let's take again a look at our example code

I won't explain the old commands, only the new ones.

Take your time and read it slowly.

The script is splitted in two pictures, so read both.

As you can see, we have now some more difficult codes, which include flashing Recoverys with certain file names. :)

Again, Congratulation who came so far.

6. Additional Commands:

- SOON -

7. Hints:

Now we are almost done with our first batch script.

At the end I have some more useful hints for you

Have always a clear structure

Take your time, it may not work for the first time

Read some other Batch-Guides on the internet

Just discover the batch coding and learn new commands, you can add to your script.

There are a lot of posibilities.

You can download the last script and the pictures from my dropbox:

https://www.dropbox.com/sh/jfogzlnj4df90uf/AABwRajM0piQHi5MnuEmzON5a?dl=0

Moreover check out my Universal_ADB-Helper, which is based on this:

http://xdaforums.com/android/software/utility-universaladb-helper-1-0-t2969165

8. Last words:

Now we are done with our batch script. I hope you enjoyed my tutorial and that it was easy to understand for beginners.

Moreover I want to say thank you at RootJunky who inspired me to start with batch scripting.

Check out this video from him:

https://www.youtube.com/watch?v=8q3y9zkEeHM

and his website:

http://www.rootjunky.com/