0

I am trying to write a script that will receive a .txt file and a path as arguments. The .txt file contains names of files, which I need to concatenate with the path.

File contains :

example1.txt
example2.txt

The path is :

C:\test

The output should be :

C:\test\example1.txt
C:\test\example2.txt

The problem comes when I try to use the "i" variable from the for loop in order to concatenate the strings:

@echo off
setlocal EnableDelayedExpansion
set file=%~1
set path=%2
for /F "tokens=*" %%i in (%file%) do ( 
echo %%i    rem this displays the line correctly
set "line=%path%\%%i"    rem this doesn't work
echo %line% )

What am I supposed to do there in order to get it to work as described above?

robertpas
  • 45
  • 3
  • Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – Squashman Apr 08 '21 at 13:22
  • Regardless of your question and answer being a duplicate, there is no reason to the value of your path variable and the `FOR` variable to another environmental variable. You can just use both directly with whatever command needs to use them – Squashman Apr 08 '21 at 13:23

1 Answers1

0

Found the answer. The problem was in how the line variable was echoed.

This is the modified code :

@echo off
setlocal EnableDelayedExpansion
set file=%~1
set path=%2
for /F "tokens=*" %%i in (%file%) do ( 
echo %%i    rem this displays the line correctly
set "line=%path%\%%i"    rem this doesn't work
echo !line! )

When echoing "line", it's supposed to be done with ! instead of %

robertpas
  • 45
  • 3
  • 2
    Do not create a new veriable named `path`, it will overwrite the very important system variable, which already has that name. – Compo Apr 08 '21 at 10:34