# Can SWIN CommonDialog.openFilename() select multiple files?

**URL:** <https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374>\
**Category:** ruby-talk\
**Created:** [11 February 2010 15:32 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374 "2010-02-11T15:32:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [11 February 2010 15:32 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/1 "2010-02-11T15:32:26Z")

</div>

Can the SWIN CommonDialog.openFilename() method be used to select  
multiple filenames at one time?

Thanks,

--Alex DeCaria

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [11 February 2010 19:35 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/2 "2010-02-11T19:35:05Z")

</div>

Had to look at the code but looks like it is a straight forward  
wrapper of the CommonDialog:GetOpenFileName ([http://msdn.microsoft.com/](http://msdn.microsoft.com/)  
en-us/library/ms646829(VS.85).aspx#open\_file)  
so supports all its functionality  
Set the OFN\_ALLOWMULTISELECT flag, and the result should be a string  
containing all the files.  
The windows doc says it could be NULL separated or space separated so  
you'll need to play a bit to figure out  
how to parse out the individual file names.

This is all based on reading, so while in theory its correct, in  
practice you'll have to figure some stuff out 😉

cheers  
Chris

> **···**
>
> On Feb 11, 10:32 am, Alex DeCaria \<alex.deca...@millersville.edu\> wrote:
> 
> > Can the SWIN CommonDialog.openFilename() method be used to select  
> > multiple filenames at one time?

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [11 February 2010 22:26 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/3 "2010-02-11T22:26:03Z")

</div>

Chris Hulan wrote:

> > Can the SWIN CommonDialog.openFilename() method be used to select  
> > multiple filenames at one time?
> 
> Had to look at the code but looks like it is a straight forward  
> wrapper of the CommonDialog:GetOpenFileName ([http://msdn.microsoft.com/](http://msdn.microsoft.com/)  
> en-us/library/ms646829(VS.85).aspx#open\_file)  
> so supports all its functionality  
> Set the OFN\_ALLOWMULTISELECT flag, and the result should be a string  
> containing all the files.  
> The windows doc says it could be NULL separated or space separated so  
> you'll need to play a bit to figure out  
> how to parse out the individual file names.
> 
> This is all based on reading, so while in theory its correct, in  
> practice you'll have to figure some stuff out 😉
> 
> cheers  
> Chris

I'm also trying to figure this out. The best I have so far is:

OFN\_EXPLORER = 0x00080000  
OFN\_ALLOWMULTISELECT = 0x00000200  
#(...)lots of other constants

require 'swin'

filetype\_filter = [['Ruby files (\*.rb)','\*.rb'],  
['All files (\*.\*)', '\*.\*']]  
paths = SWin::CommonDialog::openFilename(  
nil,  
filetype\_filter,  
OFN\_ALLOWMULTISELECT,  
'Choose one or more files.')  
puts paths

Which looks like a horrible blast from the past: windows 3.1!  
It works, but the result looks like this:  
"D:\\projects\\test\\ autoit\_test.rb binomial.rb"

And what if the filename contains a space? Ah yes, of course, short  
filenames.  
"D:\\projects\\test\\ blum\_blum\_shub.rb BOYGIR~1.RB"  
Another try:

paths = SWin::CommonDialog::openFilename(  
nil,  
filetype\_filter,  
OFN\_EXPLORER| #\<--let's try this one  
OFN\_ALLOWMULTISELECT,  
'Choose one or more files.')  
puts paths

This looks better, but it doesn't work. When multiple files are selected  
I only get the directory back, none of the files.  
Help appreciated,

Siep

> **···**
>
> > On Feb 11, 10:32�am, Alex DeCaria \<alex.deca...@millersville.edu\> \> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [13 February 2010 02:55 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/4 "2010-02-13T02:55:06Z")

</div>

I was able to get your code to work, surprisingly the docs seem to  
actually be correct 😉  
Here is your (Siep) code expanded a bit:  
require 'swin'  
OFN\_ALLOWMULTISELECT = 0x200  
OFN\_EXPLORER = 0x80000  
NUL = "\x00"  
filetype\_filter = [['Ruby files (\*.rb)','\*.rb'],['All files (\*.\*)',  
'\*.\*']]  
paths = SWin::CommonDialog::openFilename( nil, filetype\_filter,  
OFN\_EXPLORER|OFN\_ALLOWMULTISELECT, 'Choose one or more files.')  
if paths  
&nbsp;&nbsp;files = paths.split(NUL)  
&nbsp;&nbsp;if files.length \> 1  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Found #{files.length-1} files in #{files[0]}"  
&nbsp;&nbsp;&nbsp;&nbsp;files[1..-1].each\_with\_index{|el,i| puts "#{i} =\> #{el}"}  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;d,f = File.split(files[0])  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Single file: #{f} in #{d}"  
&nbsp;&nbsp;end  
else  
&nbsp;&nbsp;puts "No selction made"  
end

using OFN\_EXPLORER returns the list of files as a string will NUL  
separating the file names, allowing file names to contain spaces. The  
directory is the first item in this case  
If a single file is selected, the result is a single string with the  
full path and file name.  
Selecting cancel in the dialog results in a nul return value.

I have to thank Louis Laverna for his work on the new Ruby installer,  
and epecially the dev kit that allowed me to build and install swin to  
play with this

Cheers

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [13 February 2010 11:34 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/5 "2010-02-13T11:34:57Z")

</div>

Chris Hulan wrote:

> I was able to get your code to work, surprisingly the docs seem to  
> actually be correct 😉  
> Here is your (Siep) code expanded a bit:  
> require 'swin'  
> OFN\_ALLOWMULTISELECT = 0x200  
> OFN\_EXPLORER = 0x80000  
> NUL = "\x00"  
> filetype\_filter = [['Ruby files (\*.rb)','\*.rb'],['All files (\*.\*)',  
> '\*.\*']]  
> paths = SWin::CommonDialog::openFilename( nil, filetype\_filter,  
> OFN\_EXPLORER|OFN\_ALLOWMULTISELECT, 'Choose one or more files.')  
> if paths  
> &nbsp;&nbsp;files = paths.split(NUL)  
> &nbsp;&nbsp;if files.length \> 1  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Found #{files.length-1} files in #{files[0]}"  
> &nbsp;&nbsp;&nbsp;&nbsp;files[1..-1].each\_with\_index{|el,i| puts "#{i} =\> #{el}"}  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;d,f = File.split(files[0])  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Single file: #{f} in #{d}"  
> &nbsp;&nbsp;end  
> else  
> &nbsp;&nbsp;puts "No selction made"  
> end
> 
> using OFN\_EXPLORER returns the list of files as a string will NUL  
> separating the file names, allowing file names to contain spaces. The  
> directory is the first item in this case  
> If a single file is selected, the result is a single string with the  
> full path and file name.  
> Selecting cancel in the dialog results in a nul return value.
> 
> I have to thank Louis Laverna for his work on the new Ruby installer,  
> and epecially the dev kit that allowed me to build and install swin to  
> play with this
> 
> Cheers

Chris: Thanks for your efforts. I copied and pasted your code, but no  
matter how many files I select, files.length is coming out to be 1.  
What happens is:

1) If I select a single file, then files contains the entire filename  
including the path.

2) If I select multiple files, then files just contains the path, with  
no filename.

The .split(NUL) method on paths doesn't seem to work for me.

I am using Windows7 Professional (64-bit). I don't know if this is  
caused by a difference in OS?

--Alex

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [13 February 2010 18:33 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/6 "2010-02-13T18:33:18Z")

</div>

I installed the latest Ruby 1.9 via [RubyInstaller.org](http://RubyInstaller.org)  
Installed the devkit from there as well  
Downloaded swin (vrswin090207.zip) and built/installed it (Yeah!  
devkit)  
Ran it on a Win7 Home 64bit and it was good

How are you getting swin?

Are you running in an editor/IDE or from the command line?  
It worked from me both from the command line and from the Komodo Edit  
5.0 editor

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [13 February 2010 19:46 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/7 "2010-02-13T19:46:21Z")

</div>

Chris Hulan wrote:

> I installed the latest Ruby 1.9 via RubyInstaller.org  
> Installed the devkit from there as well  
> Downloaded swin (vrswin090207.zip) and built/installed it (Yeah!  
> devkit)  
> Ran it on a Win7 Home 64bit and it was good
> 
> How are you getting swin?
> 
> Are you running in an editor/IDE or from the command line?  
> It worked from me both from the command line and from the Komodo Edit  
> 5.0 editor

I'm running Ruby v.1.8.6 that I installed using the Windows 1-click  
installer. I'm using whatever version of SWIN came with that install  
package. I'll try to update SWIN and try again. Maybe I'll even  
upgrade to Ruby 1.9.

Thanks. --Alex

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [14 February 2010 18:52 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/8 "2010-02-14T18:52:22Z")

</div>

Chris Hulan wrote:

> I installed the latest Ruby 1.9 via RubyInstaller.org  
> Installed the devkit from there as well  
> Downloaded swin (vrswin090207.zip) and built/installed it (Yeah!  
> devkit)  
> Ran it on a Win7 Home 64bit and it was good
> 
> How are you getting swin?
> 
> Are you running in an editor/IDE or from the command line?  
> It worked from me both from the command line and from the Komodo Edit  
> 5.0 editor

Chris: I downloaded and unzipped vrswin090207. I also downloaded and  
unzipped the DevKit into my C:\Ruby directory. I don't know what to do  
next (I've never used DevKit. Can you tell me what to do next inorder  
to build and install SWIN?  
--Alex

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [13 February 2010 21:03 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/9 "2010-02-13T21:03:27Z")

</div>

Alex DeCaria wrote:

> Chris Hulan wrote:
> 
> > I installed the latest Ruby 1.9 via RubyInstaller.org  
> > Installed the devkit from there as well  
> > Downloaded swin (vrswin090207.zip) and built/installed it (Yeah!  
> > devkit)  
> > Ran it on a Win7 Home 64bit and it was good
> > 
> > How are you getting swin?
> > 
> > Are you running in an editor/IDE or from the command line?  
> > It worked from me both from the command line and from the Komodo Edit  
> > 5.0 editor
> 
> I'm running Ruby v.1.8.6 that I installed using the Windows 1-click  
> installer. I'm using whatever version of SWIN came with that install  
> package. I'll try to update SWIN and try again. Maybe I'll even  
> upgrade to Ruby 1.9.
> 
> Thanks. --Alex

I forgot to mention that I'm running it from the Windows command line.  
Also, I just tried it on a different system with Windows XP Professional  
and got the same result (files.length = 1) regardless of how many files  
I select.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [17 February 2010 00:10 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/10 "2010-02-17T00:10:14Z")

</div>

I followed the instructions in install.txt and it Just Worked!

Well the compile threw a bunch of warnings and the ended with a  
message  
about an error... but I was able to run the code for the dialog so I  
think you  
can ignore that

cheers

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [17 February 2010 19:44 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/11 "2010-02-17T19:44:35Z")

</div>

Chris Hulan wrote:

> I followed the instructions in install.txt and it Just Worked!
> 
> Well the compile threw a bunch of warnings and the ended with a  
> message  
> about an error... but I was able to run the code for the dialog so I  
> think you  
> can ignore that
> 
> cheers

Chris:

SUCCESS! I installed Ruby 1.9 and vrswin090207, and your code works as  
advertised. Apparently it can't be done with Ruby 1.8 and/or the older  
version of vrswin.

One final question. Do you know how I can have the FileDialog open to a  
predetermined directory, instead of the directory where the program is  
started? That's the last key to my puzzle. I tried using a  
Dir.chdir(mydirectory), but that didn't seem to work.

--Alex

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [18 February 2010 15:45 UTC](https://rubytalk.org/t/can-swin-commondialog-openfilename-select-multiple-files/57374/12 "2010-02-18T15:45:05Z")

</div>

Lookig at the swin code I think the 6th argument is the initial dir,  
so try this:  
SWin::CommonDialog::openFilename( nil, filetype\_filter,  
OFN\_EXPLORER|OFN\_ALLOWMULTISELECT, 'Choose one or more files.',nil,"C:\  
\some\\path")

> **···**
>
> On Feb 17, 2:44 pm, Alex DeCaria \<alex.deca...@millersville.edu\> wrote:
> 
> > Chris Hulan wrote:  
> > \> I followed the instructions in install.txt and it Just Worked!
> > 
> > \> Well the compile threw a bunch of warnings and the ended with a  
> > \> message  
> > \> about an error... but I was able to run the code for the dialog so I  
> > \> think you  
> > \> can ignore that
> > 
> > \> cheers
> > 
> > Chris:
> > 
> > SUCCESS! I installed Ruby 1.9 and vrswin090207, and your code works as  
> > advertised. Apparently it can't be done with Ruby 1.8 and/or the older  
> > version of vrswin.
> > 
> > One final question. Do you know how I can have the FileDialog open to a  
> > predetermined directory, instead of the directory where the program is  
> > started? That's the last key to my puzzle. I tried using a  
> > Dir.chdir(mydirectory), but that didn't seem to work.
> > 
> > --Alex  
> > --  
> > Posted viahttp://www.ruby-forum.com/.
