Batch file commands and using command prompt to accomplish this - android

Couple of questions, with all of them regarding batch code.
So my first question is that if I have a batch file, and it has commands in it, how would I do something like this in command prompt?
cd C:\User\username\Desktop\folder\
And in folder, there is a file called CMDTEST.bat. With this, I would want to use commands from there. In this file, it starts off by giving me a few choices. There are three, which are selected by typing i, for input, o, for output, a for ADB connection, and q for quit. My question is, how would I do something like this?
C:\Users\username\Desktop\folder\CMDTEST.bat -i
The goal of typing something like this, is it would directly go to the CMDTEST.bat file, and then execute the choice if 'i' was put in. Similarly, there would something for 'o', something for 'a', and something for 'q'. How would I go about accomplishing this?
My second question is how do I get the directory of a connected USB device? In my case, I have to get the directory of a connected tablet, which has files on it. I need to make all this run pretty quickly, so I need the directories with a simple command, like if I did the same thing as the above code block, but something like:
C:\Users\username\Desktop\folder\CMDTEST.bat -d
That would list all the usb devices connected, with their name as shown in Windows Explorer, and the path used to access the device. All of the devices would be android tablets, so theres no need for a root scheme or anything to that extent.
The third and final question I have is how would I check in an if statement, whether things were printed in a cmd Android ADB statement where I write:
C:\Users\username\Desktop\platform-tools(this is where the Android SDK is stored)
and then I would write
adb devices
which would print all available devices connected(all android devices). How would I check if things are printed from that, and if they ARE NOT, then continue to search for them in a loop, until at least one device is found, then when it is found, break out of the loop?

First question:
So my first question is that if I have a batch file, and it has commands in it, how would I do something like this in command prompt?
cd C:\User\username\Desktop\folder\
Use cd /d to change current drive as well as current directory. Combine that with the %USERPROFILE% environment variable to dynamically get to the currently logged in user's folder. You should end up with something like this:
cd /d %userprofile%\desktop
Second question:
My question is, how would I do something like this?
C:\Users\username\Desktop\folder\CMDTEST.bat -i
The goal of typing something like this, is it would directly go to the CMDTEST.bat file, and then execute the choice if 'i' was put in. Similarly, there would something for 'o', something for 'a', and something for 'q'. How would I go about accomplishing this?
Batch scripts recognize parameters as %1, %2, and so on. You could do something like this in your script:
if %1==a (
goto optionA
) else if %1 ==o (
goto optionO
) else if %1 ==q (
goto optionQ
) else (
goto end
)
:optionA
{do option A suff}
:optionO
{do option O suff}
:optionQ
{do option Q suff}
:end
echo No valid option was provided
Second Second question:
Next Question:
My second [next] question is how do I get the directory of a connected USB device?
I don't have the answer for you, but you might look at this other question on SO: Find USB Drive letter
Third and Final question:
The third and final question I have is how would I check in an if statement, whether things were printed in a cmd Android ADB statement. How would I check if things are printed from that, and if they ARE NOT, then continue to search for them in a loop, until at least one device is found, then when it is found, break out of the loop?
I'm not familiar with the function of abd devices and what or how it outputs. However, assuming it outputs to the screen you could probably pipe (|) the output to a FIND command or something to make your determination. As stated in the comments, you might have better luck trimming this out and asking it as a separate question.

Related

How to save shell command for next time use (Terminal of Android Studio)

Suppose I run this command in Android Studio terminal
D:\.android\sdk\platform-tools/adb shell input text 'Some Text'
How do I save it (as macro) for next time use After closing and opening Android Studio instead of retyping the full command?
I know in the current session we can use the UP/DOWN arrows, I am looking at something like macros to be saved and be permanent...
I also faced this problem and reduce my 80% time and effort to run shell command by creating .bat files with just one letter like d.bat etc (single letter for reducing keyboard interaction to lessen time).
If you have no idea about What is bat file? and How to deal with it? please check this answer.

pull only new files (photos) from Android adb to linux os

I've been pulling photos from my android device to my linux OS like this:
$ adb pull <what-to-pull> <where-to-place>
In the future I would prefer to pull only the ones I don't alreay have.
What's the best way to do this?
Maybe I could put all the photos I've downloaded to the same folder and skip the ones with names that already exist in the folder I'm pulling from? How to do that?
Is that even the best way? Does an easier way to do this exist?
If so... how?
I'm on arch linux by the way, in case the distribution effects your suggested answer.
adb shell find "/sdcard/DCIM" -iname "*.jpg" | tr -d '\015' | while read line; do adb pull $line; done;
^that works well enough.
From here.
The adb-sync tool worked for me: https://github.com/google/adb-sync
Note that I had to make several changes to the source code to get it working for my use-case (invalid paths for Windows causing crash, Python version mismatch apparently, etc -- for details, see issues I commented in), but it ended up being the only way I was able to retrieve my files from a corrupted data partition.
(The adb pull of the whole directory would crash on various files, and I didn't want to manually have to delete each one then restart the whole transfer. With adb-sync [+my modifications] it would just fail that one file then continue.)
Regarding your question of having it only transfer new files, I believe adb-sync does that automatically if a file hasn't been changed. If you don't want it to re-transfer an existent file ever (ie. even if the file has been updated), I think that's what the flag mentioned here is for: https://github.com/google/adb-sync/issues/22

How to change ADB Device Names during build process

I am currently working with multiple android builds on different hardware. I am having an issue where they all have the same serial number, 0123456789ABCDEF. This makes it impossible to use adb when I connected to two or more devices at once, because the adb doesn't know which one to talk to.
I know that the name is being pulled from /sys/class/android_usb/android0/iSerial and if I wanted to I could change it there once the build is complete. Ideally though, I want that file to be set during the build depending on the build settings. I want to know where that file is being generated during the build. I believe it's being set either somewhere in barebox or in /system/core/adb, but have had no luck on the things I've tried editing.
If anyone has ran into this, any help would be greatly appreciated.
Edit:
Found the solution.
This can be found in /device/company_name/device_name/init.device_name.usb.rc
on boot
write /sys/class/android_usb/android0/iManufacturer ${ro.product.manufacturer}
write /sys/class/android_usb/android0/iProduct ${ro.product.model}
write /sys/class/android_usb/android0/iSerial ${ro.serialno}
echo "ro.serialno is ${ro.serialno}"
write /sys/class/android_usb/android0/idVendor 0451
write /sys/class/android_usb/android0/idProduct D101
..."
Change the ro.serialno to whatever you'd like.

Push files from Android to PC over ADB

It's probably not possible due to safety issues and many other reasons, but it's worth a shot, so here goes:
Is it possible to push files from an Android device directly to a computer using ADB?
Why would you want that, you might ask. Good question. I find it useful to view larger Strings on a computer instead of on an Android device, especially since Log.d() won't show Strings of a length more of a couple hundred characters. Things like SOAP requests and responses, other xml files are not easily viewable on my Nexus 7. I've tried some things with the UsbManager class and the UsbDevice class, but I can't seem to find the USB-connection to my computer.
PS. I can think of other methods, like using a logging webservice, for all I care, or writing a script which pulls a certain (log) directory periodically, but I'm just curious whether or not it is possible, it makes my life ever so slightly easier.
As I can read in your question, you are quite aware of the fact that you can pull files from your Android device to your PC, so I won't suggest that.
To answer your question: No, this is not possible. It's not how adb works. Even if you could "push" from Android to PC, you need a piece of software to handle the data. Android does not contain any API which makes that possible, and neither does any part of the Android SDK.
Still you could use any of the methods you already know of (adb pull, Eclipse DDMS View, and yes, even a logging webservice, as you yourself suggested).
Hope this clarifies a bit.
You can push files from ADB to PC (eclipse).
In Eclipse Window-Open Perspective-DDMS
and then in DDMS view select your device from the left side list.
and in the Right side view, you will find a folder called mnt, inside it you will find sd card. There are your files. your devices files. Now to get them out to your pc
There are two buttons on the right side top.
one button says pull a file from device
another button says push a file to device
You need pull the file from device.
Select your respective file and click the pull a file from device button.
To copy a file or directory (and its sub-directories) from the emulator or device, use
adb pull <remote> <local>
For more details on the usage, refer this link
EDIT: What I understand is that you want your app to pull a particular file right? If yes, you need to use
public class YourAppCode {
public static void main(String[] args) throws Exception {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(new String[] {"/usr/bin/adb", "devices");
}
}
Instead of devices, you need to send pull command along with the source and destination.

Changing some system file values in Android failing

I am trying to change some sys values but I don't seem to be having much success.
In my case I am trying to change values of files in the folder
"/sys/devices/platform/omap/musb-omap2430/musb-hdrc/usb1/1-0:1.0"
e.g. the file bInterfaceClass which currently has value 09
My tries:
(In shell, as root)
chmod 777 bInterfaceClass
echo 07 >> bInterfaceClass
I didn't receive an error but when looking up the value
cat bInterfaceClass
It is still 09
Now looking up this file in Root Explorer, I can see that the last modification date of the file has changed, so my guess: something resets the value of such a sys file as soon as it changes. Can anyone shine some more light on this issue? How can I change the value?
Many thanks!
THIS IS HACKERY, you have been warned! :)
Instructions here are not generally found on the Internet, but can be great for testing interfaces and capabilities without significantly changing system code. THESE CAN BE USED TO ADDRESS ANYTHING WHICH IS BEING OVERWRITTEN without warning or cause. Using these, you can sometimes see based off of using dmesg ps and logcatwhat exactly is causing you so many problems, while testing a solution.
The is likely in the Kernel with things like this getting written over, maybe a system service or script internally. A quality perm fix would be in the /drivers folder of the kernel. I can only assume this is on a Beagle or Panda Board, maybe a Moto device. If it is Beagle or Panda, this will be easier (yay Linaro, AOSP support, big community!).
If this is something that does not need to Hold USB open, but merely have the desired number present you can try below:
Open up your boot.img and open he Root Disk/Ramdisk and finally one of your init..rc files. You can use this tool: https://github.com/dsixda/Android-Kitchen - requires Linux and a few packages, great tool!
If you are lucky, this will appear as part of the init.rc files (which you can check in-system) or in the /system/etc folder as one of the class main or core scripts.
You can declare the value you want if you look for it in the:
on init
Section of the init.platform.rc and look where
/sys/devices/platform/omap/musb-omap2430/musb-hdrc/usb1/1-0:1.0
is initialized,
then in the .rc file
chmod 777 /sys/devices/platform/omap/musb-omap2430/musb-hdrc/usb1/1-0:1.0/bInterfaceClass
write /sys/devices/platform/omap/musb-omap2430/musb-hdrc/usb1/1-0:1.0/bInterfaceClass 07
Then if doing that and initializing it as such does not hold by that alone, open the normal init.rc and add
on nonencrypted
write /sys/devices/platform/omap/musb-omap2430/musb-hdrc/usb1/1-0:1.0/bInterfaceClass 07
and also
on property:vold.decrypt=trigger_shutdown_framework
write /sys/devices/platform/omap/musb-omap2430/musb-hdrc/usb1/1-0:1.0/bInterfaceClass 07
as those two properties or functions will cover you at the end of the inits to set that property again (you already gave it 777 privilages earlier as part of the on init)
If you want something you can play with without flashing new Boot.img files:
Declare your script in the system/bin as a service in the init.platform.rc (don't worry most every .rc file is linked and includes each other) using:
service usbchanger /system/bin/sh /system/bin/usbchanger.sh
class late_start
user root
disabled
Then in the normal init.rc
on nonencrypted
start usbchanger
on property:vold.decrypt=trigger_shutdown_framework
start usbchanger
Your script will then become a constantly running service (you can do the same with a binary). This is totally a desired trait when doing debugging and testing new features/fixes because you can change the values and running commands while the system is open and does not require you to re-flash after every change. However, for production you should not have this going. Its bad code to do that generally when really, it should be in the kernel or core.

Categories

Resources