【问题标题】:How to select multiple files using the BATCH selector?如何使用 BATCH 选择器选择多个文件?
【发布时间】:2018-09-09 02:05:48
【问题描述】:

我需要修改给定的代码,以便代码一次选择多个文件。选择文件后,我想将选择的文件数量保存在一个变量中。我还需要一个变量来保存文件的目录路径。

例如:C: \Users\Andrew\Desktop。最后,我需要另一个变量来保存所选文件的扩展名(我假设所有文件都具有相同的扩展名)。示例:在 File.txt 文件中,保存了txt。我希望你能帮助我。

rem preparation command
set pwshcmd=powershell -noprofile -command "&{[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') | Out-Null;$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $OpenFileDialog.ShowDialog()|out-null; $OpenFileDialog.FileName}"

rem exec commands powershell and get result in FileName variable
for /f "delims=" %%I in ('%pwshcmd%') do set "FileName=%%I"

echo %FileName%

【问题讨论】:

标签: batch-file


【解决方案1】:

如果不能流利地使用多种脚本语言,则总是很难将它们结合起来。

您应该在 PowerShell 中开发您的解决方案,如果确实有必要,一旦它按预期工作,就将其批量包装。

OpenFileDialog 在调用.ShowDialog() 方法之前需要更多设置:

$OpenFileDialog.Multiselect = $true
$OpenFileDialog.Filter = 'TXT (*.txt)| *.txt'
$OpenFileDialog.InitialDirectory = [Environment]::GetFolderPath('Desktop')

FileNames 属性中返回 Multiselect 的结果,注意复数。

整个 PowerShell 部分,变量名缩写为 $OFD

[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms')|Out-Null
$OFD = New-Object System.Windows.Forms.OpenFileDialog
$OFD.Multiselect = $True
$OFD.Filter = 'TXT (*.txt)| *.txt'
$OFD.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$OFD.ShowDialog()|out-null
$OFD.FileNames

正如 Aacini 所暗示的,使用数组变量批量接收文件名

:: Q:\Test\2018\09\09\SO_52240766.cmd
@Echo off & Setlocal EnableDelayedExpansion
rem preparation command
set pwshcmd=powershell -NoP -C "[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms')|Out-Null;$OFD = New-Object System.Windows.Forms.OpenFileDialog;$OFD.Multiselect = $True;$OFD.Filter = 'TXT (*.txt)| *.txt';$OFD.InitialDirectory = [Environment]::GetFolderPath('Desktop');$OFD.ShowDialog()|out-null;$OFD.FileNames"

rem exec commands powershell and get result in FileName variable
Set i=0
for /f "delims=" %%I in ('%pwshcmd%') do (
    Set /A i+=1
    set "FileName[!i!]=%%I"
)
If %i% gtr 0 (
    Echo %i% files selected
    Set FileName
) else (
    Echo no files selected
)

样本输出

15:39:24 Q:\Test\2018\09\09________________________________________
> SO_52240766.cmd
2 files selected
FileName[1]=C:\Users\LotPings\Desktop\Dokument1.txt
FileName[2]=C:\Users\LotPings\Desktop\espressif-MAC1.txt

【讨论】:

  • 这正是我想要的。谢谢 LotPings
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 2015-07-01
  • 2020-05-24
相关资源
最近更新 更多