1

I have a simple script to make file transfers between a local directory and an MTP device. For the MTP device, I created a ComObject and traversed to the desired directory. Then, I use the CopyHere function to copy, using the option 20 which is supposed to suppress the progress bar and say yes to any dialog.

however, I find that all dialogs are still there including a confirmation for replacing the file:

replacement confirmation

I also tried deleting the files first as seen in the code, but then I get this:

deletion confirmation

param ([string]$ListFile = "C:\Users\benriveraflores\Documents\Projects\Switch Saves Sync\Save Directories.txt")

$shell = New-Object -ComObject Shell.Application
$root = $shell.NameSpace(0x11).self

Get-Content $ListFile | ForEach-Object {
    if ($_ -match '^\s*$' -or $_ -match '^\s*#') { return }
    $parts = $_ -split "`t", 2
    if ($parts.Count -ne 2) { return }

    $src  = $parts[0].Trim()
    
    $dest = $parts[1].Trim()
    
    $destParts = $dest.Split('\')
    $destDevice = $destParts[1]
    $destPath = $destParts[2..($destParts.Count - 1)] -join '\'
    $destDeviceObj = $root.GetFolder.Items() | Where-Object { $_.Name -eq $destDevice }
    $destFolder = $destDeviceObj.GetFolder

    $destPath.Split('\') | ForEach-Object {
        $folderName = $_
        $nextItem = $destFolder.Items() | Where-Object { $_.Name -eq $folderName }
        $destFolder = $nextItem.GetFolder
    }

   Get-ChildItem -Path $src -File | ForEach-Object {
        $fileName = $_.Name
        $fileToDelete = $destFolder.Items() | Where-Object { $_.Name -eq $fileName }
        $fileToDelete.InvokeVerbEx('delete', 20) 
   }

    Get-ChildItem -Path $src | ForEach-Object {
        $destFolder.CopyHere($_.FullName, 20)
    }
}

All I want is to suppress all the dialogs.

Does anyone know how to fix it? If not, is there any other more reliable way to interface with MTP devices for scripting?

2
  • adb is a great way but is limited to Android. Since (I assume) you are interacting with a Switch here, you may check out the MediaDevices library. I can post an example of using that library with Powershell if you would like. Commented Oct 5 at 12:55
  • You might want to try1556 (0x614) which is FOF_NO_UI (FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR) instead of 20 (which is only FOF_SILENT | FOF_NOCONFIRMATION). See learn.microsoft.com/en-us/windows/win32/api/shellapi/… and values magnumdb.com/search?q=FOF_* Commented Oct 7 at 8:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.