How to determine if file system path is directory or file? - node.js

I'm looking to determine if a file system path is directory or file. I'm not looking to checking for the type an existing path. I'm trying to determine if the path function argument string is a referring to a directory or file.
How do I make a distinction between a file and a directory when this:
/Users/thomas/Desktop/node
The following path could refer to a directory node, or a file node without an extension.
I was thinking about using a trailing / to connote directory.
So this would mean a directory:
/Users/thomas/Desktop/node/
And this would mean a file:
/Users/thomas/Desktop/node
However node's path methods like .resolve() and .join() do not take into consideration the trailing / and always remove it. So is this good practice?

There is no way to check if an arbitrary string is a directory or file if it does not exist.
However for existing paths, you can use fs.stat() on the path, which will give you an object that has methods for checking the path type (e.g. isDirectory(), isFile(), etc).

Related

Python - working with unkown directory name versions

I'm using python to download an application from a content distribution network. The application downloads as self extract file cabinet. When executed it creates a directory using a version naming format. For example app-version2/../app.exe, Thus I cannot rely on the folder name as it may change in the future. I'm trying to find the best way to work with the content inside the folder without depending on the actual folder name.
My idea was to rename the folder using os.listdir() and then os.rename(app-version2, myapp) This would work but is not automated. What would be the best automated method to find a folder name that contains version numbers and change that to something more static?
Assuming you want to find the path of the directory which begins with app, you can accomplish this using pathlib.Path:
from pathlib import Path
app_path = next(Path().glob('app*'))
This will give you the path to the first file or directory in your current directory whose name begins with "app".

MATLAB, Octave: working with folder names that have space in them

In MATLAB, actually Octave, I would like to find a list of all subfolders in the current folder so I use this:
subFolder = dir;
This gives the list of all subfolders in the current folder. This returns a structure whose one element is the name. Assume I have two subfolders with names subfolder 1A and subfolder 1B.
Now I want to go to these folders. Then I do this:
cd subFolder(1).name
But I get this error:
error: subFolder(1).name: No such file or directory
If I do this:
cd "subfolder 1A"
everything works fine. What is the solution?
The space in the folder name is a red herring in this case. It's not the source of the problem. The actual issue is that you need to call the cd function using function syntax instead of command syntax (i.e. use parentheses; related question here):
cd(subFolder(1).name);
When you use the command syntax, subFolder(1).name is itself being treated as the string argument to cd (i.e. it's looking for a folder called 'subFolder(1).name'). With the function syntax, the string contained within the structure array field is used as the argument.
To make your code a little more robust, you could also use the 'folder' field returned by dir:
cd(fullfile(subFolder(1).folder, subFolder(1).name));
This will go to the desired folder regardless of the directory you are currently in, since it specifies an absolute path instead of a partial path (which is relative to the current directory).

How Can I Specify a Directory without Using the Full Directory Name? - Python 3.4

I don't want to specify the full directory of a folder or object within my program. I do not want to do this because if a user decides to change the installation folder, it will not function properly. I've seen in HTML you can do something like: ./folder/directory/name and it would work perfectly fine. Is there a way to do something like that within Python?
From https://docs.python.org/3/reference/datamodel.html
__file__ is the pathname of the file from which the module was loaded
You may find it helpful to apply os.path.abspath() to '.' or __file__.

Resolve filesystem paths (possibly with symlinks) with Node.js

I need a way to verify whether or not a file is under a specific path. If the path is /var/www and the file is /var/www/something/something-else/file, I need to return true. If the file is /var/www/../../etc/passwd, I need to return false.
In PHP land, I usually use a function called realpath():
realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and returns the canonicalized absolute pathname.
I run realpath() on both the desired path and the full file path, then determine if the desired path is a substring of the result of the fully resolved file path. If it is, I return true.
Is there a function for Node.js that achieves the same goal? path.resolve() gets me half way, but doesn't handle any symlinks that are actually in the filesystem. fs.readlink() has absolutely no documentation at all, and may not directly apply since I won't always have symlinks.
Must I loop through each part of the path, resolving symlinks as I go, or is there a more canonical method?
Node also has fs.realpath() so that should work the same way as in PHP.

what is the right format of -adaptresourcefilenames in ProGuard

I have an application JAR file I would like to obfuscate using ProGuard. It contains "AppName\resources" folder and several "ClassName.properties" files in it.
What is the right format of -adaptresourcefilenames option in my config file to adapt the properties files to classes?
The option -adaptresourcefilenames expects the resource files names to start at the same root directory level as the class names. E.g., it can rename mypackage/MyClass.properties (without any directory prefix) corresponding to
the obfuscated name of mypackage/MyClass.class (also without any directory prefix).
In this case, you could work on the unzipped application, specifying the input and output directories so the relative paths correspond again:
-injars input(!AppName/resources/**)
-outjars output
-injars input/AppName/resources
-outjars output/AppName/resources

Resources