Find & Output Specific Text with Bach File

August 30, 2011 at 17:20:54
Specs: Windows 7
I have a text file, confsys.txt (an example of which is attached). Amongst all the junk that is in here will be either the word MAIN or TERMINAL. If it is a TERMINAL, after the word TERMINAL it will show the network path of where the data exists.

I have been trying to create a batch file using For, Find, Findstr etc. But having no luck. Don’t really know where to start.

Basically what I am after is a batch file I can run, that will do the following:

1. Does the confsys.txt contain ‘MAIN’?

a. Yes – Output: “This is the MAIN (xxxxxxxxx)” – Where xxxxxxxxx is the name of this computer

b. No – Go to Step 2

2. Does the confsys contain ‘TERMINAL’?

a. Yes – Output: “This is a TERMINAL. The Data is stored on \\xxxxxx\xxxxxx” – Where \\xxxxxx\xxxxxx is the network path that immediately proceeds the word TERMINAL in the text file. Also I would like this network path to be stored as a variable to it can be referenced in future code – Variable Name = MainPath

b. No – Output: “Error”

Please help.

Example of the part of the text file I am looking for:

"..............XPRINT CÈ 2
TERMINAL \\SERVER\data\ FCLPT2Eltro............"

See More: Find & Output Specific Text with Bach File

Report •

September 9, 2011 at 09:08:29
I think I have an understanding of what you are looking for, the code I came up with is a little different than what you were asking. What this does is it pulls the MAIN and TERMINAL and puts it into a text document. Then it pulls the path and sets it into a separate text file to be used later in a different batch. It does not output any errors it just copies them over.

@echo off

findstr MAIN confsys.txt >> new.txt
findstr TERMINAL confsys.txt >> new.txt
findstr \ consys.txt >> variables.txt

note that the findstr TERMINAL will also put the network path in new.txt. The findstr \ puts ONLY the networkpath in the variables.txt. (unless you have other text after the path it will copy that as well). This is a simple code if it does not do what you are looking for I would check into tokens and delims (not my strong suit). I hope this helps you out.

Report •

September 11, 2011 at 20:27:48
Thank you for this. It seems this codes looks for the required text and then put all the contents from the confsys.txt into the new.txt, ideally I'd want it to just contain either MAIN or TERMINAL. Or even just assign MAIN or TERMINAL as a variable which I can then output later.

The findstr \ confsys.txt >> variables.txt doesn't actually put anything in the variables.txt.

The confsys.txt I am using is just a confsys.dbf renamed to confsys.txt and as such contains a lot of strange characters and it is all on one line which is why I think when it finds it it outputs the whole ine.

I can get the file nicely into a .csv instead if it helps and then all I would need to do is ouput a specific cell reference if possible. Is it possible to ouput the contents of a specific cell from CSV using a batch command?

Report •

September 12, 2011 at 14:32:06
Im not sure, I have never tried. One thing I have tried in the past is saving as a delimited text file. As for the findstr \ not working, I have tested it multiple times and it has been working on my end. The only thing I can thing of is that you are using a different OS. If in the end it still ends up all being on one line I would suggest doing a little research in delims and tokens. I do not feel findstr will work out too well for you as it usually will pull the entire thing.

Report •

Related Solutions

Ask Question