How to Remove File Name with Extension from File Path Field in PostgreSQL - postgresql

I have an Image path Field in the table and I want to get path without filename and it's an extension.
I tried using the POSITION of the charecture way but didn't get the accurate output.
LEFT("My_Path", POSITION('\' IN REVERSE("My_Path")) - 1)
The file path is something like
\Nikunj\Images\1.png
My expected result is
Nikunj\Images\

Simply use SUBSTRING
select substring (mypath from '(^.*\\)' ) ;
DEMO

Try following example it is working correctly.
SELECT concat(SPLIT_PART('\Nikunj\Images\1.png', '\', 2),'\',SPLIT_PART('\Nikunj\Images\1.png', '\', 3),'\') as c2;

Thank you, friends, I got a solution for that and this is my solution.
concat(RTRIM(REPLACE(My_Path, reverse(substring(reverse(My_Path) from 1 for strpos(reverse(My_Path),'\')-1)),''),'\'),'\') AS VolumePath
This solution may help you in the feature.
Thanks for your answers friends.

Related

Export multiple line in CSV (Not multi-property value into CSV)

First of all I would like to clarify that my question is NOT: How to export multi-valued properties (array) into csv file.
My use case is the following: I am creating a code to audit Hyper-V infrastructure and I would like to have in one CSV cell multiple lines. For example: NIC1, NIC2 ... Disk 1, Disk 2.
I do not want to use join operator and have it on a single line.
I am almost certain that there was an article about that and that i did managed to achieve the goal, but unfortunately I am not able to find neither the article, neither the scrip in which I have used it.
Any suggestions or ideas would be highly appreciated! :)
The hint was submitted by LeroyJD in the comments.
#LeroyJD, Thank you very much, helped a lot! :)
To summarize for future reference: Yes, it is possible by using the new line backtick n to present in multiple lines. Sample code:
[PSCustomObject]#{
Value1 = 'Hello'
Value2 = "Hello `nWorld"
} | Export-Csv C:\TEMP\multiline.csv -NoTypeInformation
Invoke-Item C:\TEMP\multiline.csv
Which results into:
Thanks again to LeroyJD for the hint!

How to replace a string by a subtring in postgres?

I have the following file name in a column on my db: /folder/path/24/24_1543250974365.wav
And I want to replace it by: 24_1543250974365.wav
In summary, I need to cut off the path and replace it by what comes after the last "/" in the String.
My idea is to find the last occurrence of "/" and get the string from there to the end and do a replacement, but not sure if that's the best way to do that
UPDATE tablename
SET file = REPLACE(file, file, substring('/', 1))
WHERE id=1;
I'll need a function that replace all data in this column, so I don't have anymore the entire path but only the file name. Any help would be appreciated!
You might have to add or subtract 1, but this should work
right(file,strpos(reverse(file),'/'))
select right(file,position('/' in reverse(file))-1)

How to Use fullfile in Matlab?

I am studying Suever's answer where I do not understand its application with fullfile in Code 1.
Code 1
filename=strcat('/Users/masi/Images/', 'p.1');
save(fullfile(filename,'.mat'),'time');
saveas(her, fullfile(filename,'.png'));
Output
Error using save
Cannot create '.mat' because '/Users/masi/Images/p.1' does not
exist.
Code 2
filename=strcat('/Users/masi/Images/', 'p.1');
save(strcat(filename,'.mat'),'time');
saveas(her, strcat(filename,'.png'));
Success!
Change made based on Daniel's answer
filenameMat=fullfile('/Users/masi/Images/', 'p.1', '.mat');
save(filenameMat,'time');
but still getting
Error using save
Cannot create '.mat' because '/Users/masi/Images/p.1.mat' does not
exist.
I do not understand.
Why is code 1 giving the error?
You are using fullfile for a wrong application. The documentation clearly explains the parameters:
Folder and file names, specified as strings and cell arrays of strings.
You input a filename without extension and a file extension, hat is not what fullfile is made for. It will insert a file separator between both:
>> fullfile('foo','.bar')
ans =
foo\.bar
fullfile would be the right function to construct filename.

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

How to get the path part of a filename?

I want to get the path part of a file name in MATLAB like dirname and basename in Linux. I've tried to turn to find a function like strrchr, but I failed. I know strtok, strfind and textscan can be used, but I want to accomplish this with not more than two statements.
For this particular problem I suggest you use the fileparts function:
[path, filename, extension] = fileparts(str)
Nick's answer definitely does what you ask for, but here's an alternative answer using regexprep:
regexprep(str, '(.+)(?:\\|/)(.*)', '$1')
If you'd want to capture the filename (extension included), use the $2 token instead of $1.
It's a good exercise for regular expressions, which prove to be very useful in MATLAB when parsing text.