Using a Variable to trim another variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ian2000t
Posts: 2
Joined: 24 Apr 2010 17:31

Using a Variable to trim another variable?

#1 Post by ian2000t » 24 Apr 2010 17:35

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using a Variable to trim another variable?

#2 Post by aGerman » 24 Apr 2010 17:43

You could try this:

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

Regards
aGerman

ian2000t
Posts: 2
Joined: 24 Apr 2010 17:31

Re: Using a Variable to trim another variable?

#3 Post by ian2000t » 24 Apr 2010 17:55

Excellent, had to take off a % from each end and it worked fine!
Thanks for that!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using a Variable to trim another variable?

#4 Post by aGerman » 24 Apr 2010 18:14

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

Post Reply