Page 1 of 1
Remove extension from a filename?
Posted: 28 Jun 2011 22:13
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?
Re: Remove extension from a filename?
Posted: 28 Jun 2011 22:40
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
Re: Remove extension from a filename?
Posted: 28 Jun 2011 22:57
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!
Re: Remove extension from a filename?
Posted: 28 Jun 2011 23:25
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)?
Re: Remove extension from a filename?
Posted: 28 Jun 2011 23:57
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?
Re: Remove extension from a filename?
Posted: 29 Jun 2011 08:08
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;
}
Re: Remove extension from a filename?
Posted: 29 Jun 2011 08:12
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.
Re: Remove extension from a filename?
Posted: 29 Jun 2011 08:48
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
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).