View Single Post
12/25/23, 05:05 PM   #2
vsrs_au
Join Date: Dec 2022
Posts: 18
[ I thought this would be a useful addendum to Baertram's SavedVariables post, but if I posted this in the wrong location, my apologies and feel free to move it. ]

Hopefully someone will find this useful? I'm working on an addon, and part of the addon is to convert information in the SavedVariables file for the addon to .CSV format, so it can be imported into a spreadsheet and then analysed/graphed/whatever. For the conversion, I wrote a LUA script, and the first thing it needs to do is to find the user's documents (aka 'personal') directory in Windows, so this is what this thread post describes, how to get this directory without making any assumptions about where it actually is on each user's PC.

There are 2 ways to do this (that I know of, anyway):
  1. Use an environment variable. The following LUA code will determine the directory location using the 'USERNAME' environment variable:
    Code:
    dirDocuments = 'C:\\Users\\' .. os.getenv('USERNAME') .. '\\Documents'
    This isn't perfect, though, because on some systems (it occurred on mine), the user may have been renamed, and dirDocuments will be set to a path including the user's old name, not the new. Fortunately, there's another option (see next bullet point).
  2. Get the documents directory from the Windows registry. This is more complicated, involving (a) writing a batch file to run a registry query command, then (b) running the batch file in LUA and retrieving its output. The batch file would be:
    Code:
    @echo off
    for /f "tokens=3*" %%p in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal') do ( echo %%p %%q )
    Then, to run the batch file in LUA we use:
    Code:
    local openPop = assert(io.popen('C:\\Windows\\System32\\cmd.exe /c "D:\\_script\\findDoc.bat"', 'r'))
    local dirDocuments = openPop:read('*all')
    openPop:close()
    This example is taken from my PC, so the batch file path will change of course. This produces a similar result to the first option, but finds the correct documents directory even if the user name was changed at some point. The batch file need not already exist, because the LUA code could create it dynamically, first by getting the TEMP directory using os.getenv('TEMP'), then by writing the abovementioned batch file contents to a .bat file in that directory.

Having retrieved the user's documents directory in Windows, we can then read any SavedVariables file using code similar to:
Code:
local openPop = assert(io.popen('C:\\Windows\\System32\\cmd.exe /c "D:\\_script\\findDoc.bat"', 'r'))
local dirDocuments = openPop:read('*all')
openPop:close()

dofile(dirDocuments .. '/Elder Scrolls Online/live/SavedVariables/MyAddon.lua')

.....

<<process the file contents as you wish>>

Last edited by vsrs_au : 01/01/24 at 08:30 PM.
  Reply With Quote