Page 1 of 1

direct swap

Posted: 05 Feb 2013 08:18
by Sponge Belly
Hello All!

Came across a post about swapping values by Joey recently. Haven’t seen anything similar elsewhere so I thought I’d share it with you. Try this out:

Code: Select all

set "fruitA=apples" & set "fruitB=bananas"
set fruit
set "fruitA=%fruitB%" & set "fruitB=%fruitA%"
set fruit


Nice trick, eh? ;-) Note that the “swap” has to be on one line and it won't work under delayed expansion.

- SB

PS: Added missing per cent. Good catch, Squashman!

Re: direct swap

Posted: 05 Feb 2013 09:00
by Squashman
Makes logical sense according to the rules. All variables are expanded before the line is executed for normal variable expansion.

You are missing a %.

You could also write it like this.

Code: Select all

@echo off
set "fruitA=apples"
set "fruitB=bananas"
set fruit

(set "fruitA=%fruitB%"
set "fruitB=%fruitA%")
set fruit