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

I'm writing a little Win32::GUI application to do a bit of m3u playlist generation, and I want the user to be able to browse for the folder using window's own boxes. However, I can't see how I can feed the current "given" directory into the Browse for Folder, because, personally, it's extremely annoying having to get to the current location each time, especially if it's deeply buried. Does anyone know if this is possible?

Replies are listed 'Best First'.
Re: BrowseForFolder usage
by tachyon (Chancellor) on May 30, 2004 at 11:34 UTC

    This widget will let you browse for a folder and select a playlist. Just set the root when you call BrowseForFolder.

    use Win32::GUI; use Win32::Sound; my $folder = Win32::GUI::BrowseForFolder( -root => "C:\\Program Files", -includefiles => 1, ); print "Selected: $folder\n"; opendir DIR, $folder or die "Can't read $folder: $!\n"; my @files = grep{ -f "$folder\\$_" }readdir DIR; closedir DIR; $W = new GUI::Window( -title => "Select Playlist", -left => 100, -top => 100, -width => 360, -height => 210, -style => 1024 | WS_BORDER | WS_CAPTION | WS_SYSMENU, -name => "Window", ) or die "new Window\n"; $List1 = $W->AddListbox( -name => "List1", -left => 5, -top => 5, -height => 155, -menu => 1, -tabstop => 1, -group => 1, -width => 100, -foreground => [255, 255, 255], -background => [64, 64, 64], -style => WS_VSCROLL | WS_VISIBLE | WS_CHILD, ) or die "new Listbox\n"; $List1->SendMessage(0x0195, 201, 0); $List1->AddString($_) for sort @files; $List1->Select(0); $List2 = $W->AddListbox( -name => "List2", -style => WS_VSCROLL | WS_VISIBLE | WS_CHILD, -left => 250, -top => 5, -height => 155, -menu => 2, -tabstop => 1, -group => 1, -width => 100, -vscroll => 1, ) or die "new Listbox\n"; $Add = $W->AddButton( -text => "Add >", -left => 125, -top => 5, -width => 100, -menu => 3, -name => "Add" ) or die "new Button"; $Add = $W->AddButton( -text => "Skip", -left => 125, -top => 35, -width => 100, -menu => 4, -name => "Skip" ) or die "new Button"; $AddAll = $W->AddButton( -text => "All >>", -left => 125, -top => 65, -width => 100, -menu => 5, -name => "AddAll" ) or die "new Button"; $Remove = $W->AddButton( -text => "< Remove", -left => 125, -top => 95, -width => 100, -menu => 6, -name => "Remove" ) or die "new Button"; $RemoveAll = $W->AddButton( -text => "<< All", -left => 125, -top => 125, -width => 100, -menu => 7, -name => "RemoveAll" ) or die "new Button"; $Close = $W->AddButton( -text => "Play", -left => 250, -top => 160, -width => 100, -name => "Play" ) or die "new Button"; $W->Show; $return = $W->Dialog(); sub Add_Click { my $sel = $List1->SelectedItem(); if($sel != -1) { my $num = $List2->InsertItem($List1->GetString($sel)); $List2->Select($num); $sel = -1 if $sel > $List1->Count-2; $List1->Select($sel+1); } else { Win32::Sound::Play("SystemDefault", SND_ASYNC); } return 1; } sub Skip_Click { my $sel = $List1->SelectedItem() || 0; $sel = -1 if $sel > $List1->Count-2; $List1->Select($sel+1); return 1; } sub Remove_Click { my $sel = $List2->SelectedItem(); if($sel != -1) { $List2->RemoveItem($sel); $sel-- if $List2->Count <= $sel; $List2->Select($sel) if $sel >= 0; } else { Win32::Sound::Play("SystemDefault", SND_ASYNC); } return 1; } sub AddAll_Click { for $i (0..$List1->Count-1) { $List2->InsertItem($List1->GetString($i)); } $List2->Select($List2->Count-1); return 1; } sub RemoveAll_Click { if($List2->Count > 0) { $List2->Clear; } else { Win32::Sound::Play("SystemDefault", SND_ASYNC); } return 1; } sub Play_Click { for $i (0..$List2->Count-1) { print "Play: ", $List2->GetString($i), "\n"; } return 1; }

    cheers

    tachyon

      Thanks for the response, but I don't think I made myself very clear. This is going to be a GUI app, and it will have a box for specifying the starting directory. If, for example, someone wishes to run this app only in two specific directories:

      c:\MP3s\Comedy

      c:\MP3s\Oldies

      If I were to use root, having descended into Comedy, I would not be able to change to Oldies.

      Does anyone know if there is a way of specifying a folder without rooting to it...

        Get a grip. I have shown you how to use the browse for folder widget and make another widget based on the selection. This is proof of concept not finished product. That's your job. I take it you understand how to embed widgets in widgets. Decide how you want it to work. Make a main window that supplies all the functionality you want. FWIW I would suggest Tk as it is much better documented and you will find ready made widgets quite like what you want. There are a number of GUI packages you can use, have a look and pick one that works for you.

        cheers

        tachyon