Concatenation in for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mimismo
Posts: 11
Joined: 11 Jun 2016 07:59

Concatenation in for loop

#1 Post by mimismo » 11 Jun 2016 09:41

Hi Team
I'm wondering if you could help me.

This is my request
I have a text file containing N number of lines betwen one and ten, who has spaces in between each row.
This is an example of my file:
MyTextFile.txt
String1 numer1.txt
String2 number2.txt
String3 number3.txt

All rows in the file MyTextFile.txt are existing files in the same path
What I want is to get is a var containing all N variable lines in one row, adding a + sign.
By example my requested output will be:
MyVar="String1 number1"+"String2 number2"+"String3 number3"

after having that, I would apply a copy command in order to get a new file, by example:
copy /a MyVar NewTextFile.txt

Please advice me
Thank you in advance

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Concatenation in for loop

#2 Post by Squashman » 11 Jun 2016 10:51

Why are you truncating the file extension? The copy command is not going to find your files without the extension.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Concatenation in for loop

#3 Post by Aacini » 11 Jun 2016 11:22

This do what you requested:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "MyVar="
for /F "delims=" %%a in (MyTextFile.txt) do set "MyVar=!MyVar!+"%%a""
copy /a %MyVar:~1% NewTextFile.txt

This is the way I would do this process:

Code: Select all

@echo off
(for /F "delims=" %%a in (MyTextFile.txt) do type "%%a") > NewTextFile.txt

Antonio

mimismo
Posts: 11
Joined: 11 Jun 2016 07:59

Re: Concatenation in for loop

#4 Post by mimismo » 11 Jun 2016 12:17

Hi Squashman, you are right, I missed file extension in my post, sorry, but it should be included...

Hi Aacini, your code works as I was expected
Thank you for your help

Happy weekend

mimismo
Posts: 11
Joined: 11 Jun 2016 07:59

Re: Concatenation in for loop

#5 Post by mimismo » 15 Jun 2016 08:06

Hi Antonio

I have a quick question, the code you shared me works great, but the only thing I've noticed is when I've tried to use it as a block of another extra code.

If I use that code in a subroutine passing a variable instead of a file name, the concatenation didn't work.
Debugging the code, I can see that when a variable is an input of the subroutine, the code reads the file name itself but it did not read the content of the file and if I use a file name instead as input, the code work great

Please take a look at the code

Code: Select all

for /F "tokens=*" %%B in (Each.csv.txt) do (
   for /F "tokens=*" %%C in ("%%B") do (
      set Tem="%%C"
      call :JoinFiles
   )
)

:JoinFiles
echo Global Var Tem: %Tem%
set MyVar=%Tem%
for /F "tokens=*" %%D in (%Tem%) do (
   set %MyVar%=%MyVar%+"%%D"
)
   copy /a %MyVar% %MyVar%.csv
exit /b


How can I make i work with a variable instead of a name?

Again, thank you.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Concatenation in for loop

#6 Post by sambul35 » 15 Jun 2016 08:32

Did you try delayed expansion:

Code: Select all

setlocal EnableDelayedExpension

:: use !Tem! in the function


I didn't check your code any further. :)

mimismo
Posts: 11
Joined: 11 Jun 2016 07:59

Re: Concatenation in for loop

#7 Post by mimismo » 15 Jun 2016 09:30

Hi sambul35

Thank you for your quick response, I'd already set the "setlocal EnableDelayedExpansion" command and I've tried using !Tem! but it seems is the same situation, using a variable as input, batch file reads the file name but I didn't go through the content of the file.

I'm becoming a bit crazy because I'm stuck on it.
Thank you again for any clue to make it works...

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Concatenation in for loop

#8 Post by sambul35 » 15 Jun 2016 09:58

I didn't test it, just corrected your syntax without knowing if it does anything useful to begin with:

Code: Select all

for /F "tokens=*" %%B in (Each.csv.txt) do (
   for /F "tokens=*" %%C in ("%%B") do (
      set Tem="%%C"
      call :JoinFiles
   )
)

:JoinFiles
set MyVar=!Tem!
for /F "tokens=*" %%D in (!Tem!) do (
   set "MyVar=!MyVar!+"%%D""
)
   echo "!Tem!" '!MyVar!'
   copy /a !MyVar! !MyVar!.csv
exit /b


OR:

Code: Select all

for /F "tokens=*" %%B in (Each.csv.txt) do (
   for /F "tokens=*" %%C in ("%%B") do (
      call :JoinFiles
   )
)

:JoinFiles
set MyVar=!Tem!
for /F "tokens=*" %%D in ("%%C") do (
   set "MyVar=!MyVar!+"%%D""
)
   copy /a !MyVar! !MyVar!.csv
exit /b
Last edited by sambul35 on 16 Jun 2016 12:04, edited 1 time in total.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Concatenation in for loop

#9 Post by Squashman » 15 Jun 2016 11:44

Well the way I see it, it looks like you totally changed the scope of what you are trying to do. At least that is what I see from reading your new code.
You might want to take a step back and explain what your real goal is.
Provide real world examples of what your input is and provide a real world example of what you want the output to be.

Post Reply