queensoft

Portable device (Android phone) - copy files

3 posts in this topic

#1 ·  Posted (edited)

Connect Android phone > enable USB file transfer > go to My Computer, there's an icon, but no drive letter - this is normal.

Open phone icon > go to Internal Storage > go to DCIM > copy all files and folders to computer.

Now, I want a quick AutoIt script / windows batch to do this automatically.

Get USB path with this - it works OK:

#include <WinAPICom.au3>

Global Const $sCLSID_PortableDeviceManager = "{0af10cec-2ecd-4b92-9581-34f6ae0637f3}"
Global Const $sIID_IPortableDeviceManager = "{a1567595-4c2f-4574-a6fa-ecef917b9a40}"
Global Const $sTagIPortableDeviceManager = "GetDevices hresult(ptr;dword*); RefreshDeviceList hresult(); " & _
        "GetDeviceFriendlyName hresult(wstr;wstr;dword*); GetDeviceDescription hresult(wstr;wstr;dword*); " & _
        "GetDeviceManufacturer hresult(wstr;wstr;dword*); GetDeviceManufacturer hresult(wstr;wstr;ptr;dword*dword*); " & _
        "GetPrivateDevices hresult(ptr;dword*)"

Global Enum $eDevID, $eDevName, $eDevManufacturer, $eDevDescription


Local $aPnPDevices = GetPortableDevices()
If IsArray($aPnPDevices) Then _ArrayDisplay($aPnPDevices)

;~ Success: Return 0
;~ Failure: Return 2DArray [n][4] |;[n][0]$eDevID, [n][1]$eDevName, [n][2]$eDevManufacturer,[n][3] $eDevDescription
Func GetPortableDevices()
    Local $aDevicesInfo[0][0] ;[n][0]$eDevID, [n][1]$eDevName, [n][2]$eDevManufacturer,[n][3] $eDevDescription
    Local $oPortableDeviceManager = 0
    Local $SizeofArray = 0
    Local $hr = 0x80004005 ;E_Fail Just to Initialized <0
    Local $taPnPDeviceIDs = 0
    Local $tName = 0
    $oPortableDeviceManager = ObjCreateInterface($sCLSID_PortableDeviceManager, $sIID_IPortableDeviceManager, $sTagIPortableDeviceManager)
    If Not IsObj($oPortableDeviceManager) Then Return 0

    If FAILED($oPortableDeviceManager.GetDevices(Null, $SizeofArray)) Then Return 0
    If $SizeofArray < 1 Then Return 0
    $taPnPDeviceIDs = DllStructCreate("ptr[" & $SizeofArray & "]")
    If FAILED($oPortableDeviceManager.GetDevices(DllStructGetPtr($taPnPDeviceIDs), $SizeofArray)) Then Return 0

    ReDim $aDevicesInfo[$SizeofArray][4]

    For $i = 0 To $SizeofArray - 1
        $tName = DllStructCreate("wchar[512]", DllStructGetData($taPnPDeviceIDs, 1, $i + 1))
        $aDevicesInfo[$i][$eDevID] = DllStructGetData($tName, 1)
        $aDevicesInfo[$i][$eDevName] = _GetFriendlyName($oPortableDeviceManager, $aDevicesInfo[$i][$eDevID])
        $aDevicesInfo[$i][$eDevManufacturer] = _GetDeviceManufacturer($oPortableDeviceManager, $aDevicesInfo[$i][$eDevID])
        $aDevicesInfo[$i][$eDevDescription] = _GetDeviceDescription($oPortableDeviceManager, $aDevicesInfo[$i][$eDevID])
        $tName = 0
        _WinAPI_CoTaskMemFree(DllStructGetData($taPnPDeviceIDs, 1, $i + 1))
    Next
    Return $aDevicesInfo

EndFunc   ;==>GetPortableDevices



Func _GetDeviceManufacturer($oInterface, $PnPDeviceID)
    Local $sString = ""
    $oInterface.GetDeviceManufacturer($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetDeviceManufacturer


Func _GetDeviceDescription($oInterface, $PnPDeviceID)
    Local $sString = ""
    Local Const $Size = 128
    $oInterface.GetDeviceDescription($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetDeviceDescription


Func _GetFriendlyName($oInterface, $PnPDeviceID)
    Local $sString = ""
    Local Const $Size = 128
    $oInterface.GetDeviceFriendlyName($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetFriendlyName

Func _GetProperty($oInterface, $PnPDeviceID)
    Local $sString = ""
    Local Const $Size = 128
    $oInterface.GetDeviceFriendlyName($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetProperty

Func FAILED($hr)
    Return ($hr < 0)
EndFunc   ;==>FAILED

Path looks like this:

\\?\usb#vid_0e8d&pid_201d&mi_00#7&37c4bb9&0&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}

I can open this in a Windows Explorer windows and it works ok.

Now, search for files using AutoIt - does not work:

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <File.au3>

Local $f


$f = _RecFileListToArray("\\?\usb#vid_0e8d&pid_201d&mi_00#7&37c4bb9&0&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}", "*.*", 0, 1, 1)
_ArrayDisplay($f)

; Name...........: _RecFileListToArray
; Description ...: Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax.........: _RecFileListToArray($sPath[, $sFilter = "*"[, $iFlag = 0[, $iRecur = 0[, $iFullPath = 0]]]])
; Parameters ....: $sPath   - Path to generate filelist for.
;                 $sFilter - Optional the filter to use, default is *. Search the Autoit3 helpfile for the word "WildCards" For details.
;                 $iFlag   - Optional: specifies whether to return files folders or both
;                 |$iFlag=0 (Default) Return both files and folders
;                 |$iFlag=1 Return files only
;                 |$iFlag=2 Return Folders only
;                 $iRecur  - Optional: specifies whether to search in subfolders
;                 |$iRecur=0 (Default) Do not search in subfolders
;                 |$iRecur=1 Search in subfolders
;                 $iFullPath  - Optional: specifies whether to include initial path in result string
;                 |$iFullPath=0 (Default) Do not include initial path
;                 |$iFullPath=1 Include initial path
; Return values .: @Error - 1 = Path not found or invalid
;                 |2 = Invalid $sFilter
;                 |3 = Invalid $iFlag
;                 |4 = Invalid $iRecur
;                 |5 = Invalid $iFullPath
;                 |6 = No File/Folder Found
; Author ........: SolidSnake <MetalGX91 at GMail dot com>
; Modified.......: 22 Jan 09 by Melba23 - added recursive search and full path options
; Remarks .......: The array returned is one-dimensional and is made up as follows:
;                               $array[0] = Number of Files\Folders returned
;                               $array[1] = 1st File\Folder
;                               $array[2] = 2nd File\Folder
;                               $array[3] = 3rd File\Folder
;                               $array[n] = nth File\Folder
; Related .......:
; Link ..........;
; Example .......; Yes
; ====================================================================================================

;Special Thanks to Helge and Layer for help with the $iFlag update
; speed optimization by code65536
;===============================================================================
Func _RecFileListToArray($sPath, $sFilter = "*", $iFlag = 0, $iRecur = 0, $iFullPath = 0)
    Local $asFileList[1], $sFullPath
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If StringRight($sPath, 1) <> "\" Then $sPath = $sPath & "\"
    If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
    If Not ($iRecur = 0 Or $iRecur = 1) Then Return SetError(4, 4, "")
    If $iFullPath = 0 Then
        $sFullPath = $sPath
    ElseIf $iFullPath = 1 Then
        $sFullPath = ""
    Else
        Return SetError(5, 5, "")
    EndIf
    _FLTA_Search($sPath, $sFilter, $iFlag, $iRecur, $sFullPath, $asFileList)
    If $asFileList[0] = 0 Then Return SetError(6, 6, "")
    Return $asFileList
EndFunc  ;==>_FileListToArray

; #INTERNAL_USE_ONLY#=================================================================================

; Name...........: _FLTA_Search
; Description ...: Searches folder for files and then recursively searches in subfolders
; Syntax.........: _FLTA_Search($sStartFolder, $sFilter, $iFlag, $iRecur, $sFullPath, ByRef $asFileList)
; Parameters ....: $sStartFolder - Value passed on from UBound($avArray)
;                 $sFilter - As set in _FileListToArray
;                 $iFlag - As set in _FileListToArray
;                 $iRecur - As set in _FileListToArray
;                 $sFullPath - $sPath as set in _FileListToArray
;                 $asFileList - Array containing found files/folders
; Return values .: None
; Author ........: Melba23 based on code from _FileListToArray by SolidSnake <MetalGX91 at GMail dot com>
; Modified.......:
; Remarks .......: This function is used internally by _FileListToArray.
; Related .......:
; Link ..........;
; Example .......;
; ====================================================================================================

Func _FLTA_Search($sStartFolder, $sFilter, $iFlag, $iRecur, $sFullPath, ByRef $asFileList)

    Local $hSearch, $sFile

    If StringRight($sStartFolder, 1) <> "\" Then $sStartFolder = $sStartFolder & "\"
; First look for filtered files/folders in folder
    $hSearch = FileFindFirstFile($sStartFolder & $sFilter)
    If $hSearch > 0 Then
        While 1
            $sFile = FileFindNextFile($hSearch)
            If @error Then ExitLoop
            Switch $iFlag
                Case 0; Both files and folders
                    If $iRecur And StringInStr(FileGetAttrib($sStartFolder & $sFile), "D") <> 0 Then ContinueLoop
                Case 1; Files Only
                    If StringInStr(FileGetAttrib($sStartFolder & $sFile), "D") <> 0 Then ContinueLoop
                Case 2; Folders only
                    If StringInStr(FileGetAttrib($sStartFolder & $sFile), "D") = 0 Then ContinueLoop
            EndSwitch
            If $iFlag = 1 And StringInStr(FileGetAttrib($sStartFolder & $sFile), "D") <> 0 Then ContinueLoop
            If $iFlag = 2 And StringInStr(FileGetAttrib($sStartFolder & $sFile), "D") = 0 Then ContinueLoop
            _FLTA_Add($asFileList, $sFullPath, $sStartFolder, $sFile)
        WEnd
        FileClose($hSearch)
        ReDim $asFileList[$asFileList[0] + 1]
    EndIf

    If $iRecur = 1 Then
    ; Now look for subfolders
        $hSearch = FileFindFirstFile($sStartFolder & "*.*")
        If $hSearch > 0 Then
            While 1
                $sFile = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If StringInStr(FileGetAttrib($sStartFolder & $sFile), "D") And ($sFile <> "." Or $sFile <> "..") Then
                ; If folders needed, add subfolder to array
                    If $iFlag <> 1 Then _FLTA_Add($asFileList, $sFullPath, $sStartFolder, $sFile)
                ; Recursive search of this subfolder
                    _FLTA_Search($sStartFolder & $sFile, $sFilter, $iFlag, $iRecur, $sFullPath, $asFileList)
                EndIf
            WEnd
            FileClose($hSearch)
        EndIf
    EndIf

EndFunc

; #INTERNAL_USE_ONLY#=================================================================================

; Name...........: _FLTA_Add
; Description ...: Searches folder for files and then recursively searches in subfolders
; Syntax.........: _FLTA_Add(ByRef $asFileList, $sFullPath, $sStartFolder, $sFile)
; Parameters ....: $asFileList - Array containing found files/folders
;                 $sFullPath - $sPath as set in _FileListToArray
;                 $sStartFolder - Value passed on from UBound($avArray)
;                 $sFile - Full path of file/folder to add to $asFileList
; Return values .: Function only changes $asFileList ByRef
; Author ........: Melba23 based on code from _FileListToArray by SolidSnake <MetalGX91 at GMail dot com>
; Modified.......:
; Remarks .......: This function is used internally by _FileListToArray.
; Related .......:
; Link ..........;
; Example .......;
; ====================================================================================================

Func _FLTA_Add(ByRef $asFileList, $sFullPath, $sStartFolder, $sFile)

    Local $sAddFolder

    $asFileList[0] += 1
    If UBound($asFileList) <= $asFileList[0] Then ReDim $asFileList[UBound($asFileList) * 2]
    If $sFullPath = "" Then
        $sAddFolder = $sStartFolder
    Else
        $sAddFolder = StringReplace($sStartFolder, $sFullPath, "")
    EndIf
    $asFileList[$asFileList[0]] = $sAddFolder & $sFile

EndFunc

File copy using Autoit - does not work:

FileCopy("\\?\usb#vid_0e8d&pid_201d&mi_00#7&37c4bb9&0&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\Internal storage\DCIM\Camera MX\PHOTO_20161007_123935.jpg", 'd:\Diverse 2\654\0\')

Copy using Windows batch - does not work:

xcopy /Y /E "\\?\usb#vid_0e8d&pid_201d&mi_00#7&37c4bb9&0&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\Internal storage\DCIM\*.*" "d:\Diverse 2\654\0\"

 

Edited by queensoft

Share this post


Link to post
Share on other sites



Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Can't this part (dll files - only for copy operation), be extracted from larger development kit and accessed with AutoIt ?

Or use any other file copy utility, like robocopy or similar ?

 

Found this, first step working - I get a list of drives (including android phone) and a list of files&folders: https://gist.github.com/cveld/8fa339306f8504095815

Now the hard part, not working now, very limited Powershell knowledge right now: change path, copy files....

Edited by queensoft

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • vindicta
      By vindicta
      Hello Everyone,
      Please pardon my noobiness. I am working on backup restore script where a progress bar is required. I have tried using following code which works fine with folders but not really with single file because robocopy
      Func CopyFolder($srcDir, $destDir, $part) ;======> $part shows whats being copied (e.g. Desktop, Documents etc.) If StringRight($srcDir, 1) = "\" Then $srcDir = StringLeft($srcDir, StringLen($srcDir) - 1) If Not FileExists($destDir) Then DirCreate($destDir) $srcSize = DirGetSize($srcDir, $DIR_EXTENDED) $1_percent = $srcSize[0] / 100 $cmd = 'C:\Windows\System32\robocopy.exe "' & $srcDir & '" "' & $destDir & '" /E /Z /XO /XX /FFT /XJ /NDL /NJH /FP /ETA /NP /V /R:10 /W:5 /LOG+:"' & $logFile & '" /XF desktop.ini thumbs.db' Run(@ComSpec & " /c " & $cmd, "", @SW_HIDE) Do Sleep(300) Until ProcessExists("robocopy.exe") ProgressOn($part, $1_percent & "%", "", 0, 0, 16) Do $destSize = DirGetSize($destDir, $DIR_EXTENDED) ProgressSet(Round($destSize[0] / $1_percent, 2), Round($destSize[0] / (1024 * 1024), 2) & " MB of " & Round($srcSize[0] / (1024 * 1024), 2) & " MB copied", Round($destSize[0] / $1_percent, 2) & "%") Sleep(200) Until ProcessExists("robocopy.exe") = 0 ProgressOff() FileWriteLine($logFile, @CRLF & @CRLF) EndFunc ;==>CopyFolder Part of the problem is that I need to generate logs of files being copied. One particular problem is firefox profile directory. the directory is named as oi3jdslk.default or asdwdx.new and I need to copy places.sqlite from this directory.
      Xcopy manages to copy FROM this directory but not TO this directory because of wildcards
      Then I came across this neat script 
      Func CopyFile($fromFile, $todirectory) Local $FOF_RESPOND_YES = 16 Local $FOF_SIMPLEPROGRESS = 256 $winShell = ObjCreate("shell.application") $winShell.namespace($todirectory).CopyHere($fromFile, $FOF_RESPOND_YES) EndFunc ;==>CopyFile It helped me copy other single files but not the one required from firefox profile folder. Here is how I am trying to process that
      $path = @UserProfileDir & "\AppData\Roaming\Mozilla\Firefox\" & $ffDir & "\places.sqlite" CopyFile($path, $BUFolder & "Mozilla") ;===> $BUFolder = "E:\" & @username & " - Backup\" #cs ====================================================== $ffDir is another variable derived from this function. This variable is the gets the default firefox profile to copy places.sqlite from $ffDir is returned as Profiles\xbsen2s.default (Firefox randomizes the profile directory for extra security(?). At least that's what they say. #ce ====================================================== Func FFProfile() Local $iniFile = @UserProfileDir & "\AppData\Roaming\Mozilla\Firefox\profiles.ini" Local $ddNumber Local $ddLine Local $Line If FileExists($iniFile) Then FileOpen($iniFile, 0) For $i = 1 To _FileCountLines($iniFile) $Line = FileReadLine($iniFile, $i) If StringInStr("DEFAULT=1", $Line) Then $ddNumber = $i - 1 ExitLoop ElseIf StringInStr("Name=default", $Line) Then $ddNumber = $i + 2 EndIf Next $ddLine = FileReadLine($iniFile, $ddNumber) $ffDir = StringReplace(StringTrimLeft($ddLine, 5), "/", "\") $ffInstall = 1 Else $ffInstall = 0 EndIf FileClose($iniFile) EndFunc ;==>FFProfile I really like how natural the progress bar from second copy script look like and would like to keep the progress bar uniform across the program. I am willing to learn if anyone can point me in right direction and help me achieve this.

      Any help is highly appreciated!!
    • caramen
      By caramen
      Hello Guys,
      I am trying to make a screenshoot copy it to clipboard and paste it into a word document i want to do that to simplify my procedures création. 
       
      I am trying to use the way of sending Keys "^c" & "^v" i tryed also CTRLDOWN and c but no one of these is working. 
       
      First question :
      Why that's not working? 
      Is there anyother way i can work with ?
       
      I watched the help file for FileCopy but it s only moving Files and not copy them. 
       
      Maybe you can see somthing going wrong in my script but i guess it s not a syntax problem. 
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <ScreenCapture.au3> #include <MsgBoxConstants.au3> HotKeySet("{F2}", "Capture") HotKeySet("{F3}", "Windows") HotKeySet("{F4}", "Copy") Global $Numero = 0 Attendre () Capture() Func Capture() ; Capture full screen $Numero = $Numero+1 _ScreenCapture_Capture (@DesktopDir & "\ScreenCapture\Image"&$Numero&".jpg") ShellExecute( @DesktopDir &"\ScreenCapture\Image"&$Numero&".jpg") Sleep (500) Run("C:\WINDOWS\EXPLORER.EXE /Select, /n,/e," & @DesktopDir & "\ScreenCapture\Image"&$Numero&".jpg" ) WinWaitActive("ScreenCapture") Sleep(1500) Send("^c") ;~ Send ("{CTRLDOWN}") ;~ Sleep(10) ;~ Send ("C") ;~ Sleep(10) ;~ Send ("{CTRLUP}") EndFunc ;==>Example Func Windows () Global $Windows = WinGetTitle("[active]") MsgBox($MB_SYSTEMMODAL, "Windows", "Windows selected for copy is : "&@CRLF&$Windows) EndFunc Func Copy () WinActivate (""&$Windows) Sleep (100) Send("^v") ;~ Send ("{CTRLDOWN}") ;~ Sleep(10) ;~ Send ("V") ;~ Sleep(10) ;~ Send ("{CTRLUP}") EndFunc Func Attendre () While 1 Sleep (10) ;~ GUICreate ("Help",100,100) WEnd EndFunc  
    • oHenry
      By oHenry
      Hi,
      I would like to share this simple project which aims to be a first approach in order to use AutoIt as scripting language for mobile automation (Android).
      This project is an ImperiusGeorge client for mobile test automation that support native Android apps. 
      You can find more information at https://ohtejera.github.io/ImperiusAutoIt and here's the link to the project on Github: https://github.com/ohtejera/ImperiusAutoIt
      Best regards.
      Henry
    • mikkokh
      By mikkokh
      Hi.
       
      I have several different devices connected to my desktop PC with USB COM adapters.
       
      Sometimes there is some error that will cut my USB COM device communication (usualy caused by lose USB connector)... After reconnecting that USB COM adapter, by some reason, Windows will give it different COM port number.
      I know there are some devices with integrated COM port circuit and they are used by client programs that can found them and only them by some USB hardware relate details and then use their COM port to communicate with correct COM device.
      It would be better to define some known and static hardware related string from device managers USB COM adapter details to my script config file, than always redefine different COM port to it.
       
      How can I do this trick with autoit?
       
      I communicate COM devices with cfxUDF.au3 AutoIt UDF.
    • ur
      By ur
      There is a build machine in the network where it will automatically download the code from source repository and compile using visual studio and compresses the complete code in a zip file.
      I have added a line at the end of the build script(written in python on that machine) to copy that zip file to my ESX Virtual machine.
      Where I kept a AutoIT exe to wait for the zip file, once it is copied then I will extract the contents and find only the dll files in the extracted folder and do the Install creation using Installshield silently and copy the created setup files to anther machine.
      I wrote the script till the setup files creation.
       
      But now the problem is, when the zip file is copying through network, the autoit script is detecting it even the copy is still in progress and trying to extract the zip file and failing.
      Even when I am checking whether the file in use is not working.
       
      Any suggestion on how to check whether the copy is completed.?