AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1770892
Accepted
bat_cmd
bat_cmd
Asked: 2023-02-27 11:13:07 +0800 CST2023-02-27 11:13:07 +0800 CST 2023-02-27 11:13:07 +0800 CST

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

  • 772

我正在使用以下.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_NAME并DEVICE_FOLDER_PATH具有正确的设备名称和路径,以及插入和打开的设备,它将 MP4 文件复制到运行 bat 文件的文件夹中。MP4 文件最初位于此文件夹中:

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

既然这样,你会认为更改上面的 bat 和 ps1 文件并不难,所以它们只需打开 mp4 文件所在的文件夹即可。

我已经连续三天询问 Chat GPT,以更改我的 2 个脚本,以便它只在 Windows 资源管理器中打开文件夹,并且在可能尝试了 50 多次之后它没有给我一个有效的答案!

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

如果有人可以阐明如何执行此操作,那么您将解决我几个小时以来一直试图解决的问题!

windows
  • 1 1 个回答
  • 51 Views

1 个回答

  • Voted
  1. Best Answer
    Keith Miller
    2023-02-28T18:16:03+08:002023-02-28T18:16:03+08:00

    我没有看过批处理部分(从来没有对批处理做过太多),但对于PowerShell部分,这里有一些基本但非常实用的代码,可以将 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()
    }
    
    • 1

相关问题

  • 如何在 Windows Precision 触摸板上禁用鼠标加速?

  • 批量重命名图像文件集

  • Python 的“pass”参数的批处理等价物是什么?

  • 在 Windows 上与 Docker 守护进程通信

  • 资源管理器侧面板中的桌面外壳快捷方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve