Extract filename and extension in Bash

First, get file name without the path: filename=$(basename — “$fullfile”) extension=”${filename##*.}” filename=”${filename%.*}” Alternatively, you can focus on the last “https://stackoverflow.com/” of the path instead of the ‘.’ which should work even if you have unpredictable file extensions: filename=”${fullfile##*/}” You may want to check the documentation : On the web at section “3.5.3 Shell Parameter Expansion” … Read more

How can I sanitize a string for use as a filename?

You can use PathGetCharType function, PathCleanupSpec function or the following trick: function IsValidFilePath(const FileName: String): Boolean; var S: String; I: Integer; begin Result := False; S := FileName; repeat I := LastDelimiter(‘\/’, S); MoveFile(nil, PChar(S)); if (GetLastError = ERROR_ALREADY_EXISTS) or ( (GetFileAttributes(PChar(Copy(S, I + 1, MaxInt))) = INVALID_FILE_ATTRIBUTES) and (GetLastError=ERROR_INVALID_NAME) ) then Exit; if I>0 … Read more

Extracting a file extension from a given path in Rust idiomatically

In idiomatic Rust the return type of a function that can fail should be an Option or a Result. In general, functions should also accept slices instead of Strings and only create a new String where necessary. This reduces excessive copying and heap allocations. You can use the provided extension() method and then convert the … Read more

best way to remove file extension [duplicate]

Read the documentation of File::basename : basename(file_name [, suffix] ) → base_name Returns the last component of the filename given in file_name, which can be formed using both File::SEPARATOR and File::ALT_SEPARETOR as the separator when File::ALT_SEPARATOR is not nil. If suffix is given and present at the end of file_name, it is removed. file = … Read more

tech