I am creating a small batch application and I would like to know how to to put file contents into variables or wildcards. (Don't know which one it is) I used to be able to do "set blablabla=< MainChat.txt" but it is not working. It would be very helpful if someone could tell me how to load one line of a file into a variable, and the next line into another.
read file line by line including empty lines -> http://www.dostips.com/forum/viewtopic.php?p=9611
-OR-
processing text file:
for /f "useback tokens=* delims=" %%# in ("c:\text.file") do (
set "current_line=%%#"
)
more info here: http://ss64.com/nt/for_f.html
Related
I know pretty much nothing about batch programming and I would like to do the following thing.
I have a .txt file that could have one or several lines and I would like to cut the last line and insert it into another .txt file. I have been doing some research before asking this question but I couldn't find anything "understandable" to me in order to achieve this.
Also, the most appropriate QA found, was this link on Stack but I don't know how to "translate" it.
This are my lines within the .txt file
S,1,______,_,__;Cutii carton 370*290*2;1.00;1.000;1;1;1;0;0;
T,1,______,_,__;0;1.00;;;;
I would like to cut the line starting with "T" from this .txt file and insert into a new .txt file.
Could anyone help me? Thanks
EDIT
Please see the print screen
get the last (non-empty) line from a text file:
for /f "delims=" %%A in (ttt.txt) do set last=%%A
appending it to another file:
echo %last%>>ttt_lastline.txt
Using this:
#For /F %%A In ('Find /C /V ""^<"%~1"') Do #Set/A _=%%A-1
#(More +%_% "%~1")>"%~dpn1_lastline.txt"
Just drag and drop your text file onto the batch file and the last non empty line will be output into a text document in the same directory as the dropped file. The output file will have the same name as the dropped file but with _lastline appended to the end.
[Edit]If you want to hard code the file name then:
#For /F %%A In ('Find /C /V ""^<"File.txt"') Do #Set/A _=%%A-1
#(More +%_% "File.txt")>"File_lastline.txt"
You may prefer the following method although it will likely be slower especially as File.txt gets larger:
#For /F "UseBackDelims=" %%A In ("File.txt") Do #Set "last=%%A"
#(Echo %last%)>"File_lastline.txt"
To cut the first "n" lines of several files and concatenate them sequentially into one, you can use:
For %f in (list-filename*.txt) do more +n(number of lines to be removed) > type >> Outputfilename.txt %f
I need help with a batch file to do the (I suppose simple) task of reading info from a master file between two "tags" and overwriting current info in other file with the info from this master file.
Here is my situation as a simple example:
I have a file called "template.htmltplt" that is my master file if you will. Then I have a bunch of other ".html" files.
I would like the batch file to:
Go through all the html files
Delete all the lines between <!--Stuff-Start--> and <!--Stuff-End-->
Copy the content between the <!--Stuff-Start--> and <!--Stuff-End--> tags in the template.htmltplt into their correct place in the other html files.
Is this even possible and if so how?!
I have NO bat script knowledge so well commented code would be awesome!
Thanks in advance for those willing to help!
Regards,
Reinhardt
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "startstring=<!--Stuff-Start-->"
SET "endstring=<!--Stuff-End-->"
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
(
SET "block="
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "^" q23715314.txt') DO (
IF "%%b"=="%endstring%" SET "block="
IF DEFINED block ECHO(%%b
IF "%%b"=="%startstring%" SET block=Y
)
)>"%tempfile%r"
FOR /f "delims=" %%t IN ('dir /b /a-d %sourcedir%\*.html') DO (
SET "block="
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "^" "%sourcedir%\%%t"') DO (
IF "%%b"=="%endstring%" SET "block="
IF NOT DEFINED block ECHO(%%b
IF "%%b"=="%startstring%" SET block=Y&TYPE "%tempfile%r"
)
)>"%destdir%\%%~nt.html"
del "%tempfile%*"
GOTO :EOF
Not hard.
I used a file named q23715314.txt containing this data for my testing:
drop <this> line
<!--Stuff-Start-->
Insert this
and this
and even <this> line after an empty line
<!--Stuff-End-->
omit this
leave this out
And test .html file:
leave <this> line
empty line retained
<!--Stuff-Start-->
Replace this
replace this too
and substitute for <this> line
<!--Stuff-End-->
keep this
retain this too
resultant new .html file:
leave <this> line
empty line retained
<!--Stuff-Start-->
Insert this
and this
and even <this> line after an empty line
<!--Stuff-End-->
keep this
retain this too
Naturally, you'd need to set your own file and directory names, and I'd advise strongly against trying to use the same directory for the source and destination. That won't work at all. And any .html source line that starts with one or more colons will have those colons stripped-out - not that many .html lines start with colons, but it needs to be said...
How it works - block by block.
The first part would seem obvious. The directories involved are defined and the target strings, too.
Next there's a create-a-tempfile routine. Simply generate a random filename and see whether there is an existing matching filename in directory %temp%. Personally, I set up temp to be c:\temp but the code is designed to use the default. So, if the random-number generator chooses 18749 then the code looks for any file 18749... in the temporary directory. If such a file exists, then choose another random number. If it doesn't create a file named 18749a in the temporary directory. This is simply a placeholder.
Next step is to extract the required lines to a temporary file. the (block of code)>filename syntax directs any data echoed to a new file in filename - which should contain the full filename of a valid file in the temporay directory; for example c:\temp\18749r.
The code within the block first sets block to empty, then reads the file q23715314.txt line-by-line, numbering each line by prefixing it with number:. This ensures that empty lines are processed as 13:, otherwise they'd be skipped. The q23715314.txt isn't significant - it can be any file containing the required template data. I simply use qSOquestionnumber.extension in order that I can keep the data in files related to the batch file I write (called qSOquestionnumber.bat) - so the many questions using file1 and file2 can be easily individually retrieved in the case of a problem. The temporary file could be any valid filename you like, if you want to have a fixed filename. Note however that filenames containing spaces and some other symbols will need to be "quoted".
Since each line is processed by the for as it it was number:linefromfile then using tokens=1*delims=: will assign the number to %%a and linefromfile to %%b.
The block processing simply matches the line that was read from the file to the start/end string defined. block was originally "set" to empty, so it is undefined. When the startstring is matched, block is assigned a value. I used Y, but any value will do.
When the for reads the next string from the file, it finds that block is now defined, so it echoes %%b to the file c:\temp\18749r. This continues until endstring is found, when block is "set" to empty again; hence it is undefined and there is no more echoing to c:\temp\18749r.
The inner for of the second block is similar, but reversed. It reproduces each line from the file selected in the outer loop until the startstring is found, then types the contents of the tempfile and waits for the endstring when it clears blockand hence turns onthe echoing again.
The outer loop simply reads dir /b /a-d for the source directory - a directory list of simply the filenames. The "delims=" assigns the entire line to %%t and hence the outer loop is for...%%t...do (innerblock)>"%destdir%\%%~nt.html" which redirects the data echoed by the inner loop to the file with the name part of %%t (%%~nt) with the destination directory specified and the extension .html
Finally, the tempfiles are deleted.
You can't do that with a batch file. Use visual basic or pearl for something like that. Closest a batch file could do to something like that is have the output from one file be used as the input for another file but that's as specific wait can get.
Lottopix.has > prevwinum.cgi
Currently I just success to read from one .txt file only. Here is my code.
for /f "delims=" %%a in (C:\test\Scriptlogs\COB\log_DP_20140331_1509_CW52.txt) do SET e=%%a
My question is I got one folder contain of many .txt file, however above code only read specified .txt file. Any changes that I need to made so that it can loop through all the .txt files?
Example of .txt file name.
log_DP_20140331_1324_CW52.txt
log_DP_20345692_1234_CW51.txt
log_DP_21234324_2134_CW50.txt
FYI, folder contains more than 3 .txt files.
Any guidance ,answer or similar post to share?
Thanks for viewing, comments and answers.
You have how to iterate over a file content. The only you need is to iterate over the file list and read the content of each file. So
for %%x in (C:\test\Scriptlogs\COB\log_DP_*.txt
) do for /f "usebackq delims=" %%a in ("%%~fx") do .....
Where %%x holds a reference to each file in the set and %%~fx is the full path to the file. To avoid problems with possible spaces in filenames, i have quoted it "%%~fx", so, to indicate to for /f comand that it is not a string but a file, usebackq has been included in the for options string
What you need is the forfiles function. You can put the code you have into the body of the forfiles loop. Check out ss64.com it is a great resource for a number of scripting languages.
I'm trying to extract specific text from a text file using batch code. The file from which I need to extract data will have multiple lines of text and the number of lines will vary, which means the position of the indicators will change as well. Here's a sample of the text file:
File 1:
<File>
<General>
<Primary_1>1.2.3.4.5</Primary_1>
<Secondary_2>9.8.7.6.5</Secondary_2>
</General>
<Main_List>
<Details="Title" One="C:\Folder1\Folder2\Folder3\Folder4\Folder5" Two="I" Three="4"/>
</Main_List>
</File>
I've gone through some manipulation already and extracted the lines that contain the data I need from the text file and saved it to two separate text files so I end up with this:
File 2:
<Primary_1>1.2.3.4.5</Primary_1>
File 3:
<Details="Title" One="C:\folder1\folder2\folder3\folder4" Two="A" Three="5"/>
So, from the two files above (file 2 & file 3), I need to be able to extract two values. The first being between the |Primary_1| and |/Primary_1| indicators...in this case I would need to pull the "1.2.3.4.5" value. The second being the value after the |Details="| and before the |" One=| indicators...in this case I would need to pull the "Title" value.
I searched around and couldn't find anything that quite fit the bill. The closest I found was the "...on the same line..." code (Extract part of a text file using batch dos), but I kept getting errors. Any help would be greatly appreciated. Thank you.
Try this when both lines are in file.txt
It works for the txt as given, if TABs aren't in the file.
#echo off
for /f "tokens=2 delims=<> " %%a in ('find "<Primary_1>" ^< "file.txt" ') do echo "%%a"
for /f "tokens=2 delims==" %%a in ('find "<Details =" ^< "file.txt" ') do SET "xtitle=%%a"
SET ntitle=%xtitle:~1%
SET xtitle="%xtitle%"
ECHO +%ntitle%+ or +%xtitle%+ - your choice...
There is a more robust method using a helper batch file if your wanted text contains spaces.
(little tickle by Magoo - allows spaces in the quoted "Title" string - but I don't know whether the requirement is for quoted or unquoted variable contents...so you get both. (no extra charge)
I want to use a .bat file to read lines from .txt file then use them as commands. The text file is updated everyday so the size or the number of lines is unknown. I seem to be stuck on it. I'm completely new to batch scripting. So any kind of help is appreciated.
You can iterate over the lines of a file with
for /f "delims=" %%L in (foo.txt) do ...
To use whatever is in those lines as commands, just execute it:
for /f "delims=" %%L in (foo.txt) do %%L
A simpler way might be to just rename the file to a batch file and run it:
ren foo.txt foo.cmd
call foo.cmd
The simplest way is this:
cmd < foo.txt
This method also allows you to include lines of input data for commands in the lines following the commands. For example, try previous line with this foo.txt file:
echo Read the value given in next line
set /P var=
This is the value
echo The value read is: %var%
This feature may ve very useful in certain cases.