I am using fs.unlinkSync()
method in a Node.js script, in order to remove a file located in Appdata
directory.
Best practice of locating the Appdata
dir is specifying a path using %appdata%
, so in my code:
var filePath = '%appdata%/some/path/file.ext';
fs.unlinkSync(filePath);
The problem is an error is returned, indicating bad path, because it's trying to locate something like:
C:\my\project\%appdata%\some\path\file.ext
Which obviously doesn't exist.
So I'm looking for the best method to resolve %appdata%
into C:\Users\user\AppData\Roaming
.
Hopefully I can do something along the lines of:
var filePath = resolveToAbsolutePath('%appdata%/some/path/file.ext');
fs.unlinkSync(filePath);
Any kind of help is appreciated.
Notes:
- Nope, the issue is unrelated to using forward slashes instead of backslashes.
- This is different than using environment variables, as I get the paths externally, and I need to be able to resolve % paths as well. I'm interested in generalising the solution, rather than manually replacing paths with environment variable data.