Remove extension from a filename?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Remove extension from a filename?

Post by highend »

Hi,

I'm using

Code: Select all

$SelectedItems = get("SelectedItemsNames", "<crlf>");
to get all selected filenames (need them later in the script) without their path component.

In the while loop I want to strip off the extension (.avi, .wmv, .whatever) for that filename.

$FileName contains the current array element in the list, but If I use e.g.

Code: Select all

$OutputFile = regexreplace("$FileName", "\.\w+$");
the next command (echo $Outputfile;) will generate the output:
$Outputfile

and not e.g. BestMovieEver (if the $FileName was BestMovieEver.avi)

The regex isn't the best but it should be enough to filter out a normal (windows) file extension.

Flaw in my logic?
One of my scripts helped you out? Please donate via Paypal

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Remove extension from a filename?

Post by Stefan »

You have to initialize the var before first use.

Just:
$MyVarName="";
before the while()


Some hints:

- for the loop you can use foreach()

- for splitting base/extension you can use InStrRev() (search the forum)
which should be quicker then an regex if you work on many files.

- or use
$FILELIST = report(" {basename}?{ext}|",1);
and then use
$base = gettoken($file,1,"?");
$ext = gettoken($file,2,"?");
$filename = "$base.$ext";
inside the while() loop

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Remove extension from a filename?

Post by highend »

You should change your nickname to "AskMeIfYouHaveAScriptQuestion"^^
You have to initialize the var before first use.

Just:
$MyVarName="";
before the while()
Did that but the output was the same.
- for the loop you can use foreach()
I'll read the help and see if it's easy enough (for me) to change it.
$FILELIST = report(" {basename}?{ext}|",1);
and then use
$base = gettoken($file,1,"?");
$ext = gettoken($file,2,"?");
$filename = "$base.$ext";
inside the while() loop
I'll probably use this way.

As always, thanks for your help Stefan!
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Remove extension from a filename?

Post by highend »

Code: Select all

	$filelist = report("{basename}?{ext}|",1);

	foreach($line, $filelist, "|") {
	$base = gettoken($line,1,"?");
	$ext = gettoken($line,2,"?");
	$filename = "$base.$ext";
	echo $filename;
	}
Just to test if everything is working before I exchange the code in the real script.

First echo: "ArrangeTaskbar.vbs"
Sec. echo: "foobar2000_v1.1.7.exe"
Third echo: "."

How to avoid the third output if (only two files were selected)?
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Remove extension from a filename?

Post by highend »

$filelist = report("{basename}?{ext}|",1);

foreach($token, $filelist, "|") {
$base = gettoken($token,1,"?");
$ext = gettoken($token,2,"?");
$filename = "$base.$ext";
if ($filename != ".") { echo $filename; }
}
The if statement will now output only those lines that don't consist of a single "."

Is this the way to go, or is there a better method?
One of my scripts helped you out? Please donate via Paypal

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Remove extension from a filename?

Post by Stefan »

You get an additional loop
because your var $filelist contains an trailing delimiter by natur: "name1|name2|name3|"

Just remove them after the collection, f.ex.:

Code: Select all

$A = "123456789";
  $B = substr( $A , 0, strlen($A)-1);
  msg "$A<crlf>$B";
EDIT:
i checked the help and saw the substr() can be even more easier:

Code: Select all

$A = "eins|zwei|drei|";
  $A = substr($A, 0, -1);
   foreach($t,$A){echo $t;}



Or simple check inside the foreach() loop if the current token is empty:

Code: Select all

foreach( $token, $filelist )
  {
     if ( $token == "" ){ break; }
   
     //else:
     echo $token;
  }

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Remove extension from a filename?

Post by Stefan »

highend wrote:Hi,

I'm using

Code: Select all

$OutputFile = regexreplace("$FileName", "\.\w+$");
the next command (echo $Outputfile;) will generate the output:
$Outputfile

Flaw in my logic?
Vars are case-sensitive.

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Remove extension from a filename?

Post by highend »

$A = "eins|zwei|drei|";
$A = substr($A, 0, -1);
foreach($t,$A){echo $t;}
That's really an easy way. Changed my code respectively.
Vars are case-sensitive.
Yeah but it was late and couldn't see that any more :oops:

Thanks Stefan, script is working fine now (it consists of a few entries for converting videos to dvd compliant mpg (via ffmpeg) with preset parameters and one that ask the user for additional options (to remove unwanted audio streams, etc.), as well as pre-analyzing, and a simple .vob joiner).
One of my scripts helped you out? Please donate via Paypal

Post Reply