Old 05-15-2019, 02:42 AM   #1
fripholm
Human being with feelings
 
Join Date: Jan 2011
Posts: 19
Default Get file creation date from Lua reascript [SOLVED]

I'm a complete noob when it comes to scripting.

However, I'm trying to come up with a LUA script that can append the creation date to an existing file's name of a Reaper project.

This is what I got so far:

Code:
cdate = io.popen("dir /T:C " .. "\"" .. nameold .. "\"", 'r')

createdate = cdate:read("*all")

reaper.ShowConsoleMsg("\n") -- just for debugging
reaper.ShowConsoleMsg(createdate) -- just for debugging
I'm on Windows 10 and there's no need for this to be cross platform.

Let's assume that the string nameold is the path to the existing file "H:\_Chaos\Zips\test.rpp". This is figured in another part of the script which is not relevant for my question and thus not shown here.

The last command "reaper.ShowConsoleMsg(createdate)" creates the following output:

Code:
 Datentr„ger in Laufwerk H: ist Backup
 Volumeseriennummer: 5D78-6EC0

 Verzeichnis von H:\_Chaos\Zips

14.05.2019  21:31             2 758 test.rpp
               1 Datei(en),          2 758 Bytes
               0 Verzeichnis(se), 20 214 288 384 Bytes frei
So far, so good.

The first 17 characters within the 6th line of the string (or table, array, whatever???) createdate contain the information I need and need to be extracted (and formatted) somehow. Everything else can be ignored.

But how do I do it?

Last edited by fripholm; 05-15-2019 at 04:15 AM. Reason: Clarity
fripholm is offline   Reply With Quote
Old 05-15-2019, 03:42 AM   #2
fripholm
Human being with feelings
 
Join Date: Jan 2011
Posts: 19
Default

Just figured it out myself. I'm pretty sure there's more elegant ways, but this will do for the moment

Code:
cdate = io.popen("dir /T:C " .. "\"" .. nameold .. "\"", 'r')

createdate = cdate:read("*all")
pos = 0

for i = 0, 4 do
   pos = string.find(createdate, "\n", pos+1)
end

createdate = string.sub(createdate, pos, pos+17)
year = string.sub(createdate, 8, 11)
month = string.sub(createdate, 5, 6)
day = string.sub(createdate, 2, 3)
hour = string.sub(createdate, 14, 15)
minute = string.sub(createdate, 17, 18)

date = year.."-"..month.."-"..day.."_"..hour.."-"..minute

reaper.ClearConsole() -- just for debugging
reaper.ShowConsoleMsg(date) -- just for debugging
creates the following output and is exactly what I was looking for:

2019-05-14_21-31
fripholm is offline   Reply With Quote
Old 05-15-2019, 04:01 AM   #3
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 5,710
Default

Thx for sharing !



Can you rename your thread "Get file creation from Lua reascript" so that it will be indexed better for search engine in the future ? Thx!
X-Raym is offline   Reply With Quote
Old 05-15-2019, 04:16 AM   #4
fripholm
Human being with feelings
 
Join Date: Jan 2011
Posts: 19
Default

Quote:
Originally Posted by X-Raym View Post
Can you rename your thread(...)
Done!
fripholm is offline   Reply With Quote
Old 05-15-2019, 06:44 AM   #5
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 5,883
Default

Regular expressions to the rescue!

Given:
Code:
14.05.2019  21:31
You should be able to use:
Code:
local day, month, year, hour, minute = createdate():match("(\d+)\.(\d+)\.(\d+)\s+(\d+):(\d+)")
https://regex101.com/r/c4z5QM/1

(\d+) just means "find a section containing just digits and grab it for me. The bits in between specify the dots and spaces.

Note: match returns them as strings, so you can use tonumber(day), etc if you have to do math or compare them.
Lokasenna is offline   Reply With Quote
Old 05-15-2019, 09:37 AM   #6
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 1,123
Default

Thanks from me as well.

I hope, that SWS will get such functions at some point, as this would be more comfortable and crossplatform, IMHO.
__________________
Ultraschall-API - a Lua-functions-library4Reaper: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs4Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.