-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropboxuploadfolder.ps1
41 lines (36 loc) · 1.48 KB
/
dropboxuploadfolder.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
# Set variables
$dropboxAccessToken = "ACCESS_TOKEN"
$localFolderPath = "C:\example" # Replace with your local folder path, e.g., "C:\myfolder"
$dropboxDestinationPath = "/test" # Replace with your desired destination path in Dropbox, e.g., "/mydropboxfolder"
# Set Dropbox API URL
$dropboxApiUrl = "https://content.dropboxapi.com/2/files/upload"
# Set headers
$headers = @{
"Authorization" = "Bearer $dropboxAccessToken"
"Content-Type" = "application/octet-stream"
}
# Function to upload a file
Function Upload-FileToDropbox ($localFilePath, $dropboxFilePath) {
$headers["Dropbox-API-Arg"] = (ConvertTo-Json -Compress -InputObject @{
path = $dropboxFilePath
mode = "add"
autorename = $true
mute = $false
})
Try {
$fileContent = [System.IO.File]::ReadAllBytes($localFilePath)
Invoke-WebRequest -Uri $dropboxApiUrl -Headers $headers -Method Post -Body $fileContent -ErrorAction Stop
Write-Host "File uploaded successfully: $dropboxFilePath"
}
Catch {
Write-Host "Error uploading the file: $dropboxFilePath - $($_.Exception.Message)"
}
}
# Upload the entire folder
Get-ChildItem -Path $localFolderPath -Recurse | ForEach-Object {
if (-not $_.PSIsContainer) {
$relativePath = $_.FullName.Substring($localFolderPath.Length).Replace('\', '/')
$dropboxFilePath = $dropboxDestinationPath + $relativePath
Upload-FileToDropbox -localFilePath $_.FullName -dropboxFilePath $dropboxFilePath
}
}