如何使用BATCH选择器选择多个文件?

时间:2018-09-09 02:05:49

标签: batch-file

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

例如:Function RangeToHTML(rng As Range) As String Dim fso As Object Dim ts As Object Dim tempFile As String Dim tempWB As Workbook tempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm" 'Copy the range and create a new workbook to past the data in rng.Copy Set tempWB = Workbooks.Add(1) With tempWB.Sheets(1) .Cells(1).PasteSpecial Paste:=8 .Cells(1).PasteSpecial xlPasteValues, , False, False .Cells(1).PasteSpecial xlPasteFormats, , False, False .Cells(1).Select Application.CutCopyMode = False On Error Resume Next .DrawingObjects.Visible = True .DrawingObjects.Delete On Error GoTo 0 End With 'Publish the sheet to a htm file With tempWB.PublishObjects.Add( _ SourceType:=xlSourceRange, _ Filename:=tempFile, _ Sheet:=tempWB.Sheets(1).name, _ Source:=tempWB.Sheets(1).UsedRange.Offset(1).Address, _ HtmlType:=xlHtmlStatic) .Publish (True) End With 'Read all data from the htm file into RangetoHTML Set fso = CreateObject("Scripting.FileSystemObject") Set ts = fso.GetFile(tempFile).OpenAsTextStream(1, -2) RangeToHTML = ts.ReadAll ts.Close RangeToHTML = Replace(RangeToHTML, _ "align=center x:publishsource=", "align=left x:publishsource=") tempWB.Close savechanges:=False Kill tempFile Set ts = Nothing Set fso = Nothing Set tempWB = Nothing End Function 。最后,我需要另一个变量来保存所选文件的扩展名(我假设所有文件都具有相同的扩展名)。示例:在File.txt文件中,保存C: \Users\Andrew\Desktop。我希望你能帮助我。

txt

1 个答案:

答案 0 :(得分:0)

如果不能流利使用多种脚本语言,通常很难将它们组合在一起。

您应该在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