Solved

String concatenation in Windows batch file?

Posted on 2004-09-17
11
58,535 Views
Last Modified: 2011-08-18
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
Comment
Question by:dpichett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 50 total points
ID: 12084667
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12084672
Ahh, wait, disregard that last not sure why you need the quotes statement, I understand

Regards...
0
 

Author Comment

by:dpichett
ID: 12084754
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 25 total points
ID: 12085046
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
 
LVL 5

Assisted Solution

by:brianadkins
brianadkins earned 25 total points
ID: 12085339


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

-Brian
0
 
LVL 10

Assisted Solution

by:For-Soft
For-Soft earned 25 total points
ID: 12085551
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
 
LVL 5

Expert Comment

by:brianadkins
ID: 12085567
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
 
LVL 10

Expert Comment

by:For-Soft
ID: 12085581
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 12085608
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
 
LVL 10

Expert Comment

by:For-Soft
ID: 12085691
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
 

Author Comment

by:dpichett
ID: 12085743
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with command line that was built to copy files if changes were done. 4 51
Robocopy command line 6 69
how to count files? 4 50
Remote start and stop of service 3 39
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question