Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64097
  • Last Modified:

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?
0
dpichett
Asked:
dpichett
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
ZylochCommented:
Hi dpichett,

You could always do

SET FINAL_DEST=%DESTDIR%%DESTFIL:~1%

even though I'm not sure why you need the quotes...

Regards,
Zyloch
0
 
ZylochCommented:
Ahh, wait, disregard that last not sure why you need the quotes statement, I understand

Regards...
0
 
dpichettAuthor Commented:
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?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SteveGTRCommented:
Give this a try:

@echo off

setlocal enabledelayedexpansion

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

call :CONCAT FINAL_DEST %X3DESTDIR% %X4DESTFIL%

echo FINAL_DEST=%FINAL_DEST%

goto :EOF

:CONCAT

if "%~1"=="" goto :EOF

set EnvName=%~1
set %EnvName%=

:GETPARM

shift

if "%~1"=="" goto :EOF

set %EnvName%=!%EnvName%!%~1

goto GETPARM

Good Luck,
Steve
0
 
brianadkinsCommented:


set DESTDIR="C:\Destination Folder"
set DESTFIL="\zzzfiles\SubfolderLevel2\SubfolderLevel3\file 553.zzz"
set FINAL_DEST="%DESTDIR:"=%%DESTFIL:"=%"

-Brian
0
 
For-SoftCommented:
I'm not sure if it will work, but the simplest way to delete "" substring is to repalace it with empty string.
This solution reqiures Windows 2000/XP.

SET FINAL_DEST=%DESTDIR%%DESTFIL%
SET FINAL_DEST=%FINAL_DEST:""=%
0
 
brianadkinsCommented:
Similarly, my solution above removes all the quotes from both variables, slaps the variables together, and then puts quotes back on the ends.

-Brian
0
 
For-SoftCommented:
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.
0
 
SteveGTRCommented:
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%
0
 
For-SoftCommented:
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.
0
 
dpichettAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now