While passing address in nodejs, when to place '/' between directories and when to place '\\' . And why do we place dot(.) before writing addresses - node.js

While passing address in nodejs, when to place '/' between directories and when to place '\' . And why do we place dot(.) before writing addresses

path package provides functionality so that you don't need to worry about what directory separator you need to use.
const path = require('path');
path.join(parentDir, childDir);
And dot represents current directory i.e.
project index file path: /home/user/projects/project-name/index.js
you are in <project-name> folder
so index file path will be ./index.js

In a Unix/Linux environments you place a forward slash as a folder seperator.
e.g. /home/user/image.jpg
In a Windows system you use backslashes as folder seperators, since a backslash is also an escape character in a string you should escape the backslash.
e.g. C:\users\user\image.jpg
You use a . for relative paths.
e.g. if your script runs in /home/user/ and you want to access image.jpg you can do so by ./image.jpg
for more info: https://nodejs.org/api/path.html

Related

Bash script - split String in two variables

In a Bash script I want to split a string into two other strings based on the last "/" it contains.
In a situation where the given string is "Example/Folder/Structure", I would like to create two other strings with the following values:
string 1 = "Example/Folder"
string 2 = "Structure"
I'm trying to create a script to get a slather coverage report for a given folder in an iOS app. Although I have minimal knowledge of Bash, I was able to get it to work when the given folder is located in the root of the project. Now I want to make the script able to handle paths so that I can get the report also for subfolders, and for that I need to differentiate the desired folder from the rest of the path.
basename(1), dirname(1):
path=a/b/c
basename=$(basename "$path") # c
dirname=$(dirname "$path") # a/b
Prefix/suffix removal:
path=a/b/c
basename=${path##*/} # c
dirname=${path%/*} # a/b
Prefix/suffix removal is sufficient in some circumstances, and faster because it's native shell.
dirname/basename commands are slower (especially many paths in a loop etc) but handle more variable input or directory depth.
Eg. dirname "file" prints ., but suffix removal would print file. dirname /dir prints /, but suffix removal prints empty string; dirname also handles contiguous slashes (dirname a//b); basename a/b/ prints b, but prefix removal prints empty string.
If you know the structure is always 3 slashes (a/b/c), it may be safe to use prefix/suffix removal. But here I would use basename and dirname.
Also think about whether a better approach is to change the working directory with cd, so you can just refer to current directory with . (there's also $PWD and $OLDPWD).

res.downalod changing the name of .tiff file while downloading if fileName contain forward slash

I am using express res.download to download the file from the server location as below-
res.download('path', 'report a/b/c.tiff')
on downloading above file name is getting changed to c.tiff.
what will be the possible reason and how to get downloaded file as report a/b/c.tiff only.
/ is a globally user for differentiating directories hence forth OS will not allow / in file names as well as for directory.
In Windows you will get the below error,
likewise linux OS also uses the same. But we can use '\' for creating files. Allowed chars in Linux,
Double your \, like this: \, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, *
Hence you download has only c.tff

How to avoid trailing space being replaced with %20 when downloading a file with WinSACP

I use WinSCP script to download a file from an SFTP site. The file has length of trailing space after the file name.
After it is downloaded to local computer %20 is appended to the file name. For example, file.txt_________ becomes file.txt________%20 on local computer (_ stands for space).
Could you know any command I can put on the WinSCP script to remove it. Thank you in advance.
Add -rawtransfersettings ReplaceInvalidChars=0 to your get command:
get file directory\ -rawtransfersettings ReplaceInvalidChars=0
Read about raw transfer options.
You will lose the trailing spaces though, this way.

How to shorten the length of the path on the one folder in Matlab?

How to shorten the length of the path on the one folder in Matlab?
i.e. I want one directory up.
For example I have 'C:/mydir/folder1/folder2' I want 'C:/mydir/folder1'
If you have the folder path in a string, you can use the function fileparts:
currentFolder = pwd;
parentFolder = fileparts(currentFolder);
Note that this won't work if the folder path string ends in a file separator character (i.e. '/' or '\').
If you simply want to change to the parent directory of the current working directory, use cd:
cd ..
% or
cd('..')
We could also use Java builtin functions:
char(java.io.File(pwd).getParent())
also the Apache Commons IO library that ships with MATLAB:
char(org.apache.commons.io.FilenameUtils.getFullPath(pwd))

Batch search and replace file path

So I've got lots of m3u playlists created on my Windows machine with file path as
D:\blabla
So first I want to replace all the '\' with '/' and then D: with /media/etcetc.
How do I do that using sed or some other tool to every playlist in folder?
First it changes the root path. Then changes the \ to /.
sed 's/D:/\/media\/etcetc/;s/\\/\//' /path/to/file.m3u

Resources