Link to home
Start Free TrialLog in
Avatar of dpichett
dpichett

asked on

String concatenation in Windows batch file?

If I have environmental variables like:

X3DESTDIR="C:\Destination Folder"
X4DESTFIL="\zzzfiles\SubfolderLevel2\SubfolderLevel3\file 553.zzz"

how can the two strings be concatenated.  The use of percent signs like

SET FINAL_DEST=%DESTDIR%%DESTFIL%

results in extranneous quotes, i.e.

FINAL_DEST="C:\Destination Folder""\zzzfiles\SubfolderLevel2\SubfolderLevel3\file 553.zzz"

How to eliminate the extra quotes ("") in the middle of the result?
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, wait, disregard that last not sure why you need the quotes statement, I understand

Regards...
Avatar of dpichett
dpichett

ASKER

Hello Zyloch,

Thanks for the feedback, I don't want to strip out the quotes, I need the quotes to handle spaces in the folder and directory names.  

Essentially, I'm looking for syntax that will take the strings "John Q." and " Public" and produce "John Q. Public", rather than "John Q."" Public"

Further thoughts?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Similarly, my solution above removes all the quotes from both variables, slaps the variables together, and then puts quotes back on the ends.

-Brian
The advantage of this solution is:
If the FINAL_DEST variable does not have any "" substring it will do nothing. If "" substring exists it will be deleted.
My solution is overkill, but I love overkill :)

Also, I neglected to slap quotes back on results:

call :CONCAT FINAL_DEST %X3DESTDIR% %X4DESTFIL%

set FINAL_DEST="%FINAL_DEST%"

echo FINAL_DEST=%FINAL_DEST%
After some thought Brian's idea has an advantage also. If only one string has quotes it will work fine. Mine will do nothing.

Better is to have quotes always, than not to have quotes when needed.
thanks to all.

ultimately, stripping out all quotes (including valid and extranneous) and then re-including quotes around any subsequent use of the variable looks like it will do the trick.