MidStr equivalent

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
timle
Pro Scripter
Posts: 96
Joined: Tue Apr 20, 2004 5:53 am

MidStr equivalent

Post by timle » Thu Apr 19, 2007 3:52 pm

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

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Apr 19, 2007 5:51 pm

Anything wrong with:

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?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Thu Apr 19, 2007 9:13 pm

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:

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 - :-)

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Apr 19, 2007 10:32 pm

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.

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

joe88888
Newbie
Posts: 14
Joined: Wed Nov 22, 2006 4:56 pm

Post by joe88888 » Fri Apr 20, 2007 4:52 am

Try this, maybe ok?

Let>filename=c:\dir\somefile.ext
Length>%filename%,Len
let>len=len-4
//4=.ext
MidStr>filename,1,len,filename
MessageModal>filename

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Apr 20, 2007 6:03 am

Hello joe88888

I think that will only work with files that have 3 char extensions.
What about *.html files?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Apr 20, 2007 7:32 am

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:

Code: Select all

VBSTART
VBEND

Let>filename=c:\some.file.name.ext
VBEval>InStrRev("%filename%",".")-1,rpos
MidStr>filename,1,rpos,filename
MessageModal>filename
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

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?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Apr 20, 2007 8:28 am

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?
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 - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Apr 20, 2007 8:38 am

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?
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.
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".
Correct.
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?
It returns two elements containing nothing because "." as a list with delimiter "." is a list of two lots of nothing.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Apr 20, 2007 2:12 pm

Bob Hansen wrote:Hello joe88888

I think that will only work with files that have 3 char extensions.
What about *.html files?
The original question to which that code was part of the answer stated that it was a .pdf file.

joe88888
Newbie
Posts: 14
Joined: Wed Nov 22, 2006 4:56 pm

Re: MidStr equivalent

Post by joe88888 » Fri Apr 20, 2007 2:23 pm

tleary wrote:but the file name is not consistent and the extention is
In the original it states the extention is consistent, if html file then change 4 to 5

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts