PowerShell 和 ADB:如何查找手机上文件的路径

问题描述 投票:0回答:2

我有一个使用 PowerShell 的学校项目,我决定创建一个脚本来将手机(目前仅限 Android)中的音乐、图片和视频备份到通过 USB 连接的 PC。为此,我实际上使用的是 ADB,它将自动下载(我希望这是可能的)。

目前我有:

foreach ($file in .\adb.exe shell ls -R | Where-Object {$_ -match ".mp3" -or $_ -match ".flac" -or $_ -match ".wav"})
{
    echo $file
   .\adb.exe pull -p $cheminFile C:\Users\admin\Desktop\test
}

现在我需要的是手机上文件的路径,然后我就可以使用以下方法复制它们:

.\adb.exe pull -p $pathFile C:\Users\admin\Desktop\test

如何获取路径?如果您有任何建议,我很乐意采纳!

谢谢!

android powershell path adb
2个回答
0
投票

这是适合我的代码

<#
    This powershell script pulls from a USB cable connected android device files to a windows folder.
    If the folder has files in subfolders they will also be copied to a similar subfolder to under the target folder
    The script requires the Android Debug Bridge (adb) available on the windows computer (no installation required)

    Adb Overview
    https://developer.android.com/studio/command-line/adb.html

    Get ADB files
    Download: SDK Platform Tools Release
    https://developer.android.com/studio/releases/platform-tools.html#download

    ADB Driver Download  
    I required that driver for the Samsung S7 attached to Windows 7 -> Windows 10 had the driver by default
    https://www.androidpit.de/adb-treiber-android-windows#windowstreiber
    http://adbdriver.com/downloads/

    activate on the android device within the developer options "USB debugging"

    connect the adroid device via USB 

    change the USB connection type to PTP

    check within your android device by the property of a file the path to use

#>

$VerbosePreference = "SilentlyContinue"
$ADBExe = "D:\Android-platform-tools\adb.exe"

cls
. $ADBExe devices

function FN-PullAllFilesFromAnAndroidFolder(){
    param 
        ( [Parameter(mandatory=$true)] [String] $AndroidSourceFolder,
          [Parameter(mandatory=$true)] [String] $WindowsTargetFolder   )

    if ($True){

        $FilesInAndroidFolder = . $ADBExe shell find "'$AndroidSourceFolder'" -type f
        $FilesInAndroidFolder  | Write-Verbose

        if ($True) 
        {
            ForEach ($SingleFileInAndroidFolder in $FilesInAndroidFolder)
            {
                #Replace known characters that are not deliverd correctly by the "adb shell find" query to windows 
                $FileFullPathCodePageFixed = $SingleFileInAndroidFolder.Replace('ä', "ä")
                $FileFullPathCodePageFixed = $FileFullPathCodePageFixed.Replace('Ä', "Ä") 
                $FileFullPathCodePageFixed = $FileFullPathCodePageFixed.Replace('Ö', "Ö") 
                $FileFullPathCodePageFixed = $FileFullPathCodePageFixed.Replace('Ãœ', "Ü") 
                $FileFullPathCodePageFixed = $FileFullPathCodePageFixed.Replace('ö', "ö") 
                $FileFullPathCodePageFixed = $FileFullPathCodePageFixed.Replace('ü', "ü")

                $RelativeFullPath = $FileFullPathCodePageFixed.Substring($AndroidSourceFolder.Length - 1, (($FileFullPathCodePageFixed.Length) - $AndroidSourceFolder.Length) + 1)

                if (!(Test-Path -LiteralPath (Join-Path -Path $WindowsTargetFolder -ChildPath $RelativeFullPath)))
                {
                    #Check if folder to copy to exists; if not create it
                    $TargetDirectoryWindows = Join-Path -Path $WindowsTargetFolder -ChildPath (Split-Path -LiteralPath $RelativeFullPath)
                    If (! (Test-Path -LiteralPath $TargetDirectoryWindows) ){New-Item -Path $TargetDirectoryWindows -ItemType Directory | Out-Null}

                    #Pull files from android device
                    . $ADBExe pull $FileFullPathCodePageFixed $(Join-Path -Path $WindowsTargetFolder -ChildPath $RelativeFullPath) 

                } Else
                {
                    "Already Backuped: $FileFullPathCodePageFixed"
                }
            }
        }
    }
}


FN-PullAllFilesFromAnAndroidFolder  -AndroidSourceFolder "/storage/3333-3432/DCIM/CAMERA/" `
                                    -WindowsTargetFolder "D:\Data\Pictures\SamsungS7\"

0
投票

谢谢你,我也有同样的问题。对于上面的代码,您是否将 $AndroidSource 文件夹替换为路径?同样,将 $WindowsTargetFolder 替换为路径?最后,如何将 Google Pixel 4a 中的短信复制到 Windows PC 中?有没有办法测试这个?尽管我备份了我的手机(或者我认为是这样),但数据非常敏感,我真的想确保该命令不会删除任何内容。预先感谢您!

© www.soinside.com 2019 - 2024. All rights reserved.