是否可以修改这些 bat/ps1 文件以打开 MTP 设备上的文件夹(我当前的 bat/ps1 脚本已经可以复制 mp4 文件)

是否可以修改这些 bat/ps1 文件以打开 MTP 设备上的文件夹(我当前的 bat/ps1 脚本已经可以复制 mp4 文件)

我正在使用以下.ps1脚本和.bat文件从手机上的文件夹复制 MP4 文件,该文件夹是没有驱动器号的 MTP 设备(因此需要使用 Powershell)。

我想修改 Powershell 脚本和 bat 文件,这样它就不会复制 MP4 文件,而是只打开 MP4 文件所在的文件夹。

这是 Powershell 脚本,名为CFD.ps1

param([string]$deviceName,[string]$deviceFolder,[string]$targetFolder,[string]$filter='(.jpg)|(.mp4)$')
 
function Get-ShellProxy
{
    if( -not $global:ShellProxy)
    {
        $global:ShellProxy = new-object -com Shell.Application
    }
    $global:ShellProxy
}
 
function Get-Device
{
    param($deviceName)
    $shell = Get-ShellProxy
    # 17 (0x11) = ssfDRIVES from the ShellSpecialFolderConstants (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx)
    # => "My Computer" — the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel.
    # This folder can also contain mapped network drives.
    $shellItem = $shell.NameSpace(17).self
    $device = $shellItem.GetFolder.items() | where { $_.name -eq $deviceName }
    return $device
}
 
function Get-SubFolder
{
    param($parent,[string]$path)
    $pathParts = @( $path.Split([system.io.path]::DirectorySeparatorChar) )
    $current = $parent
    foreach ($pathPart in $pathParts)
    {
        if ($pathPart)
        {
            $current = $current.GetFolder.items() | where { $_.Name -eq $pathPart }
        }
    }
    return $current
}
 
$deviceFolderPath = $deviceFolder
$destinationFolderPath = $targetFolder
# Optionally add additional sub-folders to the destination path, such as one based on date
 
$device = Get-Device -deviceName $deviceName
$folder = Get-SubFolder -parent $device -path $deviceFolderPath
 
$items = @( $folder.GetFolder.items() | where { $_.Name -match $filter } )
if ($items)
{
    $totalItems = $items.count
    if ($totalItems -gt 0)
    {
        # If destination path doesn't exist, create it only if we have some items to move
        if (-not (test-path $destinationFolderPath) )
        {
            $created = new-item -itemtype directory -path $destinationFolderPath
        }
 
        Write-Verbose "Processing Path : $deviceName\$deviceFolderPath"
        Write-Verbose "Moving to : $destinationFolderPath"
 
        $shell = Get-ShellProxy
        $destinationFolder = $shell.Namespace($destinationFolderPath).self
        $count = 0;
        foreach ($item in $items)
        {
            $fileName = $item.Name
 
            ++$count
            $percent = [int](($count * 100) / $totalItems)
            Write-Progress -Activity "Processing Files in $deviceName\$deviceFolderPath" `
                -status "Processing File ${count} / ${totalItems} (${percent}%)" `
                -CurrentOperation $fileName `
                -PercentComplete $percent
 
            # Check the target file doesn't exist:
            $targetFilePath = join-path -path $destinationFolderPath -childPath $fileName
            if (test-path -path $targetFilePath)
            {
                write-error "Destination file exists - file not moved:`n`t$targetFilePath"
            }
            else
            {
                $destinationFolder.GetFolder.CopyHere($item)
                if (test-path -path $targetFilePath)
                {
                    # Optionally do something with the file, such as modify the name (e.g. removed device-added prefix, etc.)
                }
                else
                {
                    write-error "Failed to move file to destination:`n`t$targetFilePath"
                }
            }
        }
    }
}

这是.bat文件...

cls
@echo off
color 0f
title Copy MP4 Files

:: Use Powershell to copy MP4 files. Requires Powershell script "CFD.ps1" next to this batch file.

:: --------------------------------------------------------------------------------------------------------------
:: Put your device name here inside the double quotes:
set DEVICE_NAME="Galaxy A3 (2017)"

:: Put your device folder path (the part after the device name in "This PC") here inside the double quotes:
set DEVICE_FOLDER_PATH="Card\DCIM\Camera"

:: -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:: Copy MP4 files from Device
cls
@echo off
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dp0CFD.ps1' -deviceName '%DEVICE_NAME%' -deviceFolder '%DEVICE_FOLDER_PATH%' -targetFolder '%CD%' -filter '(.mp4)$'"

:: ------------------------------------------------------------------------------
exit

如果运行上述批处理文件并假设 2 个变量DEVICE_NAMEDEVICE_FOLDER_PATH具有正确的设备名称和路径,以及设备已插入并打开,它会将 MP4 文件复制到运行批处理文件的文件夹中。MP4 文件最初位于此文件夹中:

This PC\Galaxy A3 (2017)\Card\DCIM\Camera

既然它有效,您会认为更改上述 bat 和 ps1 文件并不困难,因此它们只需打开包含 mp4 文件的文件夹。

我已经连续三天要求 Chat GPT 更改我的两个脚本,以便它只打开 Windows 资源管理器中的文件夹,但在尝试了可能超过 50 次之后,它还没有给我一个可行的答案!

由于上述方法可以复制 MP4 文件,我不明白为什么修改它会有问题,所以它只是打开文件夹而不是从中复制 MP4 文件,但到目前为止我还没有成功。

如果有人能解释一下如何做到这一点,那么你就能解决我几个小时以来一直试图解决的问题!

答案1

我没有看过批处理部分(从来没有对批处理做过太多处理),但是对于电源外壳部分,这里有一些基本但功能齐全的代码,将打开 MTP 文件夹到目标文件夹。请注意,它现在只需要两个参数——您应该修改从的调用.bagt以反映这一点...

Param([string]$deviceName,
      [string]$deviceFolderPath)
 
$shell  = New-Object -com shell.application

$DeviceFolder = ($shell.NameSpace("shell:MyComputerFolder").Items() | where Name -eq $deviceName).GetFolder

$SourceFolderAsFolderItem = $DeviceFolderPath.Split('\') |
ForEach-Object -Begin {
    $comFolder = $DeviceFolder
} -Process {
    Try
    {
        $comFolder = ($comFolder.Items() | where {$_.IsFolder} | where Name -eq $_).GetFolder
    }
    Catch
    {
        Write-Error 'Failed to parse $DeviceFolderPath'
    }
} -End {
    $comFolder.Self
}

If ( $SourceFolderAsFolderItem )
{
    $SourceFolderAsFolderItem.InvokeVerb()
}

相关内容