# OpenFileDialog box

**URL:** <https://forums.solar2d.com/t/openfiledialog-box/355204>\
**Category:** Win32 Desktop\
**Created:** [November 23, 2022, 3:36pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204 "2022-11-23T15:36:53Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [November 25, 2022, 10:20pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/21 "2022-11-25T22:20:45Z")

</div>

Well, you probably want the `allow_multiple_selects` option. Then you’ll get a table of absolute filenames back. Roughly, for each file you would want to do something like this (untested):

```auto
local original = io.open(full_path, "rb")
local contents = original:read("*a") -- copy the contents

original:close()

local pos = full_path:find("/") -- or "\\" ... you actually want the last one, so this isn't quite right
local name = full_path:sub(pos + 1) -- get just the file name itself

local new_file = io.open(system.pathForFile(name, system.DocumentsDirectory), "wb")

new_file:write(contents) -- finish the copy
new_file:close()

```

As I said, it’s a bit rough, but that’s the general idea.

---

<div class="post-metadata">

**Author:** ![steevemoore](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@steevemoore](https://forums.solar2d.com/u/steevemoore)\
**Post date:** [November 26, 2022, 3:23pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/22 "2022-11-26T15:23:51Z")

</div>

> [@StarCrunch](#):
>
> `new_file:write(contents) -- finish the copy`

refuse to write the contents

---

<div class="post-metadata">

**Author:** ![steevemoore](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@steevemoore](https://forums.solar2d.com/u/steevemoore)\
**Post date:** [November 26, 2022, 4:22pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/23 "2022-11-26T16:22:01Z")

</div>

I think the idea is good because it allows me to understand how it works.

---

<div class="post-metadata">

**Author:** ![steevemoore](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@steevemoore](https://forums.solar2d.com/u/steevemoore)\
**Post date:** [November 26, 2022, 5:03pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/24 "2022-11-26T17:03:04Z")

</div>

based on your idea i ended up doing this, even if i don’t know yet how to find the name as well as the extension of the file. I can still predefine which extension I want to copy.

```auto
         
local original = io.open(file, "rb")

local contents = original:read("*a") -- copy the contents
 original:close()

local pos = file:find("\\") -- or "\\" ... you actually want the last one, so this isn't quite right

local new_file = io.open(system.pathForFile( pos..".txt" , system.DocumentsDirectory), "wb")
new_file:write(contents) -- finish the copy
new_file:close()

```

---

<div class="post-metadata">

**Author:** ![steevemoore](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@steevemoore](https://forums.solar2d.com/u/steevemoore)\
**Post date:** [November 26, 2022, 5:13pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/25 "2022-11-26T17:13:05Z")

</div>

for those who are interested in this post can modify this line like this:

```auto

local new_file = io.open(system.pathForFile( "my_file_name"..pos..".mp4" , system.DocumentsDirectory), "wb")

```

by adding the name and the extension

---

<div class="post-metadata">

**Author:** ![steevemoore](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@steevemoore](https://forums.solar2d.com/u/steevemoore)\
**Post date:** [November 26, 2022, 5:28pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/26 "2022-11-26T17:28:55Z")

</div>

now I want to know how to keep the position of the file in memory so that each selected file finds another number like for example `(pos+1)`

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [November 26, 2022, 5:50pm UTC](https://forums.solar2d.com/t/openfiledialog-box/355204/27 "2022-11-26T17:50:05Z")

</div>

I did a little test, and it looks like you can get the trailing bits like so:

```auto
local function GetFilename (s)
  local last, i1, i2

  repeat
    last = i2
    i1, i2 = s:find("[/\\]", i2 and i2 + 1) -- find first "/" or "\\" starting at i2 + 1 (or 1, by default)
  until not i1

  return last and s:sub(last + 1)
end
  
-- Test some different cases:
print(1, GetFilename("TopLevel/MiddleLevel/LowLevel/MyFile.format"))
print(2, GetFilename("NotAFile"))
print(3, GetFilename("A/B\\CC/DDDD\\EE\\F.file"))
print(4, GetFilename("!!!!/"))

```

There are also some utilities [here](https://docs.coronalabs.com/guide/data/readWriteFiles/index.html).

You probably don’t want to use `pos` as the name since a lot of files could have the slashes at that same position.

[Previous page](https://forums.solar2d.com/t/openfiledialog-box/355204.md?page=1)
