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

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))

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).

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

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

Does the following program access a file in a subfolder of a folder?

using
import sys
folder = sys.argv[1]
for i in folder:
for file in i:
if file == "test.txt":
print (file)
would this access a file in the folder of a subfolder? For Example 1 main folder, with 20 subfolders, and each subfolder has 35 files. I want to pass the folder in commandline and access the first subfolder and the second file in it
Neither. This doesn't look at files or folders.
sys.argv[1] is just a string. i is the characters of that string. for file in i shouldn't work because you cannot iterate a character.
Maybe you want to glob or walk a directory instead?
Here's a short example using the os.walk method.
import os
import sys
input_path = sys.argv[1]
filters = ["test.txt"]
print(f"Searching input path '{input_path}' for matches in {filters}...")
for root, dirs, files in os.walk(input_path):
for file in files:
if file in filters:
print("Found a match!")
match_path = os.path.join(root, file)
print(f"The path is: {match_path}")
If the above file was named file_finder.py, and you wanted to search the directory my_folder, you would call python file_finder.py my_folder from the command line. Note that if my_folder is not in the same directory as file_finder.py, then you have to provide the full path.
No, this won't work, because folder will be a string, so you'll be iterating through the characters of the string. You could use the os module (e.g., the os.listdir() method). I don't know what exactly are you passing to the script, but probably it would be easiest by passing an absolute path. Look at some other methods in the module used for path manipulation.

Unix Shell - Zip command

How do I use command zip so that i can compress specific files and other files that start with "red". They're all in the same directory
I'm trying zip myfiles bookyhs.txt sholah.txt ^"red"
having problems with the last part of the code
You need to use glob pattern instead of regex pattern to match all files starting with red in current directory:
zip myfiles bookyhs.txt sholah.txt red*

String Manipulation in Batch file

I'm running a FOR loop to retrieve the (absolute) path name for ALL *.properties file in the root folder: "C:\ExecutionSDKTest_10.2.2\". Now, I'm trying to slice the path name to only result in the file name. e.g. if absolute path is "C\ExecutionSDKTest_10.2.2\Test-101" I only want the "Test-101" part (w/out the quotes of course) I have:
FOR %%G IN (C:\ExecutionSDKTest_10.2.2\*.properties) DO
(
REM Ignore "C:\ExecutionSDKTest_10.2.2\" ??
java -jar %1 %G:> Logs\%%G.log
)
So the absolute file path name is stored in G, but I'd only like the file name. How can I achieve this goal?
You can use
%%~nG
to get just the filename, or
%%~nxG
if you want the filename and extension.

Resources