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)
Related
I have a batch script that reads line by line a text file (ProcessedFiles.txt). The text has many lines that represent filenames of a directory. They can be either JSON or HTML files. I would like to check if the string "json" exists in that line. I have already searched and tried a few answers like
if not x%%a:json=%==x%%a% echo It contains JSON
or even extracting the last 4 characters from the %%a
SET var=%%a:~-4%
but I must be doing something really wrong here because I cannot get it to work.
I apologize for my very limited knowledge on batch script.
Basically I want to know if each line of the text file I am reading contains the string "json". This is my for loop code i am trying to correct
for /F "tokens=*" %%a in (%jsonDir%ProcessedFiles.txt) do (
echo Processing file: %%a
SET var=%%a:~-4%
)
The echo Processing file: %%a displays the line correctly and this is what I am aiming to search if it contains the string "json".
For example, if text file ProcessedFiles.txt contains the lines
ABCD.json
BCDF.json
ABDR.html
CVBF.json etc.
when the loop reads the line ABCD.json or BCDF.json or CVBF.json then I need to know that it has encountered a JSON file, but for line ABDR.html it is not a JSON file.
Thank you in advance
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 would be really grateful for anyone that could save me some time on this.
I have a Windows batch file that I am using to match two different file types with different extensions and write the results out to a file.
It reads through a table of filenames (called matched_filenames.txt) and, for each line in it, tries to locate the filename in two other tables.
I have this code (which works to an extent).
for /f "tokens=*" %%z in (%Ourhome%\matched_filenames.txt) do (
rem -------
rem ------- now (for each line), find the photo name in both the list of raw and list of jpg files
rem -------
findstr /C:"%%z" raw_photos_directory.txt >>located_matches.txt
findstr /C:"%%z" jpg_photos_directory.txt >>located_matches.txt
)
But it writes separate lines out to the located_matches.txt file.
I would really like to assign the results of the findstr's to variables so that I can then concatenate them and then write the concatenated line as a single line to the file.
Can anyone recommend a more elegant way of doing this?
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
Good Day All.
I have a bunch of log files that contains a certain folder in a folder path, that is "/input", that is part of a pathname that varies - "/input" being the only constant here...
How do you scan all the log files (*.log), look for all sub-string instances of "/input" and write the whole line, containing this part of the path, that is "/input", to a new text file?
Example of one line in a log file:
2014-01-16 00:33:57 10.0.1.169 ddca 192.168.34.11 21 CWD /DDAA/Input/ 250 0 0
How do I write all the lines, containing this part "/input" to a new text file?
Thanks.
#echo off
setlocal enableextensions
set "source=c:\where\the\log\files\are"
set "target=c:\output\folder\newFile.log"
pushd "%source%"
(for /f "tokens=1,* delims=:" %%a in ('findstr /i /l /c:"/input/ " "*.log"') do (
echo(%%b
)) > "%target%"
popd
Search for the string using findstr. It will return a string containing the file name where the string was found, followed by a colon and the full line in the log file. Then, using for command this line is splitted using the colon as a delimiter and the right part of the line (the original line in log file) is echoed. The output of the for command is then redirected to the final file.
pushd and popd are used to ensure that references to the log files, and the names in the output of the findstr command, does not contain aditional colons that interfere in the splitting part.
findstr /i /c:"/input" *.log >output.txt
The /i switch makes the search case insensitive. The /c: switch is used so that the leading / is not treated as a switch indicator.
Because a wildcard was used, it will prefix each line with the name of the file, followed by a colon, like in the following
filename.log:2014-01-16 00:33:57 10.0.1.169 ddca 192.168.34.11 21 CWD /DDAA/Input/ 250 0 0
Lots of options. You can create a small tool that does this for you, but my guess is somebody somewhere already done this. Did you google for something like this?
If you are up to some manual work, I know Notepad++ has the functionality to search through txt-files in folder(s) matching a (sub)string.
Also relevant:
https://superuser.com/questions/110350/how-to-use-notepad-to-find-files-in-a-directory-that-do-not-contain-string