如何在多个文件上调用动词 [英] How to invoke a verb on multiple files

查看:65
本文介绍了如何在多个文件上调用动词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PowerShell 我想对多个文件调用打印动词.在 Windows 资源管理器中,我可以进入一个文件夹,选择多个文件,右键单击并选择打印选项.这将打开带有所有选定文件的打印图片"对话框.我可以使用以下方法为一个文件执行此操作:

Using PowerShell I would like to invoke the print verb on multiple files. In Windows Explorer I can go into a folder, select a number of files, right-click and choose the print options. This opens up the Print Pictures dialog with all the selected files. I am able to do this for one file using:

$path = "C:\person.jpg";
Start-Process -FilePath $path -Verb Print | Out-Null;
Start-Sleep -s 150;

但是想知道我如何为多个文件做到这一点.

But was wondering how I could do it for a number of files.

推荐答案

您可以使用 COM 在一次操作中对多个文件调用一个动词.

You can use COM to invoke a verb on multiple files in one operation.

假设...

$folderPath = 'X:\Test'
$verbName = 'print'
$verbArguments = ''

...您可以使用...打印文件夹中的所有对象

...you can print all objects in a folder with...

$application = New-Object -ComObject 'Shell.Application'
$folder = $application.NameSpace($folderPath)
$folderItems = $folder.Items()
Write-Verbose "Folder ""$($folder.Self.Name)"" contains $($folderItems.Count) item(s)."
$folderItems.InvokeVerbEx($verbName, $verbArguments)

我说对象"是因为 $folderItems 将包含文件和文件夹.默认似乎是枚举子文件夹并忽略隐藏的对象,而动词在不支持它们的对象上被忽略.

I say "objects" because $folderItems will contain both files and folders. The default appears to be enumerate subfolders and ignore hidden objects, while verbs are ignored on objects that don't support them.

例如,如果您只想打印具有特定扩展名的直接子文件并包含隐藏文件,则可以使用 Filter() 方法...

If, for example, you wanted to only print files with a certain extension that are immediate children and include hidden files, you can do so using the Filter() method...

New-Variable -Name 'SHCONTF_NONFOLDERS'    -Option 'Constant' -Value 0x00040
New-Variable -Name 'SHCONTF_INCLUDEHIDDEN' -Option 'Constant' -Value 0x00080

$application = New-Object -ComObject 'Shell.Application'
$folder = $application.NameSpace($folderPath)
$folderItems = $folder.Items()
$folderItems.Filter($SHCONTF_NONFOLDERS -bor $SHCONTF_INCLUDEHIDDEN, '*.jpg')
Write-Verbose "Filtered folder ""$($folder.Self.Name)"" contains $($folderItems.Count) item(s)."
$folderItems.InvokeVerbEx($verbName, $verbArguments)

如果您想打印一些不能很好地按扩展名、可见性等对齐的自定义文件集,那么我看不到这样做的方法.这似乎需要修改 $folderItems 或创建一个新的 FolderItems3 实例,而且似乎都不可能.我看到有一个 ShellFolderView 类型 支持项目选择,但这看起来是为了与资源管理器进行交互(-like) 窗口.

If you want to print some custom set of files that don't nicely align by extension, visibility, etc. then I'm not seeing a way to do that. That would seem to require modifying $folderItems or creating a new FolderItems3 instance, and neither appears to be possible. I see there is a ShellFolderView type that supports item selection, but that looks like it's for interacting with an Explorer(-like) window.

上面使用的类型和常量的文档:

  • Shell.Application property
  • Shell.NameSpace method
  • Folder object
  • FolderItems3 object
  • FolderItems2.InvokeVerbEx method
  • FolderItems3.Filter method
  • _SHCONTF Enumeration

这篇关于如何在多个文件上调用动词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆