#1 21 Dec 2020 08:54

Minecraft49
Member
From: Poland
Registered: 08 Aug 2020
Posts: 10
Website

Reading multiline file without FOR loops

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

#2 21 Dec 2020 12:30

pappaG
Member
Registered: 04 Jan 2020
Posts: 6

Re: Reading multiline file without FOR loops

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

#3 21 Dec 2020 14:01

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 360

Re: Reading multiline file without FOR loops

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

#4 21 Dec 2020 16:08

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 196

Re: Reading multiline file without FOR loops

FOR is the single most important command in batch. I highly recommend you learn how to use it.

Offline

#5 23 Dec 2020 07:31

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,123

Re: Reading multiline file without FOR loops

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

Board footer

Powered by FluxBB