Solved

String concatenation in Windows batch file?

Posted on 2004-09-17
11
59,277 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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