Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String concatenation in Windows batch file?

Posted on 2004-09-17
11
Medium Priority
?
60,390 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
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 150 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 75 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 75 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 75 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

926 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