How to read lines in txt file in batch
Moderator: DosItHelp
How to read lines in txt file in batch
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
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
Re: How to read lines in txt file in batch
Is there any reason why you try making it difficult if you already know the simple way?
Steffen
Steffen
Re: How to read lines in txt file in batch
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...
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.
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.
Re: How to read lines in txt file in batch
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
- 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
Re: How to read lines in txt file in batch
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:
So i use a code in my batch like this:
to download program selected in selection.ini and using a file called Links.txt like this:
to extract link and silent parameter for silent install.
Infact to install program i use this code:
I would like to use a file Links.txt like the one below:
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?
Thanks
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
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
))
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
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
))
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
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?
Thanks
-
- Posts: 4
- Joined: 23 Feb 2018 08:52
Re: How to read lines in txt file in batch
In my case I'm doing a batch to download and modify a file written in Python. Download part is working:
Can you use:
My problem is when I try to modify a python script line by line this happens:
Input:
Output:
Some characters like the "!" disappear.
Code: Select all
echo Downloading scoutr.py
bitsadmin /transfer DownloadJob /download /priority normal "http://domain.com/script.py" "%cd%\my.py" >null
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%
Input:
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
.
.
.
Output:
Code: Select all
#/usr/bin/python
# -*- coding: utf-8 -*-
import logging
.
.
.
Re: How to read lines in txt file in batch
Just to give you an indication:
Steffen
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
Re: How to read lines in txt file in batch
Thanks your code was perfect for my purpose.
Bye
Bye
Re: How to read lines in txt file in batch
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:
To read Line with Description= i need only to modify last part of your code :
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?
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
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
)
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?
Re: How to read lines in txt file in batch
I found a different way to solve my problem....
This works only if you know the exact number of lines to show.
Bye
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!
))
Bye
Last edited by megaborg on 12 Mar 2018 06:36, edited 1 time in total.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States