help needed with interpreting strings and acting in response

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

help needed with interpreting strings and acting in response

#1 Post by Rileyh » 20 Nov 2011 03:27

I am stumped at this how to do this. I have literally tried for about 5 weeks, every day to get this to work and it hasn't. So don't say that i am pestering the forum.
Now, to business:

I need a batch file that finds a text string from a text file (both are internally specified, not user specified) and does something if it is found. If it is not found, the batch file jumps to the next point in the batch file. But it must also set the text next to it on the line as a variable (a standard %(something)% variable).

Could you please post how to do this? I know it sounds confusing, but could you please try and understand it, I really need help!

Thanks in advance,
Rileyh

EDIT- In response to your posts I have modified the title.
EDIT2- In response to your posts I have modified the content.
Last edited by Rileyh on 21 Nov 2011 00:24, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: PLEASE HELP ME!

#2 Post by dbenham » 20 Nov 2011 19:25

You need to provide more specific info, such as:

what are you searching? - a file? text in a variable? directory file names?

what type of search(es) are you trying to do? - regular expression? simple string match?

what have you tried that failed?, hopefully distilled down to the essentials of the problem without extraneous details

Dave Benham

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: PLEASE HELP ME!

#3 Post by nitt » 20 Nov 2011 20:07

dbenham wrote:You need to provide more specific info, such as:

what are you searching? - a file? text in a variable? directory file names?

what type of search(es) are you trying to do? - regular expression? simple string match?

what have you tried that failed?, hopefully distilled down to the essentials of the problem without extraneous details

Dave Benham


Yes, and please refrain from using such despicable titles.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help needed with interpreting strings and acting in resp

#4 Post by aGerman » 21 Nov 2011 15:05

Rileyh wrote:I need a batch file that finds a text string from a text file (both are internally specified, not user specified) and does something if it is found. If it is not found, the batch file jumps to the next point in the batch file. But it must also set the text next to it on the line as a variable (a standard %(something)% variable).


OK, I tried to understand but I didn't. An example could be helpful.
Imagin you have a file called "test.txt" with the following content:

Code: Select all

AAA WWW
BBB XXX
CCC YYY
DDD ZZZ

Say you are looking for string BBB. What will happen if it is found and what if it's not found? Try to explain with the given example or write your own that would suit you according to your requirement.

Regards
aGerman

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#5 Post by Rileyh » 22 Nov 2011 21:57

In the text file:

Code: Select all

AAA WWW
BBB XXX
CCC YYY
DDD ZZZ


So pretend I am looking for the string BBB. I want my code to look for that string and check if it is there. And it is, so it will be set to echo whatever comes after it on the line, in this case it will echo xxx. And it also makes the variable %a% be set to xxx (the next thing on the line).
So in another scenario there is this in the txt file:

Code: Select all

printline xxx

My code will find "printline" and set %(any variable name that i come up with)% to the next thing on the line. And when it finds "printline", it is set to do "echo" in its place. See what I mean?

Sorry about the poor explanation that I gave before.

Regards,
Rileyh

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: help needed with interpreting strings and acting in resp

#6 Post by Ed Dyreen » 22 Nov 2011 22:56

'
Specific question, specific answer.

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$file=input.TXT"
set "$match=BBB"

for /f "usebackq tokens=*" %%? in (

   "!$file!"

) do (
   set "$$=%%~?"
   set "$$=!$$:%$match%=!"
   if /i ["!$$!"] neq ["%%~?"] (
      echo.$$=!$$!_
   )
)
untested !

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#7 Post by Rileyh » 23 Nov 2011 20:25

How can I implement this code in my program?
It does not have the next characters on the line as a standard %(something)% variable.

Could you show me how to do that?

Regards,
Rileyh

PS- I am only asking this question because the syntax is unlike anything I have seen before and therefore I cannot modify it safely.
Sorry if this inconveniences you.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: help needed with interpreting strings and acting in resp

#8 Post by Ed Dyreen » 23 Nov 2011 21:00

'
Rileyh wrote:It does not have the next characters on the line as a standard %(something)%
Not sure I understand your question, you can access variable $$ as !$$!, The %$$% isn't like that in for loops !

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#9 Post by Rileyh » 23 Nov 2011 22:14

Could you also show me where the "function" section is? (the part where I define what to do when it finds the "BBB" string?

Sorry for continued pestering, Ed, but I really need it,

Regards,
Rileyh

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: help needed with interpreting strings and acting in resp

#10 Post by Ed Dyreen » 23 Nov 2011 22:21

'

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$file=input.TXT"
set "$match=BBB"

for /f "usebackq tokens=*" %%? in (
   "!$file!"
) do    set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
   ::
   echo. '!$match!' appears in line:
   echo. '%%~?'
   echo. $$=!$$!_
   echo. the part where I define what to do when it finds the "BBB" string
)

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#11 Post by Rileyh » 23 Nov 2011 22:38

Code: Select all

   echo. the part where I define what to do when it finds the "BBB" string
)


Does that mean that that is the part where I write what happens, or does it mean that that is the part where the defining ends?
In other words, is that the point at which I can write code to define what happens, or is that where the defining ends and before that is where I define what happens?

By the way, sorry about asking about the %(something)% bit. I don't know why I didn't see the !$$! variable :wink:

Thanks again,
Rileyh

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: help needed with interpreting strings and acting in resp

#12 Post by Ed Dyreen » 23 Nov 2011 22:44

'
that is the point at which you can write code to define what happens.

What actually is it you want to do when you find a match :?:

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#13 Post by Rileyh » 23 Nov 2011 22:50

This will be used to make a batch compiler. The string "BBB" is just a pretend string. Really it will be a batch command. The user will type code into an environment and then save it as a batch file. But they have the option to press F5 to run it and that will run a batch file (which contains the code that I need, that is what I am asking how to write) that finds a command and if it works do what that command does. Hence the section on "what to do when found", I need to use that section to define what to do when it finds "echo" for example.

Regards,
Rileyh

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#14 Post by Rileyh » 23 Nov 2011 23:06

I have tested it and Ed Dyreen, you are a genius!!!!!!!!!!
How the hell do you make this stuff?
You have no idea how grateful I am!

But one last question: I have inserted this code:

Code: Select all

set "$file=test.txt"
set "$match=pause >nul "

for /f "usebackq tokens=*" %%# in (
   "!$file!"
) do    set "$$=%%~#" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~#"] (
   ::
 pause >nul
 
)


But when I run this, pauses, even when there is no "pause >nul" in the file "test.txt". H
How do I get it to pause only if the string is found, and not just do it anyway?

But thank you so much again,
Rileyh

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help needed with interpreting strings and acting in resp

#15 Post by Rileyh » 25 Nov 2011 00:43

unfortunately it didn't work, Ed.
Could you try again?

Regards,
Rileyh

Post Reply