How to make a variable that holds multiple lines by reading from a text file in batch or cmd [closed] - batch-file

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I have this text file containing multiple lines of text (message.txt) and I wanted my variable have the value of it.
set /p message=<message.txt
this will only take the first line
type message.txt
will read the full text but i wasn't able to set is as a variable

Simplest way
for /f "tokens=1delims==" %%b in ('set message') do set "%%b="
for /f "tokens=1*delims=:" %%b in ('findstr /n /R ".*" message.txt') do set "message%%b=%%c"
set message
for /f "tokens=1*delims==" %%b in ('set message') do echo %%c
The first for line clears all the message variables.
The second for line reads the data from the file; the set shows the values read
The third for line shows the values of the message variables.
Note that this simple method is limited to 9 lines (beyond that, you need to actually program the retrievel of the message variables) and will delete any leading : in any line of message.txt

Here is an example using an array in batch to store multiple lines of text read from a file.
#echo off
setlocal enabledelayedexpansion
set idx=0
for /f "usebackq delims=" %%i in ("message.txt") do (
set /a idx+=1
set "line[!idx!]=%%i"
)
echo Number of lines: %idx%
for /l %%j in (1,1,%idx%) do (
echo Line %%j: !line[%%j]!
)
pause
This code uses a for loop to read each line of the message.txt file and store each line in a separate element of the line array.
The variable idx is used to keep track of the number of lines read from the file.
After reading the file, the code outputs the number of lines and then uses another for loop to output each line stored in the line array.
Note the use of setlocal enabledelayedexpansion to enable delayed expansion, which allows the use of variables within a block of code even if their values are changed within the block.

The usual method to read a file is load each line in a different array element.. However, if you want to load all lines in the same variable, you can do it this way:
#echo off
setlocal EnableDelayedExpansion
call :readLines message=<message.txt
rem Standard expansion expands just the first line:
echo %message%
rem Delayed expansion expands all lines:
echo !message!
goto :EOF
:readLines variable < file
set /P "%1="
:nextLine
set "line="
set /P "line="
if not defined line exit /B
set ^"%1=!%1!^
%Don't remove this line%
%line%^"
goto nextLine

Related

Combine lines in text file using batch

I want to make a program that takes the content of the second line of a text file and puts it on the first. (It doesn't matter if the second doesn't get edited)
for /f "tokens=1" %%t in (file.txt) do set string1=%%t
for /f "tokens=2" %%t in (file.txt) do set string2=%%t
echo %string1%%string2%>file.txt
I have two issues hat I can't seem to be able to fix.
One: the loops only include the first word of each line in the variables.
Two: Echo doesn't replace the first line of the file with the variables given and instead writes ECHO command deactivated (I have the French version of Windows 10 and simply translated what got written in the file, the text in English Windows version might be slightly different, but you get the idea)
If you have any suggestions, I would appreciate if you explain what the code you provide does (I always like to learn)
Your question is not clear and can be understood in several different ways. Anyway, this management is simpler with no for command:
#echo off
setlocal EnableDelayedExpansion
< file.txt (
rem Takes the content of the first line
set /P "line1="
rem Takes the content of the second line and puts it on the first
set /P "line2="
echo !line1!!line2!
rem It doesn't matter if the second line doesn't get edited
echo !line2!
rem Copy the rest of lines
findstr "^"
) > output.txt
move /Y output.txt file.txt
The FOR command uses a space as a delimiter by default. So you have to tell it to not use any delimiters with the DELIMS option. Also, you should be able to do this with a single FOR /F command. Just hold the previous line in a variable.
#ECHO OFF
setlocal enabledelayedexpansion
set "line1="
(for /f "delims=" %%G in (file.txt) do (
IF NOT DEFINED line1 (
set "line1=%%G"
) else (
echo !line1!%%G
set "line1="
)
)
REM If there are an odd amount of lines, line1 will still be defined.
IF DEFINED line1 echo !line1!
)>File2.txt
EDIT: I think I completely misunderstood your question. Once you clarify your question I will repost a code solution if needed.
Use skip to omit the first line and write the 2nd line twice. In general an edit of a file implies a rewrite to a new file and possibly a rename to retain the old file name.
:: Q:\Test\2018\07\25\SO_51508268.cmd
#Echo off
Set "flag="
( for /f "usebackq skip=1 delims=" %%A in ("file1.txt") Do (
If not defined flag (
Echo=%%A
Set flag=true
)
Echo=%%A
)
) >file2.txt
Del file1.txt
Ren file2.txt file1.txt
After running the batch a file1.txt with initially numbered lines 1..5 looks like this:
> type file1.txt
2
2
3
4
5

Batch: convert pipe delimited text file to comma delimited csv file

I have a .txt file like this:
Customer Number||Customer Name||Partner Number||Partner Name||Customer Country
1ABC||Jame||1234||Anny||USA
2DEF||Susan||5678||Prissy||UK
My output should be a .csv file with no empty columns.
This is what I tried:
#echo off
setlocal disableDelayedExpansion
set input="C:\a.txt"
set output="C:\Customer_Pipe.csv"
>%output% (
for /f "tokens=*" %%a in ('input"') do (
set line=%%a
setlocal enableDelayedExpansion
echo "!line:|=","!">>"%~2"
)
)
If you want to read the file stored in variable INPUT, you need to read it as %INPUT% to do that (you stated INPUT literally, with odd quotation). When you specify '' within the set of for /F, the part within () is interpreted as a command rather than a file, so for /F tries to execute command input which cannot be found and so the script fails. I stringly recommend to put "" around the specified file, together with the usebackq option, in order to avoid trouble with white-spaces in paths/file names.
If you want to write into the file stored in variable OUTPUT, you must not redirect the echo to somewhere else (you placed >>"%~2" in the code).
Here is the code as I would write it:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "INPUT=%~1"
set "OUTPUT=%~2"
if not defined INPUT exit /B 1
if not defined OUTPUT set "OUTPUT=con"
> "%OUTPUT%" (
for /F usebackq^ delims^=^ eol^= %%L in ("%INPUT%") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
echo(!LINE:^|^|=,!
endlocal
)
)
endlocal
exit /B
The input file for the batch program must be provided as the first command line argument. The second argument (optional) is the output file.
Note that this script does not check whether the input data contains , characters, which impact the way the converted file is treated. If you want to put quotation marks around all fields of the returned CSV data to avoid data interpretation troubles, replace the above echo command line by:
echo("!LINE:||=","!"
The output data (with the original echo command line) looks like this:
Customer Number,Customer Name,Partner Number,Partner Name,Customer Country
1ABC,Jame,1234,Anny,USA
2DEF,Susan,5678,Prissy,UK
The output data with the modified echo command line looks like this:
"Customer Number","Customer Name","Partner Number","Partner Name","Customer Country"
"1ABC","Jame","1234","Anny","USA"
"2DEF","Susan","5678","Prissy","UK"
There are two problems in your code.
1)
You use setlocal EnableDelayedExpansion, that's a good idea, but you have to close it with endlocal else you get an error after ~32 setlocals.
2)
Your replace functions looks a bit odd.
You add some quotes, they will be part of the output later.
You could try this.
echo(!line:^|^|=,!
At all
>%output% (
for /f "tokens=*" %%a in (%input%) do (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:||=,!"
(echo(!line!)
endlocal
)
)

How define a big array in Batch?

I want to define a big array (>400 keys) in batch, but when I execute my script the windows close. I use this setting:
set FILE_LIST=(filename1.xxx [...] filename450.yyy)
Some help? Thx
A Windows Batch file have a limit in the value of each variable to 8192 characters, including the name of the variable and the equal sign. If the value of each "filename#.xxx " have 16 characters, you may store up to 8192/16=512 file names in one variable; to do that, you must use Batch commands. For example:
#echo off
setlocal EnableDelayedExpansion
set "FILE_LIST="
for /L %%i in (1,1,450) do set "FILE_LIST=!FILE_LIST!filename%%i.xxx "
echo FILE_LIST=%FILE_LIST%
Please, note that previous variable is a list, NOT and array. To define an array, use this method:
#echo off
setlocal EnableDelayedExpansion
for /L %%i in (1,1,450) do set "FILE_ARRAY[%%i]=filename%%i.xxx"
echo FILE_ARRAY:
set FILE_ARRAY
There is a limit of 64 MegaBytes for the total space occupied by all variables.
For a detailed description of arrays and other data structures in Batch files, see: Arrays, linked lists and other data structures in cmd.exe Batch script
EDIT: Reply to the comments
The Batch file below assume that there is one file name per line in the .txt file, and that file names does not include exclamation marks:
#echo off
setlocal EnableDelayedExpansion
rem Load the .txt file in FILE_ARRAY elements:
set num=0
for /F "delims=" %%a in (fileList.txt) do (
set /A num+=1
set "FILE_ARRAY[!num!]=%%a"
)
rem Process the FILE_ARRAY elements:
for /L %%i in (1,1,%num%) do echo Processing: %%i- "!FILE_ARRAY[%%i]!"
I finaly use this way:
set FILE_ARRAY[0]=filename1.xxx
set FILE_ARRAY[1]=filename2.yyy
set FILE_ARRAY[2]=filename3.zzz
for /F "tokens=2 delims==" %%i in ('set FILE_ARRAY[') do (
echo %%i
)
Thanks for your answers.
Seems like you might be hitting a batch file limitation (link), The following link describes a WA for this issue as dumping what you want in a var into a file, then reading that file back in when you need that massive var.
Ah batch, and your endless workarounds...

Batch File To Copy Every Line In .txt File And Then Create New .txt File For Each Line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have .txt file contain 297 line, i want every line of that copied into new .txt file, so it will contain 297 file, for file name each file like this line1.txt line2.txt line3.txt, it possible using batch?
I have try using findrepl.bat and instruction form here batch to copy FIRST line of multiple text files but thats script for first line only.
FOR /F is what you want, with the use of SET /A to numerically increment a variable. Because CMD/batch is really only a punch card reader pretending to be a shell, the contents of source_file.txt can cause the script to break. In particular, if it has any special shell characters (like < > ! " or &), the shell will interpret those as special characters and not just echo them to the output file.
SETLOCAL ENABLEDELAYEDEXPANSION
SET LINENO=1
FOR /F "delims=" %%l IN (source_file.txt) DO (
ECHO %%l>file!LINENO!.txt
SET /A LINENO=LINENO+1
)
This should split a file and create separate files for every line.
The filenames will be the same as each line.
#echo off
for /f "delims=" %%a in (file.txt) do >>"%%a.txt" echo %%a
setlocal enableextensions disabledelayedexpansion
for /f "usebackq tokens=1,* delims=:" %%a in (
'findstr /n "^" "file.txt"'
) do echo(%%b>line%%a.txt
endlocal

Batch file to create multiple variables from single lines in a text file

I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)

Resources