Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate strings in a Windows batch file?

I have a directory for which I want to list all the .doc files with a ;.

I know the following batch command echos all the files:

for /r %%i In (*.doc) DO echo %%i 

But now I want to put them all in a variable, add a ; in between and echo them all at once.
How can I do that?

set myvar="the list: " for /r %%i In (*.doc) DO <what?> echo %myvar% 
like image 847
Fortega Avatar asked Jan 08 '10 11:01

Fortega


People also ask

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do I append to a batch file?

Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file. Following is a simple example of how to create a file using the redirection command to append data to files.

How do I concatenate strings in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do I combine multiple strings into one?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


1 Answers

What about:

@echo off set myvar="the list: " for /r %%i in (*.doc) DO call :concat %%i echo %myvar% goto :eof  :concat set myvar=%myvar% %1; goto :eof 
like image 70
Rubens Farias Avatar answered Oct 09 '22 12:10

Rubens Farias