How to echo a multiple line text file (batch) - batch-file

I need to echo a text file with multiple lines.
Here's what I've done, but this only echo's the first line.
set /p errorlog=<errorlog.txt
echo %errorlog%
pause

If you want to include the entire file, and don't really need the environment variable, why not type the file?
type errorlog.txt
Environment variables are not intended to hold the entire contents of the file, and cannot store newlines as values. The value will terminate at the first newline.

If you want it to be stored as a variable at one point, try:
for /f "tokens=* delims=" %%G in (errorlog.txt) do (
echo %%G
)
Note that this method can allow you to only echo part of a file if you include a count function, but is slower the longer errorlog.txt is.

Related

Add all contents of a file in a single variable in Batch

Suppose I have a text file with following contents:
Hello
World
Abc
Now I want to read this in batch and copy them into a single variable. So my variable should contain:
var=Hello World Abc
What is possible work around for this?
If I am trying I am either getting the first word or the last line word.
Thanks
This seems to work. I have appended \n to each line in the read file. I'm not sure how you expect that behavior to actually work:
#echo off
SETLOCAL EnableDelayedExpansion
set "filecontents="
for /f "delims=" %%x in (input.txt) do (
set filecontents=!filecontents!%%x\n
)
echo %filecontents%
To read the contents of a file in a batch file you can use a FOR /F command. You then use the SET command to assign a value to a variable. Also, you will need to utilized delayed expansion to reassign the existing variable to the original variable. A leading space will get assigned so the echo command is using a substring to remove it.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%G IN (file.txt) do set "var=!var! %%G"
echo %var:~1%

Batch script Read multi line Argument in Variable

I wanted to take list of files to delete from user as a argument. One line per argument.
How can store the list of files separated by new line in a variable.
I am using below command.
Set DeletionFiles=${p:DeleteFiles}"
for %%i in (%DeletionFiles%) do (
echo %%i
)
Then i wanted to iterated them on a loop.
${p:DeleteFiles} will get replaced by it's value from external app, which will contain list of files separated by new line.I can not change it.
#ECHO OFF
SETLOCAL
SET "deletionfiles="
:dloop
SET "deleteme="
SET /p "deleteme=Which file to delete ? "
IF DEFINED deleteme SET "deleteme=%deleteme:"=%"
IF DEFINED deleteme SET "deletionfiles=%deletionfiles%,"%deleteme%""&goto dloop
ECHO delete %deletionfiles:~1%
GOTO :EOF
There is no need to use a newline. Your for command (or a del command) will operate perfectly happily on a comma-(or space-)separated list.
Note that there are certain characters that batch uses for special purposes and batch string-processing may not process them in the expected manner. These characters include % ^ and &.
${p:DeleteFiles} will get replaced by it's value from external app,
which will contain list of files separated by new line.I can not
change it.
After the replacement the batch file looks like:
Set DeletionFiles=file1.jpg
file2.jpg
file3.jpg
"
This isn't a valid batch file anymore.
Furthermore it's a bad idea to modify the batch file itself, as this works only once.
You could place the ${p:DeleteFiles} into another file, like input.txt.
Your batch would look like
echo ${p:DeleteFiles} > input.txt
<external program for replacing the DeleteFiles> input.txt
for /F "tokens=*" %%A in (input.txt) do (
echo File: %%A
)
If I understand you correctly, your external program will generate a list of files. You then want to store this multi-line list to a variable. What do you want to do with the variable once you have it? I assume you want to delete the files, but your question isn't clear on that point, so I'll try to over-answer to cover it.
for /f "delims=" %%a in ('{command that generates your list}') do (
echo Doing stuff to %%a...
echo %%a>>listOfFilesToDelete.txt
set var=%%a
if "%var:~0,7%"="DoNotDelete" copy "%%a" \someArchiveFolder\
del "%%a"
)
This will read each line in your generated list as variable %%a. It will then do whatever command(s) you specify. This way, you can run a command on each of the files in the list. In the above code it's
Printing each line to the console embedded in some text
Outputting it to a file
Checking the first 7 characters of the line against a specified string and then copying it to a folder if it matches
And then deleting it
If you still need to reference each line from your generated list, you can even setup an array-like structure. (See Create list or arrays in Windows Batch)
setlocal EnableDelayedExpansion
:: Capture lines in an 'array'
set /a i=0
for /f "delims=" %%a in ('dir /b') do (
set /a i+=1
set var!i!=%%a
)
:: Loop through the 'array'
for /L %%a in (1,1,%i%) do (
echo Do more stuff with !var%%a!
)
Just like above, this will read each line in your generated list as variable %%a. It will then set a variable var!i! equal to the value of the current line. You can then reference each line as var1, var2, and so on, or, as the second section shows, you can loop through them all using for /L. You'll need to get a grasp on working with delayed expansion variables, though.

batch read file without new lines

I've script that reads into variable content of files, but in some cases, files has new lines. Variable has loaded olny firest line. I use:
set /p varfile=file.txt
Is it possible to read contents of the file with ignored newlines? I cannot find solition wihtin batch commands.
Although this is probably not what you need, this is what you asked for. You can capture the entire contents of a file to a single variable (up to 8191 bytes, is it?) by using a for loop. Since for /f can read the contents of a file, you can finally eliminate that set /p statement you keep misusing.
#echo off
setlocal enabledelayedexpansion
rem // capture a line break to a variable
set BR=^
rem // Leave the two lines above blank.
for /f "usebackq delims=" %%I in ("file.txt") do (
if not defined contents (
rem // set contents variable to first line of file
set "contents=%%I"
) else (
rem // append line break + next line to variable
set "contents=!contents!!BR!%%I"
)
)
echo(!contents!
Now, what are you going to do with this variable now that it contains the entire contents of a text file?

put contents of a text file in commandline

in a batch I have
echo VirtualDub.video.AddComment^(0x0000000C,"","%tc%"^)^;>>v:\automazioneclip\virtualdubmod\temp\%%~na.vcf
but now in place of %tc% I would like insert the contents of a text file, all content of a text file
How I have to modify it? thanks
Use SET /P to print out the first portion of the line without a newline. Then use TYPE to print out the contents. Then finish up with a normal ECHO.
<nul (
set /p ^"=VirtualDub.video.AddComment^(0x0000000C,"",""
type file.txt
(echo ^"^);)
) >>"v:\automazioneclip\virtualdubmod\temp\%%~na.vcf"
Note that the closing quote after the file contents will appear on the next line if the file ends with a newline. Obviously the value will be spread across multiple lines if the file contains multiple lines. Multiple lines may or may not be a problem depending on the language of the code you are writing.
Related question: How do you loop through each line in a text file using a windows batch file?
So possibly something like:
for /F "tokens=*" %%A in (myfile.txt) do [process] %%A
where "process" is your line above.
for /F "tokens=*" %%A in (myfile.txt) do echo VirtualDub.video.AddComment^(0x0000000C,"","%%A"^)^;>>v:\automazioneclip\virtualdubmod\temp\%%~na.vcf

Modifying For loop variables cmd

Thing I want to do is to filter out specific video file extensions from a text file containing various video file names e.g filename.txt contents are
Red.mp4
Yellow.mp4
Blue.mp4
Orange.wmv
Purple.wmv
Now I will parse this file for .mp4 only & utilise 'for parameter' to make several .txt files each containing particular code for a particular file. In other words will create number of files same as number of .mp4 video in filename.txt My code
::==
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('type filename.txt ^|findstr ".mp4"') do (
set /a n+=1
echo >myfile!n!.txt
set v!n!=x264 --crf 23 --level 3.1 --tune film --o "%%a" "%%a.mkv"
)
set v
pause
::==
I have two problems:
How do I modify %%a to have its .mp4 string removed because when I apply "%%a.mkv" a file will be named *.mp4.mkv and I don't want that and .mp4 can be typed manually like --o "%%a.mp4" "%%a.mkv"
Provided that required txt files are already created how do I pass variables v1,v2,v3... to its respective text file I tried with
echo %v!n!% > >myfile!n!.txt
within loop but it didn't work, so plz advice
I don't see any need to create an array of variables with one loop, and then add the content to files in a 2nd loop. Simply do everything in one loop.
Also, there is no need to create an empty file, and your attempt to do so is incorrect - it adds the line ECHO is off. to each file. To echo a blank line you should use >filename ECHO(. To create an empty file use copy nul filename.
This is unlikely to be a concern, but it is possible for a filename to contain !. Expansion of a FOR variable containing ! will be corrupted if delayed expansion is enabled. The issue can be solved by toggling delayed expansion on and off within the loop, but there is another solution: use another FINDSTR command to prepend each line with the line number, and then parse the line number and file name with the FOR loop. This eliminates the need to increment a counter and eliminates the need for delayed expansion.
The ~n FOR variable modifier is used to get the name without the extension.
#echo off
for /f "tokens=1* delims=:" %%A in (
'findstr /lie ".mp4" filename.txt^|findstr /n "^"'
) do (
>"myfile%%A.txt" echo x264 --crf 23 --level 3.1 --tune film --o "%%B" "%%~nB.mkv"
)

Resources