如何使用BATCH选择器选择多个文件? [英] How to select multiple files using the BATCH selector?

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

问题描述

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

I need to amend the code given so that the code selects multiple files at once. After file selection, I want to save the amount of files selected in a variable. I also need a variable to save the directory path of the file.

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

For example: C: \Users\Andrew\Desktop. And finally, I need another variable to save the extension of the selected files (I'm assuming all files have the same extension). Example: In the File.txt file, txt is saved. I hope you can help me.

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%

推荐答案

如果不熟练使用多种脚本语言,则始终很难将它们组合在一起.

If not fluent in multiple script languages it's always difficult to combine them.

您应该在PowerShell中开发解决方案,如果确实需要,可以按预期将其分批包装.

You should develop your solution in PowerShell and if really neccessary wrap it in batch once it works as expected.

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

OpenFileDialog needs some more settings before invoking the .ShowDialog() method:

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

还要在FileNames属性中返回Multiselect的结果,请注意复数.

Also the result of a Multiselect is returned in the FileNames property, note the plural.

整个PowerShell部分,其变量名简称为$OFD

The whole PowerShell part with the variable name shorted to $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提示使用数组变量批量接收文件名

As Aacini hinted use an array variable to receive the file names in batch

:: 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

这篇关于如何使用BATCH选择器选择多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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