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
Related
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.
Okay I have several lines in a text file. I want to get the first line and save it in another file. For example this is the text file:
put returns between paragraphs
for linebreak add 2 spaces at end
for linebreak add 2 spaces at end2
for linebreak add 2 spaces at end3
I want put returns between paragraphs to be saved into another file.
I used
for /f "tokens=" %%A in ('findstr /r "^[0-9][0-9]*$" <"C:\Users\Sherlock\Desktop\AbcImport\123.txt"') do echo 123>>1234.txt
pause
But it doesn't work at all.
How to get just the first line of a text file written into a new text file using a batch file?
Option 1 - SET /P : This is the simplest and fastest pure batch solution, provided the line does not exceed 1021 bytes, and it does not end with control characters that must be preserved. The size of the file does not matter - it will always read and write the first line very quickly.
#echo off
setlocal enableDelayedExpansion
set "ln="
<"input.txt" set /p "ln="
>"output.txt" (echo(!ln!)
Option 2 - FOR /F : This will work with lines up to ~8191 bytes long, but it can be slow if the file is really large because the FOR /F loop must read the entire file before it processes the first line. This solution is basically the same as the Mofi answer, except it disables the EOL option, so it never ignores the first line, regardless what the first character is. It does have a limitation that it will skip empty lines, so technically it does not give the correct result if the first line is empty:
#echo off
for /f usebackq^ delims^=^ eol^= %%A in ("input.txt") do echo(%%A>"output.txt"&goto :break
:break
There is a way to preserve the first line if it is empty using pure batch, but I would not bother. I would move on to ...
Option 3 - JREPL.BAT, or some other non-batch solution : Batch is quite poor at manipulating text files. You are much better off using some other scripting language like VBScript, JScript, or Powershell. Or a Windows port of any number of unix utilities.
I would use JREPL.BAT - a hybrid JScrpit/batch regular expression text processing utility that runs natively on any Windows machine from XP onward. It is way overkill for such a simple task, but it is an extremely handy, powerful, and efficient tool to have in your arsenal. Once you have it, then it can be used for many text processing tasks. Full documentation is embedded within the script.
jrepl "^.*" "$&" /jendln "quit=true" /f "input.txt" /o "output.txt"
Use CALL JREPL if you put the command within a batch script.
Here is the batch code to write just first non blank/empty line of a text file into another text file.
#echo off
for /F "usebackq delims=" %%I in ("InputTextFile.txt") do (
echo %%I>"OutputTextFile.txt"
goto ContinueAfterLoop
)
:ContinueAfterLoop
InputTextFile.txt is the file in current directory containing the first line to copy.
OutputTextFile.txt is the file created in current directory with first line from input file copied into this output file.
The command GOTO is used to exit the loop after first line is processed and continue the batch file below the loop.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
Read also the Microsoft article about Using Command Redirection Operators.
You can use use this command:
SetLocal EnableDelayedExpansion
for /f "tokens=* delims=;" %%m in ("C:\Users\Sherlock\Desktop\AbcImport\123.txt") do (
set /p FirstLine=<%%m
echo !FirstLine!>>1234.txt
)
and for multiple file:
SetLocal EnableDelayedExpansion
for %%a in ("*") do (
for /f "tokens=* delims=;" %%m in ("%%a") do (
set /p FirstLine=<%%m
echo !FirstLine!>>1234.txt
)
)
rem Get the first line of a text file:
set /P "line=" < "C:\Users\Sherlock\Desktop\AbcImport\123.txt"
rem Write it into a new text file:
echo %line%> 1234.txt
I'm not sure if this is possible, but is there a way for a batch file to read a text file, but skip lines that do not start with a number?
For example:
handled
219278
check
219276
control
219274
co
219268
Can a for loop skip handled, check, control, etc.?
I know this:
cscript C:\Users\c1921\Test\curltest.vbs"!$MyVar!">>C:\Users\c1921\Test\Datapoints\!$MyVar!.txt
Will put all output to this text file but this:
FOR /F %%i in (C:\Users\c1921\Test\Datapoints\!$MyVar!.txt) DO (
set "$UnitID=%%i"
)
Reads every line into a variable. Can I somehow use delims and tokens to only get the numbers?
Edit:
I thought this might be possible going off of an answer on this question: Windows Batch file to echo a specific line number
The file I have on occassion might not have a number between the words for example:
handled
check
219276
control
219274
co
219268
This should not happen often, but I'd like to make sure I can avoid this when it does.
FINDSTR /b "[0-9]" q25003233.txt
I used a file named q25003233.txt containing your data for my testing.
for /f "delims=" %%a in ('findstr /r /b /c:"[0-9]" "c:\somewhere\file.txt"') do echo %%a
This uses findstr to filter the input file with a regular expresion , returning only lines that start with a number
I've searched a thousand of example and tried, but none of them actually works for me. My requirement is pretty straight forward, I have a file - config.txt, it has one of lines:
sqlServer=localhost
I'm trying to update this line to:
sqlServer=myMachine\sql2012
I looked examples online, some of them are just working with set variables, or some are not replacing but inserting. There are some examples are writing into a new file, but the new file has line number in front of each line. I don't find a useful instruction how to write batch scripts, and none of the update file batch scripts works for me.
It will be very helpful if you leave some comments.
Thanks in advance
EDITED - to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
set "config=.\config.txt"
set "dbServer=localhost\sql2012"
for /f "tokens=*" %%l in ('type "%config%"^&cd.^>"%config%"'
) do for /f "tokens=1 delims== " %%a in ("%%~l"
) do if /i "%%~a"=="sqlServer" (
>>"%config%" echo(sqlServer=%dbServer%
) else (
>>"%config%" echo(%%l
)
type "%config%"
endlocal
Input file is read line by line (for /f %%l), then each line is split (for /f %%a) and if the first token in the line is "sqlserver" then the line is replaced, else the original line is sent to file.
The command used to retrieve the information in the first for loop includes an cd.>"%config%" that will remove the contents of the file, so the final resulting lines (that have been read in memory by the for command before removing them) are sent directly to the same file.
You can do this:
FINDSTR /I /V /B "sqlServer=" config.txt > config.new
ECHO sqlServer=myMachine\sql2012 >> config.new
DEL config.txt
REN config.new config.txt
The FINDSTR will remove all lines that start with sqlServer= and create a new file called newfile.
The ECHO will add a line at the end with sqlServer=MyMachine\sql2012.
The last two lines delete your existing config.txt and replace it with the output of the first two lines.
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"
)