You are not logged in.
The most common way to read text files inside batch scripts is using a FOR /F loop. Because FOR loops are always making a lot of problems and decrease the code readability, I tried to find another, simpler method to read files - and I found:
set /p destination_variable= <myfile.txt
It works fine with single-line files, but when I am trying to read a multi-line file, I am getting only the first line. Is it possible to read a multi-line file without using FOR?
Offline
it all depends on how big the file is that your reading, there are limitations on variable lengths. not sure but I think it's just over 200 characters.
so if what your reading to variable is over that, could be the problem
I also have seen that a registry setting could change this behavior in windows 10.
also have a good read at the set command https://ss64.com/nt/set.html
Last edited by pappaG (21 Dec 2020 12:46)
Offline
My suggestion is that rather than avoid FOR loops, you embrace them. Use them and you will become familiar with them. If you avoid them you will be giving up a LOT of power and versality.
The biggest problem people have with FOR loops is that they don't understand the difference between LOAD time behavior and RUN time behavior. In simple lines of code all variables are expanded at LOAD time. If that variable is modified during RUN time, its new value is available immediately. In FOR loops and lines within parenthesis, the entire FOR loop or lines within parenthesis are expanded at load time as if they were 1 line. Therefore, values of variables modified within are not available until outside the loaded lines. You can often get around this by using SETLOCAL ENABLEDELAYEDEXPANSION, but that causes problems with "!" characters. See my post SETLOCAL ENABLEDELAYEDEXPANSION were you can see this for yourself.
Windows Shell Scripting and InstallShield
Offline
FOR is the single most important command in batch. I highly recommend you learn how to use it.
Offline
As otherwise stated FOR is most certainly the correct tool for this job, however, you might be able to do something like this:
@echo off
if "%~1" EQU "read" (
call :readline
) ELSE (
call "%~0" read < yourfile.txt
)
goto :EOF
:readline
set /p "line="
if errorlevel 1 goto :EOF
echo read line "%line%"
goto :readline
I haven’t tested this code, so it may not work. Even if it does it’s a long way to go around to achieve something that can be done more succinctly with FOR. There is also a limitation that empty lines would be treated as end of file.
Last edited by bluesxman (23 Dec 2020 07:32)
cmd | *sh | ruby | chef
Offline