Copy last three characters of a variable to a new variable

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
CGooley
Newbie
Posts: 17
Joined: Tue Jun 25, 2019 6:13 pm

Copy last three characters of a variable to a new variable

Post by CGooley » Wed Jun 26, 2019 3:36 pm

Hi,

Would like to assign the last three characters of a string variable to a new string variable. Any idea on how to do this?

Thanks!

CGooley
Newbie
Posts: 17
Joined: Tue Jun 25, 2019 6:13 pm

Re: Copy last three characters of a variable to a new variable

Post by CGooley » Wed Jun 26, 2019 6:19 pm

Here is how I am currently trying to do this:

GetClipBoard>filename

Let>filenamelength={length(%filename%)}
Let>count=filenamelength-3

Let>at={Pos(" ",%filename%)}

Let>branch={copy(%filename%,count,%at%-1)}

message>branch


Branch should return the last 3 characters, but only does this if I specify manually in count.

As the code is now, I get an Unknown Identifier "COUNT" error

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

Re: Copy last three characters of a variable to a new variable

Post by JRL » Thu Jun 27, 2019 3:21 pm

You probably have previous programming experience which will be a benefit for logic but a deficit for syntax. You need to study posted scripts and get a feel for available commands and Macro Scheduler syntax which does not allow multiple functions on the same line.

Code: Select all

GetClipBoard>filename
Length>FileName,filenamelength
Let>filenamelength={%filenamelength%-2}
Midstr>filename,filenamelength,3,branch

mdl>branch

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

Re: Copy last three characters of a variable to a new variable

Post by Marcus Tettmar » Mon Jul 01, 2019 2:35 pm

CGooley wrote:
Wed Jun 26, 2019 6:19 pm
Here is how I am currently trying to do this:

GetClipBoard>filename

Let>filenamelength={length(%filename%)}
Let>count=filenamelength-3

Let>at={Pos(" ",%filename%)}

Let>branch={copy(%filename%,count,%at%-1)}

message>branch


Branch should return the last 3 characters, but only does this if I specify manually in count.

As the code is now, I get an Unknown Identifier "COUNT" error
You've missed the % .. % around "count" as with all your other variables. That line should be:

Let>branch={copy(%filename%,%count%,%at%-1)}
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
Marcus Tettmar
Site Admin
Posts: 7393
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Copy last three characters of a variable to a new variable

Post by Marcus Tettmar » Mon Jul 01, 2019 2:37 pm

JRL wrote:
Thu Jun 27, 2019 3:21 pm
You probably have previous programming experience which will be a benefit for logic but a deficit for syntax. You need to study posted scripts and get a feel for available commands and Macro Scheduler syntax which does not allow multiple functions on the same line.
CGooley's syntax is correct - it is using complex expressions. Just missed % around the count variable. But your solution works too :-)
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
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: Copy last three characters of a variable to a new variable

Post by PepsiHog » Wed Jul 03, 2019 10:42 pm

@CGooley,

Yeah, you could do it that way. Some people like long lines of programming to accomplish a 1 liner.

Code: Select all

let>FileName=MyFilesNamelast3.txt
RegEx>.*(...)\..*,FileName,0,match,nom,1,$1,res

mdl>%res%
Boy! If the RegEx commission ever finds out how much I use RegEx, I am going to be in a lot of trouble!

PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

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