批处理自动化adb push时目标路径变量为空及特殊路径处理问题
Fixing Your Android File Transfer Batch Script
Hey there! Let's get your batch script working properly—we'll tackle the variable typo first, then handle those tricky paths with spaces, single quotes, and shared folders.
The Core Issues in Your Original Script
- Variable Name Typo: You wrote
%loaction%instead of%location%—that's exactly why your target path showed up empty! - Incorrect Quoting: Using single quotes (
') around paths doesn’t play nice with Windows batch or theadbcommand, especially when dealing with spaces, single quotes in filenames, or network shared paths.
Corrected Batch Script
Here's the fixed version that addresses all these problems:
@echo off setlocal enabledelayedexpansion set /p filename="Enter the full path and name of the file to transfer: > " set /p location="Enter the target path on your Android device (e.g., /sdcard/Documents/): > " :: Optional: Preview the command before execution echo Preview: adb push "%filename%" "%location%" :: Execute the actual adb push command adb push "%filename%" "%location%" echo Transfer attempt finished! Check above for adb feedback. pause
Key Improvements Explained
- Fixed Variable Typo: We corrected
loactiontolocationso the target path variable is properly referenced. - Double Quotes for Paths: Windows batch and
adbboth recognize double quotes (") as the standard way to wrap paths with spaces, special characters (like'inthat's right.txt), or network shared paths (e.g.,\\mysharedfolder\text files\). This ensures the entire path is treated as a single value. setlocal enabledelayedexpansion: This helps avoid parsing issues with any special characters in user-input paths, making the script more robust.- Optional Preview Line: Added a preview of the command so you can verify it’s correct before running the actual transfer.
Quick Usage Tips
- Make sure
adbis added to your system’s PATH, or save this script in the same folder as youradb.exefile. - For Android target paths, use forward slashes (
/) instead of backslashes (e.g.,/sdcard/Downloads/—adb will handle this automatically, but it’s good practice). - Ensure your Android device has USB debugging enabled and is connected to your computer before running the script.
内容的提问来源于stack exchange,提问作者user3906610
相关产品推荐
相关产品推荐

