I looked through all of the previously asked similar questions but couldn't find the answer.
I have a batch file that creates new batch files with specific text inside them, the text is taken from a specific directory. It works great, but right now a lot of code is repeated multiple times, and should be automated.
Here is the working code that I have:
set /p x=<"path were all the source files are kept\FileOne.txt"
set x=%x:~20%
set y="C:\Program Files\TightVNC\tvnviewer" -password=zzz %x%
break> FileOne.bat
echo %y% >>FileOne.bat
First line opens a specific directory, opens a text file and saves the first line of it as x.
The second then removes the first 20 symbols from it, leaving only the needed part.
The third line sets Y to the command I need inside the newly created batch file and puts X at the end of it.
Then Y is saved as an output .bat file.
This code is repeated again and again for FileTwo, FileThree, FIleFour and so on.
Here is how I tried to turn it into a loop:
etlocal enabledelayedexpansion
FOR %%A in (FileOne FileTwo) DO (
set /p x=<"path were all the source files are kept\%%A.txt"
set x=%x:~20%
set y="C:\Program Files\TightVNC\tvnviewer" -password=zzz %x%
break> %%A.bat
echo %y% >>%%A.bat
)
The two files are created as planned, but both of them had Echo is Off inside (or, when I turned it on, Echo is On).
Here is the console output:
C:\>setlocal enabledelayedexpansion
C:\>FOR %A in (FileOne FileTwo) DO (
set /p x= 0<"path were all the source files are kept\%A.txt"
set x=~20
set y="C:\Program Files\TightVNC\tvnviewer" -password=zzz %x%
break1>%A.bat
echo 1>>%A.bat
)
C:\>(
set /p x= 0<"path were all the source files are kept\FileOne.txt"
set x=~20
set y="C:\Program Files\TightVNC\tvnviewer" -password=zzz %x%
break1>FileOne.bat
echo 1>>FileOne.bat
)
C:\>(
set /p x= 0<"path were all the source files are kept\FileTwo.txt"
set x=~20
set y="C:\Program Files\TightVNC\tvnviewer" -password=zzz %x%
break1>FileTwo.bat
echo 1>>FileTwo.bat
)
I see that it did make the loop, and created the files, but some commands look corrupt for some reason. Is there a special way I need to declare and use other variables inside a loop in a .bat file?