How to read lines in txt file in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
megaborg
Posts: 6
Joined: 08 Oct 2017 06:14

How to read lines in txt file in batch

#1 Post by megaborg » 04 Feb 2018 10:38

I'd like to create a program in batch to download some freeware program but i don't know how to read all lines from txt to download every files
If i use a txt like this:

VLC,http....
Skype,http....

I've no problem but i'd like to use txt like this one called list.txt:

VLC
Link http:.......

Skype
Link http:.......

Utorrent
Link http:......

etc

It's possible to do that in batch ? What do you think?
Thanks

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

Re: How to read lines in txt file in batch

#2 Post by aGerman » 04 Feb 2018 13:42

Is there any reason why you try making it difficult if you already know the simple way?

Steffen

megaborg
Posts: 6
Joined: 08 Oct 2017 06:14

Re: How to read lines in txt file in batch

#3 Post by megaborg » 05 Feb 2018 06:28

Because for every program i have not only the link but a description, command for autoinstall and other two options and I would prefer to have for every single program every single option on a different line....because all options on the same line makes a bit of confusion for me...that's the only reason... :roll:

Online i found something like this to create an array of every single line :
@echo off &setlocal enabledelayedexpansion
for /F "delims=" %%a in (List.txt) do (
set /A count+=1
set "array[!count!]=%%a"
)

for /L %%i in (1,1,%count%) do (
set /A count +=%count%
echo !array[%%i]! >>prova.txt)
)

but i don't know if its' the correct way to do what i want ,and then i don't know how to use this code for every program i've in my list.

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

Re: How to read lines in txt file in batch

#4 Post by aGerman » 05 Feb 2018 12:52

There are several possibilities to achieve this. In order to pick up something that fits best I have some additional questions.

- What exactly do you want to do with the file content? Is there anything like a menu where the user pick there choices? If so, is the menu already based on the file content?
- Are there key words (like VLC or Skype) always at the beginning of a line that you want to search for in order to pick up the link in the next line?
- Is the word "Link" really the first word in the lines with your links or did you only add it in your example. Some real data would be nice for tests (use the </> button to enclose it in code tags).

Steffen

megaborg
Posts: 6
Joined: 08 Oct 2017 06:14

Re: How to read lines in txt file in batch

#5 Post by megaborg » 27 Feb 2018 12:07

Sorry aGerman but i couldn't answer your question because my pc had a big problem...but now it's all ok.

With my program i create a file in which i have all programs selected in my menu.
The file is called selection.ini and the one below it's a little version with only 4 programs:

Code: Select all

[OPTIONS] 
Download=yes 
Install=no  
 
[PROGRAMS] 
Vlc=yes 
Ccleaner=yes 
Skype=no 
7zip=yes 
So i use a code in my batch like this:

Code: Select all

:Download
FOR /F "skip=6 tokens=1 delims==" %%G IN ('FIND /v /i "no" "Selection.ini"') do (
FOR /F "tokens=2 delims=," %%H IN ('FIND /i "%%G" "Links.txt"') do (
wget -q --show-progress -O %%G.exe %%H
))
to download program selected in selection.ini and using a file called Links.txt like this:

Code: Select all

Vlc,http://download.videolan.org/pub/videolan/vlc/3.0.0/win32/vlc-3.0.0-win32.exe,/S
Skype,http://go.skype.com/windows.desktop.download, /VERYSILENT
Ccleaner,http://download.ccleaner.com/ccsetup540.exe,/S
7zip,http://d.7-zip.org/a/7z1801.exe,/S
to extract link and silent parameter for silent install.
Infact to install program i use this code:

Code: Select all

:install
FOR /F "skip=6 tokens=1 delims==" %%G IN ('FIND /v /i "no" "Selection.ini"') do (
FOR /F "tokens=3 delims=," %%H IN ('FIND /i "%%G" "Links.txt"') do (
%%G.exe %%H
))
I would like to use a file Links.txt like the one below:

Code: Select all

Vlc
Link=http://download.videolan.org/pub/videolan/vlc/3.0.0/win32/vlc-3.0.0-win32.exe
Silent=/S

Skype
Link=http://go.skype.com/windows.desktop.download
Silent=/VERYSILENT

Ccleaner
Link=http://download.ccleaner.com/ccsetup540.exe
Silent=/S

7zip
Link=http://d.7-zip.org/a/7z1801.exe
Silent=/S
but i don't know how to create the code to download and install from this file
For me it's ok even to use for example:
Name=Vlc
instead of only Vlc in Links.txt if it's easier for batch coding
Can you help me with that? :roll: :D
Thanks

gustavo.lago
Posts: 4
Joined: 23 Feb 2018 08:52

Re: How to read lines in txt file in batch

#6 Post by gustavo.lago » 27 Feb 2018 12:47

In my case I'm doing a batch to download and modify a file written in Python. Download part is working:

Code: Select all

echo Downloading scoutr.py
bitsadmin /transfer DownloadJob /download /priority normal "http://domain.com/script.py" "%cd%\my.py" >null
Can you use:

Code: Select all

@echo off
::7Zip
set Link=http://d.7-zip.org/a/7z1801.exe
set program=7zip.exe
set Silent=/S
echo Downloading 7zip
bitsadmin /transfer DownloadJob /download /priority normal "%Link%" "%cd%\%program%" >null
%cd%\%program% %Silent%
My problem is when I try to modify a python script line by line this happens:

Input:

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging
.
.
.

Output:

Code: Select all

#/usr/bin/python 
# -*- coding: utf-8 -*- 
import logging 
.
.
.
Some characters like the "!" disappear.

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

Re: How to read lines in txt file in batch

#7 Post by aGerman » 27 Feb 2018 13:48

Just to give you an indication:

Code: Select all

@echo off &setlocal
set "list=Links.txt"
set "search=Ccleaner"

set "Link=" &set "Silent="
for /f "delims=:" %%i in ('findstr /nixc:"%search%" "%list%"') do (
  setlocal EnableDelayedExpansion
  <"!list!" (
    for /l %%j in (1 1 %%i) do set /p "="
    for /l %%j in (1 1 2) do (set "ln=" &set /p "ln=" &if defined ln set "!ln!")

    echo !Link!
    echo !Silent!
  )
  endlocal
)

pause
Steffen

megaborg
Posts: 6
Joined: 08 Oct 2017 06:14

Re: How to read lines in txt file in batch

#8 Post by megaborg » 02 Mar 2018 06:30

Thanks your code was perfect for my purpose.
Bye :P :D :D

megaborg
Posts: 6
Joined: 08 Oct 2017 06:14

Re: How to read lines in txt file in batch

#9 Post by megaborg » 11 Mar 2018 04:32

Hi aGerman,
I use your code for my program and it works perfectly but now i would have to add another option in links.txt and do something like the file below:

Code: Select all

Vlc
Link=http://download.videolan.org/pub/videolan/vlc/3.0.0/win32/vlc-3.0.0-win32.exe
Silent=/S
Description=Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
            bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
            cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

Skype
Link=http://go.skype.com/windows.desktop.download
Silent=/VERYSILENT
Description=Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
            fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
            gggggggggggggggggggggggggggggggggggggggggggggg

Ccleaner
Link=http://download.ccleaner.com/ccsetup540.exe
Silent=/S
Description=hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
            iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
            lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
                  
7zip
Link=http://d.7-zip.org/a/7z1801.exe
Silent=/S
Description=mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
            nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
            ooooooooooooooooooooooooooooooooooooooooooooooooooooo
To read Line with Description= i need only to modify last part of your code :

Code: Select all

<"!list!" (
    for /l %%j in (1 1 %%i) do set /p "="
    for /l %%j in (1 1 3) do (set "ln=" &set /p "ln=" &if defined ln set "!ln!")

    echo !Link!
    echo !Silent!
    echo !Description!
  )
  endlocal
  )
but the other 2 lines with no definitions at the beginning with your code cannot be shown in my program
I've tried some method with set /p but i'm not so expert to modify your code to show the two lines below Description=
The lines below Description are always 2 for every program
Do you think there's an easy way to do that o can you indicate some example to understand how to do it?

megaborg
Posts: 6
Joined: 08 Oct 2017 06:14

Re: How to read lines in txt file in batch

#10 Post by megaborg » 11 Mar 2018 10:29

I found a different way to solve my problem....

Code: Select all

for /f "delims=" %%a in ('findstr /nixc:"%search%" "%list%"') do (
<"%list%" (
  for /l %%j in (1 1 %%a) do set /p "="
  set /p "line1="
  set /p "line2="
  set /p "line3="
  set /p "line4="
  set /p "line5="


echo !line1:~5!
echo !line2:~7!
echo !line3:~5!
echo !line4:~5!
echo !line5:~5!

))
This works only if you know the exact number of lines to show.
Bye :D :lol:
Last edited by megaborg on 12 Mar 2018 06:36, edited 1 time in total.

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to read lines in txt file in batch

#11 Post by ShadowThief » 11 Mar 2018 16:20

megaborg wrote:
11 Mar 2018 10:29
I found a different way to solve my problem....with a different code using set /p
Bye :D :lol:
https://xkcd.com/979/

Post Reply