Estou usando o seguinte .ps1
script e .bat
arquivo para copiar arquivos MP4 de uma pasta no meu telefone que é um dispositivo MTP sem letra de unidade (daí a necessidade de usar o Powershell).
Quero modificar o script do Powershell e o arquivo bat para que ele não copie os arquivos MP4 e, em vez disso, apenas abra a pasta onde estão os arquivos MP4.
Aqui está o script do Powershell, chamado 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"
}
}
}
}
}
Aqui está o .bat
arquivo...
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
Se o arquivo de lote acima for executado e assumindo as 2 variáveis DEVICE_NAME
e DEVICE_FOLDER_PATH
tiver o nome e o caminho corretos do dispositivo, junto com o dispositivo sendo conectado e ligado, ele copia os arquivos MP4 para a pasta onde o arquivo bat foi executado. Os arquivos MP4 estão originalmente nesta pasta:
This PC\Galaxy A3 (2017)\Card\DCIM\Camera
Como isso funciona, você pensaria que não seria tão difícil alterar os arquivos bat e ps1 acima, para que eles apenas abram a pasta onde estão os arquivos mp4.
Estou pedindo ao Chat GPT há três dias seguidos para alterar meus 2 scripts, para que ele apenas abra a pasta no Windows Explorer e não tenha me dado uma resposta funcional depois de provavelmente mais de 50 tentativas!
Como o acima funciona para copiar arquivos MP4, não vejo por que seria um problema modificá-lo para que ele apenas abra a pasta em vez de copiar os arquivos MP4 dela, mas não consegui até agora.
Se alguém puder esclarecer como fazer isso, estará resolvendo algo que venho tentando resolver há horas!
Eu não olhei para a parte do lote (nunca fiz muito com o lote), mas para a parte do PowerShell , aqui está um código básico, mas bastante funcional, que abrirá a pasta MTP na pasta de destino. Observe que agora são necessários apenas dois parâmetros - você deve modificar a chamada do
.bagt
para refletir isso...