如何插入我在powershell文件选择器gui中选择的文件?

时间:2018-01-11 03:45:09

标签: powershell batch-file

我找到了一个powershell脚本来打开一个gui文件选择器,现在如何获取我选中的文件插入变量?另外,我有一个名为binsmp的程序,它从命令行替换文件中的十六进制文件,如何将文件插入其中?

@echo off
setlocal
for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
echo You chose %%~I
)
goto :EOF

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Roms (*.sfc;*.smc)|*.sfc;*.smc|All Files (*.*)|*.*"
$f.ShowHelp = $false
$f.Multiselect = $false
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

binsmp filename -paste paste.txt

1 个答案:

答案 0 :(得分:0)

假设你的binsmp调用的filename部分是实际文件名所在的位置,试试这个:

<# :
:: launches a File... Open sort of file chooser and outputs choice(s) to the console
:: https://stackoverflow.com/a/15885133/1683264

@setlocal
@echo off

for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    binsmp %%~I -paste paste.txt
)
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

当您删除围绕实际cmd脚本代码的完全非标准/支持的PowerShell注释块时,您就破坏了它。