Lua - My documents path and file creation date - lua

I'm planning to do a program with Lua that will first of all read specific files
and get information from those files. So my first question is whats the "my documents" path name? I have searched a lot of places, but I'm unable to find anything. My second question is how can I use the first four letters of a file name to see which one is the newest made?
Finding the files in "my documents" then find the newest created file and read it.
The reading part shouldn't be a problem, but navigating to "my documents" and finding the newest created file in a folder.

For your first question, depends how robust you want your script to be. You could use Lua's builtin os.getenv() to get a variety of environment vars related to user, such as USERNAME, USERPROFILE, HOMEDRIVE, HOMEPATH. Example:
username = os.getenv('USERNAME')
dir = 'C:\\users\\' .. username .. '\\Documents'
For the second question, there is no builtin mechanism in Windows to have the file creation or modification timestamp as part of the filename. You could read the creation or modification timestamp, via a C extension you create or using an existing Lua library like lfs. Or you could read the contents of a folder and parse the filenames if they were named according to the pattern you mention. Again there is nothing built into Lua to do this, you would either use os.execute() or lfs or, again, your own C extension module, or combinations of these.

Related

Parsing logs in Zabbix

i'm try to parse logs from windows folder in Zabbix, but everyday creates a new directory like "2022_03_15" and log files in her, how parse a new name directory?
log["C:\Windows\Temp\app\web\0\Log\YYYY_MM_DD\Application.log"]
The logrt item would come closest but reading https://www.zabbix.com/documentation/current/en/manual/config/items/itemtypes/zabbix_agent#supported-item-keys
It notes:
file_regexp - absolute path to file and the file name described by a regular expression. Note that only the file name is a regular expression
So, sadly, this is not possible yet.
You could try to work around this by first using a list to find the latest directory and use lld to create a new log item when that is needed. For these cases it is a pity that the file and location are part of the item key.

What is the recommended way to make & load a library?

I want to make a small "library" to be used by my future maxima scripts, but I am not quite sure on how to proceed (I use wxMaxima). Maxima's documentation covers the save(), load() and loadFile() functions, yet does not provide examples. Therefore, I am not sure whether I am using the proper/best way or not. My current solution, which is based on this post, stores my library in the *.lisp format.
As a simple example, let's say that my library defines the cosSin(x) function. I open a new session and define this function as
(%i0) cosSin(x) := cos(x) * sin(x);
I then save it to a lisp file located in the /tmp/ directory.
(%i1) save("/tmp/lib.lisp");
I then open a new instance of maxima and load the library
(%i0) loadfile("/tmp/lib.lisp");
The cosSin(x) is now defined and can be called
(%i1) cosSin(%pi/4)
(%o1) 1/2
However, I noticed that a substantial number of the libraries shipped with maxima are of *.mac format: the /usr/share/maxima/5.37.2/share/ directory contains 428 *.mac files and 516 *.lisp files. Is it a better format? How would I generate such files?
More generally, what are the different ways a library can be saved and loaded? What is the recommended approach?
Usually people put the functions they need in a file name something.mac and then load("something.mac"); loads the functions into Maxima.
A file can contain any number of functions. A file can load other files, so if you have somethingA.mac and somethingB.mac, then you can have another file that just says load("somethingA.mac"); load("somethingB.mac");.
One can also create Lisp files and load them too, but it is not required to write functions in Lisp.
Unless you are specifically interested in writing Lisp functions, my advice is to write your functions in the Maxima language and put them in a file, using an ordinary text editor. Also, I recommend that you don't use save to save the functions to a file as Lisp code; just type the functions into a file, as Maxima code, with a plain text editor.
Take a look at the files in share to get a feeling for how other people have gone about it. I am looking right now at share/contrib/ggf.mac and I see it has a lengthy comment header describing its purpose -- such comments are always a good idea.
For principiants, like me,
Menu Edit:configure:Startup commands
Copy all the functions you have verified in the first box (this will write your wxmaxima-init.mac in the location indicated below)
Restart Wxmaxima.
Now you can access the functions whitout any load() command

How to create and load a configuration file in dxl

I have a script which saves some files at a given location. It works fine but when I send this code to someone else, he has to change the paths in the code. It's not comfortable for someone who does not know what is in that code and for me to explain every time where and how the code should be changed.
I want to get this path in a variable which will be taken from the configuration file. So it will be easier for everyone to change just this config file and nothing in my code. But I have never done this before and could not find any information on how I can do this in the internet.
PS: I do not have any code and I ask about an ultimate solution but it is really difficult to find something good in the internet about dxl, especially since I'm new with that. Maybe someone of you already does that or has an idea how it could be done?
DXL has a perm to read the complete context of a file into a variable: string readFile (string) (or Buffer readFile (string))
you can split the output by \n and then use regular expressions to find all lines that match the pattern
^\s*([^;#].*)\s*=\s*(.*)\s*$
(i.e. key = value - where comment lines start with ; or #)
But in DOORS I prefer using DOORS modules as configuration modules. Object Heading can be the key, Object Text can be the value.
Hardcode the full name of the configuration module into your DXL file and the user can modify the behaviour of the application.
The advantage over a file is that you need not make assumptions on where the config file is to be stored on the file system.
It really depends on your situation. You are going to need to be a little more specific about what you mean by "they need to change the paths in the code". What are these paths to? Are they DOORS module paths, are they paths to local/network files, or are the something else entirely?
Like user3329561 said, you COULD use a DOORS module as a configuration file. I wouldn't recommend it though, simply because that is not what DOORS modules were designed for. DOORS is fully capable of reading system files in one line at a time as well as all at once, but I can't recommend that option either until I know what types of paths you want to load and why.
I suspect that there is a better solution for your problem that will present itself once more information is provided.
I had the same problem, I needed to specify the path of my configuration file used in my dxl script.
I solved this issue passing the directory path as a parameter to DOORS.exe as follow:
"...\DOORS\9.3\bin\doors.exe" -dxl "string myVar = \"Hello Word\"
then in my dxl script, the variable myVar is a global variable.

How to get the "Created by <name> on <date>" text

So the question is actually simple, but I have no idea how to approach this issue. I know this code is generated by template based on this question:
XCode automatically generated comments?
I want to use the <name> that xcode provides on each mac machine which is unique for it's user, for some types of logs.
EDIT:
This is how the swift template file looks before it's used by Xcode to create my work file:
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
Surely, there is no point in parsing it.
The question is: Does anyone knows how I can get this name using swift in my application?
I searched for an answer here/Google but so far no luck.
I don't know how to read the header. But you can do it otherwise.
First if you need the creation-date of a file, you can use the NSFileManager:
var path = "path/to/your/file/"
var fileAttribs:NSDictionary = NSFileManager.defaultManager().attributesOfFileSystemForPath(path, error: nil)!
var creationDate = fileAttribs.objectForKey(NSFileCreationDate)
Also if you need the full username, you can use the function NSFullUserName() or NSUserName(). It should return the same string as __FULLUSERNAME__
var fullUsername = NSFullUserName()
var username = NSUserName()
Sometimes in the iOS Simulator, this username is empty, but in a real app, it should work properly.
That text written at template instantiation time — that is, when you create a new Xcode project (or a new file in an existing project using the File > New > File... templates). You can't read the contents of the source file your code was compiled from. (Well, unless you ship that file along with your compiled binary, and read it in like any other text file.)
But that's just text substitution — it can be done anywhere in the file, not just in the comment headers. So you could create your own file or project templates, and in the template files, put those substitution macros in code instead of in comments:
let schmoeWhoCreatedThisFile = "___FULLUSERNAME___"
Here's a tutorial found in a couple seconds of web searching that has the full details on creating templates and the substitution macros you can use in them.
Remember, substitution happens when you create a new file or project — if you're looking for who made the latest change to your source file or who built the app that shipped to your customers, you're barking up the wrong tree. Some of those sorts of things you can do with source control; others are more a matter of (human-defined, human-executed) policy for you or or your organization.

Difference between App.Path and using a ShellObject.Self.Path

I am converting a VB6 application to VB.NET. In the code I am converting, it seems that the developer found the applications path two separate ways. However, it appears that he expects the two methods to produce different results.
Simple question:
What is the difference between calling these two lines of code:
strAppDataPath = CreateObject("Shell.Application").
NameSpace(ssfLOCALAPPDATA).Self.Path
and
strAppDataPath = App.Path
Explanation:
This is the code in question:
strAppDataPath = CreateObject("Shell.Application").
NameSpace(ssfLOCALAPPDATA).Self.Path
strAppDataPath = strAppDataPath & "\DataFolder\"
If (Not objFileSystem.FileExists(strAppDataPath & strAppDataFile)) Then
If (objFileSystem.FileExists(App.Path & strAppDataFile)) Then
...
End If
End If
The application's path is appended with \DataFolder\, and stored in the String strAppDatapath.
We check if the file strAppDataFile does not exist in strAppDatapath. Followed by checking if the file does exist in App.Path.
The concept behind what is going on makes sense to me: If the file doesn't exist in the subfolder, and if the file exists in App.Path, then do .... What I do not understand is why they didn't use one of the method for finding the application's path exclusively.
They don't point to the same path.
App.Path: Path where the currently executing EXE/DLL resides.
CreateObject("Shell.Application").NameSpace(ssfLOCALAPPDATA).Self.Path: Will point to the local (non-roaming) app data user folder. Same as the value in (user) environment variable LOCALAPPDATA.
Since Windows XP, the roaming & local app data paths are preferred places to store user settings and files (instead of the probably rights-restricted app folder).
So what the code does, is:
Look for strAppDataFile in %LOCALAPPDATA%\DataFolder\ (e.g. where "%LOCALAPPDATA%" could point to "C:\Users\UserName\AppData\Local\" on Windows Vista/7)
If not found, look for the same file in the application folder.
Please note that preferably applications should create an application specific sub-directory in local/roaming app data, e.g. use something like:
strAppDataPath = strAppDataPath & "\" & App.ProductName & "\DataFolder\"
' NOTE: Make sure to set the "Product Name" entry in the version information
' of the project settings
that could resolve to something like
"C:\Users\UserName\AppData\Local\My Application\DataFolder\"
My guess is that when the program is installed per-machine, a per-user location under LocalAppData is used. However during development or when installed per-user or as a portable application the application folder is used.
Some programmers use something like the code in question, while others make the decision by comparing App.Path against the ssfPROGRAMFILES path to determine the environment. The latter is probably preferable for a number of reasons but in the degenerate case where only one instance of the program exists on a machine they're equivalent.

Resources