/
phone_backup.ps1
106 lines (83 loc) · 2.91 KB
/
phone_backup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#for an enhanced version that supports nested folders go to https://github.com/nosalan/powershell-mtp-file-transfer/blob/master/phone_backup_recursive.ps1
$ErrorActionPreference = "Stop"
$DestDirForPhotos = "C:\BACKUP\TELEFON_DCIM_ALL"
$DestDirForVoiceRecordings = "C:\BACKUP\TELEFON_VOICE_RECORDINGS_ALL"
function Create-Dir($path)
{
if(! (Test-Path -Path $path))
{
Write-Host "Creating: $path"
New-Item -Path $path -ItemType Directory
}
else
{
Write-Host "Path $path already exist"
}
}
function Get-SubFolder($parentDir, $subPath)
{
$result = $parentDir
foreach($pathSegment in ($subPath -split "\\"))
{
$result = $result.GetFolder.Items() | Where-Object {$_.Name -eq $pathSegment} | select -First 1
if($result -eq $null)
{
throw "Not found $subPath folder"
}
}
return $result;
}
function Get-PhoneMainDir($phoneName)
{
$o = New-Object -com Shell.Application
$rootComputerDirectory = $o.NameSpace(0x11)
$phoneDirectory = $rootComputerDirectory.Items() | Where-Object {$_.Name -eq $phoneName} | select -First 1
if($phoneDirectory -eq $null)
{
throw "Not found '$phoneName' folder in This computer. Connect your phone."
}
return $phoneDirectory;
}
function Get-FullPathOfMtpDir($mtpDir)
{
$fullDirPath = ""
$directory = $mtpDir.GetFolder
while($directory -ne $null)
{
$fullDirPath = -join($directory.Title, '\', $fullDirPath)
$directory = $directory.ParentFolder;
}
return $fullDirPath
}
function Copy-FromPhone-ToDestDir($sourceMtpDir, $destDirPath)
{
Create-Dir $destDirPath
$destDirShell = (new-object -com Shell.Application).NameSpace($destDirPath)
$fullSourceDirPath = Get-FullPathOfMtpDir $sourceMtpDir
Write-Host "Copying from: '" $fullSourceDirPath "' to '" $destDirPath "'"
$copiedCount = 0;
foreach ($item in $sourceMtpDir.GetFolder.Items())
{
$itemName = ($item.Name)
$fullFilePath = Join-Path -Path $destDirPath -ChildPath $itemName
if(Test-Path $fullFilePath)
{
Write-Host "Element '$itemName' already exists"
}
else
{
$copiedCount++;
Write-Host ("Copying #{0}: {1}{2}" -f $copiedCount, $fullSourceDirPath, $item.Name)
$destDirShell.CopyHere($item)
}
}
Write-Host "Copied '$copiedCount' elements from '$fullSourceDirPath'"
}
$phoneName = "MyPhoneName" #Phone name as it appears in This PC
$phoneRootDir = Get-PhoneMainDir $phoneName
$phoneCardPhotosSourceDir = Get-SubFolder $phoneRootDir "Card\DCIM\Camera"
Copy-FromPhone-ToDestDir $phoneCardPhotosSourceDir $DestDirForPhotos
$phonePhotosSourceDir = Get-SubFolder $phoneRootDir "Phone\DCIM\Camera"
Copy-FromPhone-ToDestDir $phonePhotosSourceDir $DestDirForPhotos
$phoneVoiceRecorderSourceDir = Get-SubFolder $phoneRootDir "Phone\VoiceRecorder"
Copy-FromPhone-ToDestDir $phoneVoiceRecorderSourceDir $DestDirForVoiceRecordings