New technic: set /p can read multiple lines from a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New technic: set /p can read multiple lines from a file

#31 Post by aGerman » 15 May 2016 07:44

To be honest I would rather append the data at the end of the file. Then you can simply skip the batch code (e.g. by getting the line number of the last occurrence of "exit /b" or "goto :eof" using FINDSTR /N.

Code: Select all

@echo off
for /f "delims=:" %%i in ('findstr /nir /c:"\<exit[ ]*/b\>" /c:"\<goto[ ]*:eof\>" "%~fs0"') do set /a EndOfBatch=%%i
for /f %%i in ('type "%~fs0"^|find /c /v ""') do set /a EndOfFile=%%i

setlocal EnableDelayedExpansion
<"%~fs0" (
  for /l %%i in (1 1 %EndOfBatch%) do set /p "="
  for /l %%i in (%EndOfBatch% 1 %EndOfFile%) do (
    set "line=" &set /p "line="
    if defined line echo(!line!
  )
)
endlocal

pause
Exit /B

Help=0
Args=0
Set=0
Defaults=0
Options=0
LastRun=0
SaveRun=0


Regards
aGerman

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: New technic: set /p can read multiple lines from a file

#32 Post by Aacini » 15 May 2016 09:27

I don't see any advantage on using this method vs. a series of SET commands:

Code: Select all

set Help=0
set Args=0
set Set=0
set Defaults=0
set Options=0
set LastRun=0
set SaveRun=0

Antonio

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New technic: set /p can read multiple lines from a file

#33 Post by aGerman » 15 May 2016 09:59

Aacini wrote:I don't see any advantage on using this method vs. a series of SET commands

Neither do I. It was only an example to meet the topic :wink:

Regards
aGerman

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: New technic: set /p can read multiple lines from a file

#34 Post by thefeduke » 15 May 2016 11:18

Aacini wrote:I don't see any advantage on using this method vs. a series of SET commands:

Code: Select all

set Help=0
set Args=0
set Set=0
set Defaults=0
set Options=0
set LastRun=0
set SaveRun=0

Antonio
This presumes an intention of setting those values, not creating a test file to process by later code. Both Set /P approaches can handle examples such as:

Code: Select all

111
aaa
222
bbb
333
and:

Code: Select all

The quick brown fox
jumped
without warning
By the way, neither preserves blank lines. aGerman's code discards them and mine erroneously repeats the previous line. I'll work on that because a blank line might be appropriate in an input test file. [Original post was corrected to pass null lines. (I am not clever enough to count spaces with SET /P.)]

John A.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New technic: set /p can read multiple lines from a file

#35 Post by aGerman » 15 May 2016 12:41

thefeduke wrote:neither preserves blank lines

That's the reason why you have to undefine the variable before you read the next line. I discarded blank lines because I actually read one line more than the file has.
Simple correction:

Code: Select all

@echo off
for /f "delims=:" %%i in ('findstr /nir /c:"\<exit[ ]*/b\>" /c:"\<goto[ ]*:eof\>" "%~fs0"') do set /a EndOfBatch=%%i+1
for /f %%i in ('type "%~fs0"^|find /c /v ""') do set /a EndOfFile=%%i

setlocal EnableDelayedExpansion
<"%~fs0" (
  for /l %%i in (2 1 %EndOfBatch%) do set /p "="
  for /l %%i in (%EndOfBatch% 1 %EndOfFile%) do (
    set "line=" &set /p "line="
    echo(!line!
  )
)
endlocal

pause
Exit /B
The quick brown fox
jumped
over the lazy dog

without warning

Regards
aGerman

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: New technic: set /p can read multiple lines from a file

#36 Post by thefeduke » 15 May 2016 15:24

aGerman,
I prefer to view mine as the correction of an error and yours as an adjustment for changing requirements. :) Thanks for the pleasant collaboration.
John A.

ideleon007
Posts: 3
Joined: 12 May 2016 14:50

Re: New technic: set /p can read multiple lines from a file

#37 Post by ideleon007 » 16 May 2016 08:01

aGerman, Thank you,
that did exactly what I wanted it to do.
I don't understand it, but will be studying it!
I am planning to run this batch file with elevated rights for any user that feels their
machine may be compromised by malware or whatever.
This is one of the most common locations that Malware likes to hide,
and so, I wanted to clean this area out, then continue to give them the ability to
run a full virus scan on their machine.
This will or may prevent another call for viral threat.
Thanks again for your help,
Israel
:mrgreen:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New technic: set /p can read multiple lines from a file

#38 Post by aGerman » 16 May 2016 13:01

You're welcome! I added some remarks to the code. Also have a look at the Command Index for further reading about FOR /D, RD /S /Q, and DEL /F /Q.

Regards
aGerman

Post Reply