How to trim the file extensions from a filename - powershell

I have a foreach loop that gets a list of objects in Folder4 after trimming the full path where the objects reside.
Here is sample code:
$row.Path = $path.InnerText.Replace("/Folder1/Folder2/folder3/folder4/","")
Sample Output:
usp_StoredProcedurename.prc,
fn_FunctionName.udf
File.sql
The last thing I need to do is to remove any extension, ie .prc, .pdf, .udf, .sql, etc
Here is the coplete for each:

You are probably looking for the static GetFileNameWithoutExtension method. To use it, you have to pass a single file or path to it:
[System.Io.Path]::GetFileNameWithoutExtension("usp_StoredProcedurename.prc")
Depending on the actual output of $row.Path you could split the path and join them back later if you want.
Alternative, you could use a regex to remove the file extensions for alle files within your string at once:
$row.Path -replace '\..*'
Be aware that regex will remove everything after a dot.

Related

Run for-loop only if there is at least one json file

I want to iterate over all json files in a specific subdirectory.
#!/bin/sh
source_dir="nodes"
for file_path in $source_dir/*.json;
do
file_name=$(basename $file_path .${file_path##*.})
echo $file_name
done
My code is working as expected if there is at least one json file in the directory.
If there is no json file in the directory, the loop will still be executed. The file_name is then "*".
How do I have to change the for loop so that it is only executed if there is at least one json file in the directory?
you can wrap you loop in an if clause to check if the pattern matches anything.
Check this SO question on how to do this: Check if a file exists with wildcard in shell script

Loop through files with specific extension

I need to open many files in a loop, with the same extension.
Example file names are: c1_p1_t_r.mat,c1_p3_t_r.mat,c1_p6_t_r.mat,c1_p7_t_r.mat,c1_p10_t_r.mat,etc.
So basically, the first and last part of the file names are the same, but something in the middle changes.
I tried with:
Ext = 'c1_*t_r*.mat';
files = dir(Ext);
but it doesn't work. Any suggestion would be greatly appreciated.
Looking at the file names you shared you should use c1*t_r.mat rather than c1*t_r*.mat
Use files = dir('*.Ext'); You need the apostrophes to pass it as a string and the asterisk as the wildcard for file names. I think passing multiple asterisks here is the problem. You might resort to creating the variable name as a full string in case they are as similar though:
for ii = 1:NumberOfFiles
filename = sprintf('c1_p%dt_r.mat',ii);
%//load file with created name
end

Saving strings from the input .txt filename - MATLAB

Via textscan, I am reading a number of .txt files:
fid1 = fopen('Ev_An_OM2_l5_5000.txt','r');
This is a simplification as in reality I am loading several hundred .txt files via:
files = dir('Ev_An*.txt');
Important information not present within the .txt files themselves are instead part of the filename.
Is there a way to concisely extract portions of the filename and save them as strings/numbers? For example saving 'OM2' and '5000' from the above filename as variables.
fileparts appears to require the full path of the file rather than just defaulting to the MATLAB folder as with textscan.
It depends on how fixed your filename is. If your filename is in the string filename, then you can use regexp to extract parts of your filename, like so:
filename = 'Ev_An_OM2_l5_5000.txt'; %or whatever
parts = regexp(filename,'[^_]+_[^_]+_([^_]+)_[^_]+_([^\.]+)\.txt','tokens');
This will give you parts{1}=='OM2' and parts{2}=='5000', assuming that your filename is always in the form of
something_something_somethingofinterest_something_somethingofinterest.txt
Update:
If you like structs more than cells, then you can name your tokens like so:
parts = regexp(filename,'[^_]+_[^_]+_(?<first>[^_]+)_[^_]+_(?<second>[^\.]+)\.txt','names');
in which case parts.first=='OM2' and parts.second=='5000'. You can obviously name your tokens according to their actual meaning, since they are important. You just have to change first and second accordingly in the code above.
Update2:
If you use dir to get your filenames, you should have a struct array with loads of unnecessary information. If you really just need the file names, I'd use a for loop like so:
files = dir('Ev_An*.txt');
for i=1:length(files)
filename=files(i).name;
parts = regexp(filename,'[^_]+_[^_]+_(?<first>[^_]+)_[^_]+_(?<second>[^\.]+)\.txt','tokens');
%here do what you want with parts.first, parts.second and the file itself
end

How to simply extract leading N parts of a path?

I've got a bunch of directory names and file names, some are absolute path, some are relative path. I just wish to get the 2 leading parts of each path. Input:
D:\a\b\c\d.txt\
c:\a
\my\desk\n.txt
you\their\mine
I expect to get:
D:\a
c:\a
\my\desk
you\their
Is there a convenient way in PowerShell to achieve this?
You can sometimes get your hand slapped for suggesting string manipulation as it can sometimes be "unreliable". However your test data contains 3 different possibilities. Also, never seen someone looking for the first parts from a path.
I present a simple solution the nets your desired output as you have it in your question
"D:\a\b\c\d.txt\","c:\a","\my\desk\n.txt","you\their\mine" | ForEach-Object{
($_ -split "(?<=\S)\\")[0..1] -join "\"
}
I needed to use a lookbehind since your sample output contains a leading a leading slash that you wanted to retain. It splits every string on slashes that have a non white-space character in front of them.
This would not return the correct path for UNC's. Split-Path would be the obvious choice if you only wanted a single portion of the path. I suppose you could nest the call to get 2 but at this time I am unable to find a simple way to account for all of your examples with the same logic.

Store user input as wildcard

I am having some trouble with a data processing function in MATLAB. The function takes the name of the file to be processed as an input, finds the desired files, and reads in the data.
However, several of the desired files are variants, such as Data_00.dat, Data.dat, or Data_1_March.dat. Within my function, I would like to search for all files containing Data and condense them into one usable file for processing.
To solve this, I would like desiredfile to be converted into a wildcard.
Here is the statement I would like to use.
selectedfiles = dir *desiredfile*.dat % Search for file names containing desiredfile
This returns all files containing the variable name desiredfile, rather than the user input.
The only solution that I can think of is writing a separate function that manually condenses all the variants into one file before my function is run, but I am trying to keep the number of files used down and would like to avoid this.
You could concatenate strings for that. Considering desiredFile as a variable.
desiredFile = input('Files: ');
selectedfiles = dir(['*' desiredfile '*.dat']) % Search for file names containing desiredfile
Enclosing strings between square brackets [string1 string2 ... stringN]concatenates them. Matlab's dir function receives a string.
I believe you can achieve that using the dir command.
dataSets = dir('/path/to/dir/containing/Data*.dat');
dataSets = {dataSets.name};
Now simply loop over them, more information here.
To quote the matlab help:
dir lists the files and folders in the MATLABĀ® current folder. Results appear in the order returned by the operating system.
dir name lists the files and folders that match the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. You can use wildcards (*).