viennese_finger has asked for the wisdom of the Perl Monks concerning the following question:

Hi

i just posted this and it came up as posted by an anonymous user, so I'm redoing it properly...bear with me

I would like to write a perl script that allows a user to select a file from windows explorer and use this as the input for the code within the perl script (like STDIN).

So the code would open windows explorer to the correct directory, the user would then click on (select) their file, and then the script would do it's business on the selected file as a variable (e.g. my $selectedFile).

I have found some code to open windows explorer:

my $explorer = 'c:/windows/SysWOW64/explorer.exe'; my $directory = 'C:\\testdir\\'; system($explorer,$directory);
...which works fine (based on Perl monks http://www.perlmonks.org/bare/?node_id=313539) to open windows explorer from the pl script.

How do I get the code to then recognise the file I click on in the explorer window, and create a variable from it? Is it possible or is Perl not the way to do it?

Cheers, Matt

Replies are listed 'Best First'.
Re: select input file from windows explorer
by Corion (Patriarch) on Jul 06, 2016 at 17:55 UTC

    Launching explorer.exe will not really help your users because Explorer.exe doesn't talk back to your program. I would look at Win32::GUI::Reference::Methods::OpenFile or ::BrowseForFolder. Another alternative would be to use Tk, but I don't really know that much about it.

Re: select input file from windows explorer
by BrowserUk (Patriarch) on Jul 06, 2016 at 17:56 UTC

    See Win32::FileOp::OpenDialog().


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: select input file from windows explorer
by Marshall (Canon) on Jul 06, 2016 at 18:02 UTC
    Tk is a way to build GUI's. There is definitely a learning curve to this critter! Here is a short hack that implements the standard "File" options of Open,Save,Close,Exit. See the command window for the print output of what it does. If you are using a recent version of Perl, Tk comes with the distribution and this should run without having to install anything.
    #!usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; #$mw->geometry("1000x400+0+0"); my $menu_f = $mw->Frame()->pack(-side=>'top',-fill=>'x'); my $menu_file = $menu_f->Menubutton (-text=>'File',-tearoff=>'false') ->pack(-side=>'left'); $menu_file->command(-label=>'Open', -command=> \&open_log); $menu_file->command(-label=>'Save', -command=> sub {print "in Save\n";}); $menu_file->command(-label=>'Close', -command=> sub {print "in Close\n";}); $menu_file->command(-label=>'Exit',-command=>\&exit_msg); sub open_log { my @types = (["log files", [qw/ .log/]], ["All files", '*'], ); my $file = $mw->getOpenFile(-filetypes => \@types) or return; print "$file\n"; ######## ### call worker program here ########## } sub exit_msg{ my $response = $mw->messageBox( -title => "some title", -message => "Do you really want to exit?\nAll unsaved work will +be lost!\n\n", -type => 'YesNoCancel'); print "exit_msg response = $response\n"; exit() if ($response =~ /^yes$/i); } MainLoop;
    Update: I guess going this route is more than what you need. But FYI, such things are possible in Perl!
Re: open windows explorer and select file for processing
by stevieb (Canon) on Jul 06, 2016 at 18:05 UTC

    Cross posted at StackOverflow. When you cross-post on numerous sites, please let all sites know you're doing so, as to not waste people's time working on a solution when one could already be located somewhere else.

    Here's a very simple example from my SO answer. It requires only a single use statement (you'll have to install Tk first, of course), and one line of code. It's also cross-platform:

    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; }
      I posted a more involved Tk starter script at the original node, Re: select input file from windows explorer that shows the file type options.

      At least with Active State, Tk comes with the distribution, so there is nothing to install. There are about 280 modules that shipped with the distribution I'm currently using, Perl 5.20.

        I saw that Marshall, but not until I had this post done. I figured that since it didn't appear they were using any GUI and this seemed to be the only UI feature they needed, it'd do it.

        It's nice that you posted a full-blown example though, for sure.

        Perhaps I'll consider all of the nodes under the anonymonk posting to be re-parented up here.

Re: open windows explorer and select file for processing
by FreeBeerReekingMonk (Deacon) on Jul 06, 2016 at 18:26 UTC
    Matt,

    What I usually do, is write the GUI in another language, like powershell, then get those variables, and run perl with these as parameters. This way, the same perl script works if you want to automate things (non-interactively).

    If you still want to use Perl, that works too, of course:

    Win32::GUI or TK: How do I make Open and Save As file dialogs? (Win32)

    Personally, I like AutoIT3 or VBScript

Re: open windows explorer and select file for processing
by harangzsolt33 (Deacon) on Jul 07, 2016 at 03:58 UTC

    Here's another idea: If you want any file to be the STDIN for a particular Perl Script on your computer, you could do this: Create a file named MyScript.pl that takes some data from STDIN and does something with it. Then create a file called X.BAT that executes your script like so:

    @ECHO OFF C:\BIN\PERL\TINYPERL -I C:\BIN\PERL\LIB C:\BIN\PERL\MYSCRIPT.PL < "%1" ECHO. PAUSE
    X.BAT takes an argument. So, now you open REGEDIT in Windows. Open HKEY_CLASSES_ROOT .. then under that heading, find "txtfile" Under the txtfile heading, look for "shell," and under the "shell" heading, look for print, then command and change the default value to: C:\BIN\PERL\X.BAT "%1"

    What this does is it allows you to right-click on any text file in Windows Explorer, and when the context menu comes up, you can select Print, and instead of printing the text file, it will feed it to your Perl script through STDIN. ;)

Re: select input file from windows explorer
by viennese_finger (Initiate) on Jul 06, 2016 at 17:59 UTC

    oh that's great - thanks for the clear input, monks!

    matt

    PS sorry about the double posting...