Page 1 of 1

Concatenation in for loop

Posted: 11 Jun 2016 09:41
by mimismo
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

Re: Concatenation in for loop

Posted: 11 Jun 2016 10:51
by Squashman
Why are you truncating the file extension? The copy command is not going to find your files without the extension.

Re: Concatenation in for loop

Posted: 11 Jun 2016 11:22
by Aacini
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

Re: Concatenation in for loop

Posted: 11 Jun 2016 12:17
by mimismo
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

Re: Concatenation in for loop

Posted: 15 Jun 2016 08:06
by mimismo
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.

Re: Concatenation in for loop

Posted: 15 Jun 2016 08:32
by sambul35
Did you try delayed expansion:

Code: Select all

setlocal EnableDelayedExpension

:: use !Tem! in the function


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

Re: Concatenation in for loop

Posted: 15 Jun 2016 09:30
by mimismo
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...

Re: Concatenation in for loop

Posted: 15 Jun 2016 09:58
by sambul35
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

Re: Concatenation in for loop

Posted: 15 Jun 2016 11:44
by Squashman
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.