trim .txt off of file names

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
RCR1991
Newbie
Posts: 7
Joined: Fri May 05, 2006 11:56 pm

trim .txt off of file names

Post by RCR1991 » Sat May 06, 2006 2:42 pm

Hi,

I read a list of files all w/ .txt extension. Program I am passing that info to needs to have just the filename passed to it, not the extension. Been searching the archives here and found this copy command pasted below--but I don't completely understand the syntax.

Code:
//code to read file list
GetFileList>D:\AOK_5_1_06_CHART\test\*.txt,files
Separate>files,;,file_names

Let>k=0
Repeat>k
Let>k=k+1

Let>fn2={copy(file_names_%k%,4,length(file_names_%k%)-4)}
Message>%fn2%


Label>end
//Until>k,file_names_count

The lines in bold above are what I am stuck on.

Purpose is to get a newvar (fn2) to hold the value of the variable file_names - last 4 characters. The copy command was deep in a thread about this sort of text manipulation, and I believe was submitted by one of the MJT folk. But I cannot find documentation on the syntax so....help how to make this work?

TIA

Rob

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

Post by Marcus Tettmar » Sat May 06, 2006 3:04 pm

That syntax is wrong and in any case starts from the 4th character. You want this:

Let>filename=file_names_%k%
Let>filename={copy(%filename%,1,length(%filename%)-4)}

E.g. try this example on it's own:

Let>filename=somefile.txt
Let>filename={copy(%filename%,1,length(%filename%)-4)}
MessageModal>filename

You will see that the outcome is somefile without the .txt

How does it work? Read the Complex Expressions topic in the help file. In there you will see topics for the copy and length functions.

Length returns the length of a string, so in this case Length(%filename%) returns the length of somefile.txt = 12. Copy extracts x chars from some string starting from position n. In this case the string is filename, the starting position is the first char (1) and the number of chars to extract is the length of filename - 4 = 8 characters. 1 to 8 is somefile. .txt is 4 you see.

So all it is is the two functions combined. The following is the same in longer form but easier to read:

Let>filename=somefile.txt
Let>len_filename={length(%filename%)}
//len_filename should now equal 12
Let>filename={copy(%filename%,1,%len_filename%-4)}

Hope this explains it.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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