2

By using context menu in Windows I would like to wirelessly push selected file(s) to Android device using ADB.

I want to connect to the device, push selected file(s) and disconnect each time, so I crated this simple batch file:

@ECHO OFF
D:\adb.exe connect 192.168.0.103:5555
D:\adb.exe push "%%~f1" /sdcard/
D:\adb.exe disconnect

The command in context menu points to my batch file:

[HKEY_CLASSES_ROOT\*\shell\ADB_Push_to_Phone]
[HKEY_CLASSES_ROOT\*\shell\ADB_Push_to_Phone\command]
@="D:\\Tools\\CMD\\ADB_PUSH_TO_PHONE.cmd"

Running this as intended (right click on file -> ADB_Push_to_Phone) gives this error at the push command:

adb: error: cannot stat '%~f1': No such file or directory

How do I expand properly the selected file(s) full path inside this command? Or is it a different issue here with this approach?

Rayearth
  • 355
  • 3
  • 6
  • 16

3 Answers3

1

If I take a look at a file association for the GIMP image editor, the command line is as follows:

"C:\Program Files\GIMP 2\bin\gimp-2.10.exe" "%1"

You don’t have the "%1" part in your command line. If you add it, this should work. The complete command could look like this:

D:\Tools\CMD\ADB_PUSH_TO_PHONE.cmd "%1"

(Note that I am not using any escaping that would be required in .reg files.)

To diagnose things, I recommend debug prints in your batch file and possibly using the pause or timeout commands to have time to look at them.

user219095
  • 65,551
1

The solution was to set context menu command to:

"D:\Tools\CMD\ADB_PUSH_TO_PHONE.cmd" "%1"

And to modify push command in batch file:

D:\adb.exe push %1 /sdcard/
Rayearth
  • 355
  • 3
  • 6
  • 16
1

adb: error: cannot stat '%~f1': No such file or directory

There is no expansion for %1 in %~f1 for any registry arguments:

Arguments:
%1, %D, %H, %I, %L, %S, %V, %W
Not Work/Expand:
%~nx1, %~nxD, %~nxH, %~nxI, %~nxL, %~nxS, %~nxV, %~nxW
  Often we find "%1" in the commands associated with file types
(HKEY_CLASSES_ROOT\FileType\shell\open\command).
In Windows 9x through 2000 (and possibly XP) this would evaluate
to the short notation of the fully qualified path of the file of type FileType.
To get the long file name, use "%L" instead of "%1".

When using right click on file -> ADB_Push_to_Phone, the selected file has to be passed to your .bat, it doesn't come automatically, it is first "passed" from the register to .bat as an argument.

You are already treating the path, passing it as complete by right click to windows registry in "%V", so you can use "%~1", since the argument received in your bat file.


@echo off

cd /d "%~d0" adb.exe connect 192.168.0.103:5555 adb.exe push "%~1" /sdcard/ adb.exe disconnect


In the Windows registry (or .reg file) use:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT*\shell\ADB_Push_to_Phone] [HKEY_CLASSES_ROOT*\shell\ADB_Push_to_Phone\command] @="C:\Windows\System32\cmd.exe" /q /c "D:\Tools\CMD\ADB_PUSH_TO_PHONE.cmd" "%L""

Your bat can go straight to where the executable is, without the need to edit the path for each drive/volume change, use cd /d "drive_where_your_bat_is":

@echo off

cd /d "%~d0"

adb.exe connect 192.168.0.103:5555 adb.exe push "%~1" /sdcard/ adb.exe disconnect


  • Or use drive_where_your_bat_is: + \adb.exe
@echo off

%~d0\adb.exe connect 192.168.0.103:5555 %~d0\adb.exe push "%~1" /sdcard/ %~d0\adb.exe disconnect

Io-oI
  • 9,237