Solved

String concatenation in Windows batch file?

Posted on 2004-09-17
11
57,529 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 50 total points
Comment Utility
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
Comment Utility
Ahh, wait, disregard that last not sure why you need the quotes statement, I understand

Regards...
0
 

Author Comment

by:dpichett
Comment Utility
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
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 25 total points
Comment Utility
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
Comment Utility


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

-Brian
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Assisted Solution

by:For-Soft
For-Soft earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now