Page 1 of 1

Using a Variable to trim another variable?

Posted: 24 Apr 2010 17:35
by ian2000t
Hi, hope you can help me with this... hopefully should be an easy one!

I'm trying to write a batch file but I'm having problem trying to use the VALUE of a Variable in a 'Trim' function.

I have variable "BackupDir" which needs right-trimming by x amount. Amount x is defined by a variable "Length".

To do it by a fixed amount (trim 2 from the left and x from the right) I'd do:

%BackupDir:~2,-x%

But... how do I trim it by the value of "Length" ? An example below... but in reality the value of Length will change.

Set Length=11
Set BackupDir=%BackupDir:~2,- [%Length%] %

- I want this to do: BackupDir=%BackupDir:~2,-11%

Any ideas?

Re: Using a Variable to trim another variable?

Posted: 24 Apr 2010 17:43
by aGerman
You could try this:

Call Set BackupDir=%%BackupDir:~2,-%Length%%%

Regards
aGerman

Re: Using a Variable to trim another variable?

Posted: 24 Apr 2010 17:55
by ian2000t
Excellent, had to take off a % from each end and it worked fine!
Thanks for that!

Re: Using a Variable to trim another variable?

Posted: 24 Apr 2010 18:14
by aGerman
ian2000t wrote:[...]had to take off a % from each end[...]

Realy? That would never work for me, because the Call command expands %Length% to the value and removes one % from each end.

Regards
aGerman