MidStr equivalent
Moderators: Dorian (MJT support), JRL
MidStr equivalent
Is there a function equivalent with MidStr but instead of "string,start,length,result", it would be "string,END,length,result" ? I want to display the file name without the .extention but the file name is not consistent and the extention is
thank you
thank you
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Anything wrong with:
Let>filename=c:\dir\somefile.ext
Position>.,filename,1,pdot
Let>pdot=pdot-1
MidStr>filename,1,pdot,filename
MessageModal>filename
Let>filename=c:\dir\somefile.ext
Position>.,filename,1,pdot
Let>pdot=pdot-1
MidStr>filename,1,pdot,filename
MessageModal>filename
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
There's a problem...
A period (.) is a valid character for a filename so if we ran across a file called: c:\dir\some.file.ext
that script would return the erroneous value: c:\dir\some
The script below properly searches for the first period "on the right" and would return: c:\dir\some.file
which I think is what is desired:
A period (.) is a valid character for a filename so if we ran across a file called: c:\dir\some.file.ext
that script would return the erroneous value: c:\dir\some
The script below properly searches for the first period "on the right" and would return: c:\dir\some.file
which I think is what is desired:
Code: Select all
Let>filename=c:\dir\some.file.ext
Length>filename,filename_length
Let>char_position=filename_length
Label>look_for_right_most_period
MidStr>filename,char_position,1,this_char
If>this_char=.,found_right_most_period
Sub>char_position,1
If>char_position=0,no_periods_in_filename
Goto>look_for_right_most_period
Label>found_right_most_period
Sub>char_position,1
MidStr>filename,1,char_position,filename
MDL>filename
Goto>end
Label>no_periods_in_filename
MDL>Sorry, no period (.) in filename: %filename%
Label>end
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
I agree, extra dots in file names can be hazardous. I've also been bitten by dots in directory names.
I like using separate to remove file extensions.
I like using separate to remove file extensions.
Code: Select all
Let>filename=c:\dir\some.directory\some.file.ext
Separate>%filename%,\,dir
Let>value=dir_%dir_count%
Separate>%value%,.,ext
If>%ext_count%=1
Let>filename= There is no extension in file%CRLF%%filename%
Goto>NoDot
EndIf
Let>value=ext_%ext_count%
Position>.%value%,filename,1,ext
sub>ext,1
MidStr>filename,1,%ext%,filename
Label>NoDot
MessageModal>filename
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
VBScript has a very handy function called InStrRev which works like InStr but in reverse. So it finds the position of a character/substring in a string starting from the end. So we can use it to solve this problem as follows:
If you don't want to use VBScript here's another solution using the Separate command. This splits the string up between the dots and then puts them back together again omitting the last part:
Code: Select all
VBSTART
VBEND
Let>filename=c:\some.file.name.ext
VBEval>InStrRev("%filename%",".")-1,rpos
MidStr>filename,1,rpos,filename
MessageModal>filename
Code: Select all
Let>filename=some.file.name.ext
Separate>filename,.,parts
Let>filename=parts_1
Let>k=1
Repeat>k
Let>k=k+1
Let>this=parts_%k%
Let>filename=%filename%.%this%
Until>k={%parts_count%-1}
MessageModal>filename
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Thanks JRL and Marcus for the improved code examples. There are still two cases which produce odd results:
Let>filename=c:\some.file.name.ext\
JRL code produces: -blank-
Marcus code (both) produces: c:\some.file.name
Actually there is no filename here, just a directory so what should we display?
Let>filename=c:\some.file.name.ext\.
JRL code produces: c:\some
Marcus code (both) produces: c:\some.file.name.ext\
This one may not be fair as it may not be possible to name a file just .
Try to rename a file with the following command:
RenameFile>c:\junk.txt,c:\.
It does not rename it to just .
Maybe Marcus can tell us why; I think it has special meaning and may mean "this directory".
One last thing...
Separate>list,delimiter,returnvar
Stepping through JRL's code with the debugger in the first example, its interesting to see what happens when the Separate> command is fed . as both list and delimiter. Its just a single character, a period, why does it generate two list elements?
Let>filename=c:\some.file.name.ext\
JRL code produces: -blank-
Marcus code (both) produces: c:\some.file.name
Actually there is no filename here, just a directory so what should we display?
Let>filename=c:\some.file.name.ext\.
JRL code produces: c:\some
Marcus code (both) produces: c:\some.file.name.ext\
This one may not be fair as it may not be possible to name a file just .
Try to rename a file with the following command:
RenameFile>c:\junk.txt,c:\.
It does not rename it to just .
Maybe Marcus can tell us why; I think it has special meaning and may mean "this directory".
One last thing...
Separate>list,delimiter,returnvar
Stepping through JRL's code with the debugger in the first example, its interesting to see what happens when the Separate> command is fed . as both list and delimiter. Its just a single character, a period, why does it generate two list elements?
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Well, you supplied a directory. The code was written assuming a filename was supplied. If necessary modify the code to first check to see if the supplied argument is a directory or a filename.jpuziano wrote:Let>filename=c:\some.file.name.ext\
JRL code produces: -blank-
Marcus code (both) produces: c:\some.file.name
Actually there is no filename here, just a directory so what should we display?
Correct.This one may not be fair as it may not be possible to name a file just .
Try to rename a file with the following command:
RenameFile>c:\junk.txt,c:\.
It does not rename it to just .
Maybe Marcus can tell us why; I think it has special meaning and may mean "this directory".
It returns two elements containing nothing because "." as a list with delimiter "." is a list of two lots of nothing.Stepping through JRL's code with the debugger in the first example, its interesting to see what happens when the Separate> command is fed . as both list and delimiter. Its just a single character, a period, why does it generate two list elements?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Re: MidStr equivalent
In the original it states the extention is consistent, if html file then change 4 to 5tleary wrote:but the file name is not consistent and the extention is