You are not logged in.
Pages: 1
Well, I'm starting with pascal and I need some tips.
What I'm trying to do is:
If I have a folder I'll put a temporal extension '.fldr', and if i have a file without extension or the extension (or what it thinks it's an extension) is less than 4 characters the extension will be '.ext'
This is my script but I get some errors like 'Error: Identifier expected'
begin
If WideFileExists(FilePath) then
If WideSameText(WideExtractBaseName(FileName), FileName) then
FileName:= FileName + '.ext';
else
If Length(WideExtractFileExt(FileName)) > 5 then
FileName:= FileName + '.ext';
else
FileName := FileName + '.fldr';
end.
And by the way, the ; are used correctly? I don't know very well how to use them
Last edited by SafetyCar (2009-06-12 11:01)
If this software has helped you, consider getting your pro version. :)
Offline
Yeah, it is about ;
And about blocking of code. It's always safier to add begin-end if there is more than one if-then-else statement and you're not sure how it will be interpreted by the compiler.
begin
If WideFileExists(FilePath) then
begin
If WideSameText(WideExtractBaseName(FileName), FileName) then
FileName:= FileName + '.ext'
else
If Length(WideExtractFileExt(FileName)) > 5 then
FileName:= FileName + '.ext'
end
else
FileName := FileName + '.fldr';
end.
You shouldn't use ; before the else part of if-then-else statement. ";" means there is no "else" part.
Now there is a question if you want to add '.ext' after the existing (4 or more char) extention (what you did) or to replace it with '.ext'.
Then the adequate line should sound:
If Length(WideExtractFileExt(FileName)) > 5 then
FileName:= WideExtractBaseName(FileName) + '.ext'
Last edited by krtek (2009-06-12 12:58)
Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).
Offline
Now there is a question if you want to add '.ext' after the existing (4 or more char) extention (what you did) or to replace it with '.ext'.
Then the adequate line should sound:If Length(WideExtractFileExt(FileName)) > 5 then FileName:= WideExtractBaseName(FileName) + '.ext'
Well this rule is for being able to clean better the dots on the filenames, because the "skip extension" doesn't work like I want for folders or some files with bad extensions. So I just wanted to add '.ext' because in this way it's that what will skip and not other parts of the name that aren't extensions but the program thinks so.
The script is the first rule, the last is a RegEx ' \.(fldr|ext)$', an between these, the rest of the rules (with the skip extension marked).
But continuing with the script, one doubt, the end of the first if-statement is closed with ; but the 2nd (inside) it's not, so I don't know if de the if-statements are always closed whit ; or not.
And about the 3rd If-Statement, from your explanation I interpret that it shoul be ended with ; (because there is no "else" part) like this:
begin
If WideFileExists(FilePath) then
begin
If WideSameText(WideExtractBaseName(FileName), FileName) then
FileName:= FileName + '.ext'
else
If Length(WideExtractFileExt(FileName)) > 5 then
FileName:= FileName + '.ext';
end
else
FileName := FileName + '.fldr';
end.
If this software has helped you, consider getting your pro version. :)
Offline
You're right. It should end with ";".
But I guess that begin-end block makes that ";" unnecessary.
Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).
Offline
And by the way, the ; are used correctly? I don't know very well how to use them
Semicolons ";" must be at the end of every statement, but you must not put semicolons before the ELSE keyword. In case of IF ELSE there is an exception because it is a compound statement. Syntax for IF ELSE would be as follows:
IF logic THEN
statement1
ELSE
statement2;
The IF ELSE code can also be expanded to use BEGIN and END:
IF logic THEN
BEGIN
statement1;
END
ELSE
BEGIN
statement2;
END;
Offline
In general:
IF <expression> THEN
BEGIN
statement 1;
statement 2;
...
statement n;
END
ELSE
BEGIN
statement 1;
statement 2;
...
statement n;
END;
where <expression> can be as complex as you want and can involve functions or any type of operator (arithmetic, logical etc.), as long as the whole thing evaluates to a Boolean value of True or False.
You can view this whole block as one huge compund statement, which is why the ; comes right at the end.
There are tons of Pascal tutorials on the net, such as this one for example.
P.S. Do we need to add basic topics like these to the wiki, or should we point to other pages on the net?
Last edited by Andrew (2009-06-13 17:21)
Offline
There are tons of Pascal tutorials on the net, such as this one for example.
P.S. Do we need to add basic topics like these to the wiki, or should we point to other pages on the net?
In fact, I was on that same page when I was searching for information but, well, I don't know, I didn't understand much of the explanations and others pages explained the if-else statement very simply, nothing explained about begin-end or semi-colons
Thanks for all
If this software has helped you, consider getting your pro version. :)
Offline
P.S. Do we need to add basic topics like these to the wiki, or should we point to other pages on the net?
Yes, we should really create small help section regarding scripting on the Wiki. Related section is already created in the Wiki, someone just needs to add content there..
Offline
SafetyCar, if you feel that there are some specific topics in pascal script which need explanation feel free to create Wiki articles as questions, which we later can explain. In this way we will be creating user manual for stuff that really matters.
Offline
I remember reading something-or-other on semicolons, which explains the difference between how they behave in Pascal, versus something like C.... With a lil bit of rummaging, I re-found it. Go figure, it was a Wikipedia article:
http://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C
Another, more subtle, difference is the role of the semicolon. In Pascal semicolons separate individual statements within a compound statement whereas they terminate the statement in C. They are also syntactically part of the statement itself in C (transforming an expression into a statement). This difference manifests itself primarily in two situations:
there can never be a semicolon directly before else in Pascal whereas it is mandatory in C (unless a block statement is used)
There are other interesting differences listed, but this is the big one here. The Wikipedia page goes a bit further than what I copied here, so it might be worth a full read.
Offline
Pages: 1