[英]perl - open windows explorer and select input file for code to work on
我想編寫一個perl腳本,允許用戶從Windows資源管理器中選擇一個文件,並將其用作perl腳本(例如STDIN)中代碼的輸入。
因此,代碼會將Windows資源管理器打開到正確的目錄,然后用戶單擊(選擇)他們的文件,然后腳本將對所選文件作為變量(例如我的$ selectedFile)進行處理。
我找到了一些打開Windows資源管理器的代碼:
my $explorer = 'c:/windows/SysWOW64/explorer.exe';
my $directory = 'C:\\testdir\\';
system($explorer,$directory);
...可以正常工作(基於Perl僧侶http://www.perlmonks.org/bare/?node_id=313539 ),可以從pl腳本打開Windows資源管理器。
如何獲取代碼以識別在瀏覽器窗口中單擊的文件,並從中創建變量? 有可能還是Perl無法做到這一點?
干杯,馬特
這是一個使用Win32 :: GUI的非常簡陋的腳本:
use strict;
use warnings;
use Win32::GUI ();
my $file = Win32::GUI::GetOpenFileName(
-filemustexist => 1,
);
if (defined $file) {
print "Selected file: $file\n";
} else {
print "Canceled\n";
}
如果您還沒有Win32 :: GUI( Can't locate Win32/GUI.pm in @INC ...
),則可以使用cpan Win32::GUI
至少使用Strawberry Perl進行安裝 。 如果您使用的是ActivePerl ,我認為可以通過ppm install Win32-GUI
來獲得。
這是使用Tk的一種非常簡單的方法。
use warnings;
use strict;
use Tk;
my $file = Tk::MainWindow->new->getOpenFile;
print "selected file: $file\n\n";
open my $fh, '<', $file or die $!;
while (<$fh>){
print;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.