Unable to check if a string contains a specific string in a for loop of a text file - batch-file

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

Related

Find last line of text in .jpg file

As weird as the question sounds, I want to find the last line of text in a .jpg file using batch.
I am working on a .txt file encryption via batch. I want the program to encrypt the contents of a .txt and append them to a set .jpg.
I then want the program to retrieve the last line of the jpg file and decrypt it.
I have been successful with the first part but I can't seem to be able to retrieve the last line of the jpg file as text. Any suggestions will be much appreciated.
for /f "delims=" %%x in (version.txt) do set line=%%x
This will set the last line of any file as %line%

batch command that will loop through files ending in “.txt” and display file contents

I'm pretty sure I am just missing something small, but I am trying to figure out how to loop through the contents of a directory, identify those with a .txt extension, and display the contents of the .txt files to the display.
I searched for an answer to this but couldn't find it specifically. I found this one that shows how to find all the .txt and use echo to display the file name.
I also found this one that shows how to use Type to display the file contents, and this one that combines them sort of. But I keep getting errors when I try different variations on using type in a for loop.
I've tried:
for %%i in (*.txt) do type %%i
and
for %%i in (*.txt) do (
type %%i
echo.
)
But both of those are giving errors. I'm sure it's something simple I am missing.
Update:
The problem was spaces in the filenames. Adding quotes around the %%i following Type fixed it.
No need for any FOR loop. type *.txt will do the trick. Each file name will be printed to stderr, followed by the content on stdout. By default, both stderr and stdout appear on the console. There will be one or two line gaps between each file name and file content.
If you don't want to see the file names, then type *.txt 2>nul. But then there is no gap between each file.

Read a file in batch

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

Extract specific text from text file using batch

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)

batch to find a string of numbers from tsv or text file and put it into a variable

I have a .tsv file that I would like to use a batch file to search for a string of numbers after some text "sampletext 1345678" at the 3rd line of the tsv file. I would like to put the numbers into a variable then print that variable in a temporary text file. I've tried searching for something like this but nothing matches what I would like to do.
set "var="
for /f "skip=2tokens=2" %%i in (whatever.tsv) do if not defined var set var=%%i
echo found %var%
should accomplish this (as a batchfile snippet)

Resources