help needed with interpreting strings and acting in response

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
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

#16 Post by Ed Dyreen » 25 Nov 2011 01:13

'
Didn't notice, you changed the code, not sure if it'll work, but you need to escape symbols with direct expansion !

Code: Select all

set "$match=pause ^>nul "

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#17 Post by paultomasi » 25 Nov 2011 23:18

This is a simple problem and easily solvable as shown below:

Code: Select all

for /f "tokens=1,*" %%a in ('findstr /b /i "printline" "file.txt"') do (
   echo echo %%~b
)

or you can process each line while testing for "printline" like this:

Code: Select all

for /f "tokens=1,* usebackq" %%a in ("file.txt") do (
   if /i "%%~a"=="printline" echo echo %%b
)

Change '%%b' to '%%~b' if you don't want the quotes (if there are any)

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#18 Post by paultomasi » 26 Nov 2011 00:01

Ed Dyreen

I've looked at your code and in my years of experience I can state, if it's your intention to help people then why attempt to make your code obscure and unconventional?

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
)


The readable layout looks something like this:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set file=input.TXT
set match=BBB

for /f "tokens=* usebackq" %%a in ("%file%") do (
   set $$=%%~a
   set $$=!$$:%$match%=!

   if /i "!$$!" neq "%%~a" (
      echo '%match%' appears in line:
      echo '%%~a'
      echo $$=!$$!
      echo the part where I define what to do when it finds the "%match%" string
   )
)

Even though this is still far removed from how it should be done.

Furthermore, the proper use of FOR is with a..z and A..Z. There is nothing clever about using '?' or some other non-alphabetic character. Your code serves only to confuse readers.

See my solution for how I propose to solve this problem.

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

#19 Post by Ed Dyreen » 26 Nov 2011 03:27

'
paultomasi wrote:Furthermore, the proper use of FOR is with a..z and A..Z. There is nothing clever about using '?' or some other non-alphabetic character. Your code serves only to confuse readers.
I cannot agree, the use of ?,! or $ was chosen intentionally. Tokens a-Z should be kept free for real delimited input and not for delimit-less input. This is normally not a problem for simple scripting. But you are probably right, I am tooComplex.

However I do not see much difference in my code and your correction, apart from the fact you replaced & with linefeed and inserted some tabs. Making it longer and use more memory.

I am anticipating your next suggestion, make variables with distinct and defined names.
You are right, I would normally, but not in DOS. I suffer a macro cold.
But they do have meaning, $ for example is used to indicate a variable in many languages.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: help needed with interpreting strings and acting in resp

#20 Post by jeb » 26 Nov 2011 14:42

paultomasi wrote:Furthermore, the proper use of FOR is with a..z and A..Z. There is nothing clever about using '?' or some other non-alphabetic character.

There is something clever about NOT using a..z nor A..Z :!:

Code: Select all

@echo off
set append=abcd
for /F "delims=" %%a in ("hello") do (
  echo %%~a%append%
)

Yes, as expected you got bcd as result :!:

That's the effect of the greedy flag parsing (dbenham shows this somewhere).
Therefore it could be useful not to use a-z.

jeb

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#21 Post by paultomasi » 26 Nov 2011 15:54

jeb

Code: Select all

@echo off
set append=abcd
for /F "delims=" %%a in ("hello") do (
  echo %%~a%append%
)

There's absolutely no need to use a 'FOR /F'-loop with "delims=" to make your point. The following would have sufficed:

Code: Select all

@echo off
set append=abcd
for %%a in ("hello") do (
  echo %%~a%append%
)

Simple and clean! It seems some 'experts' cannot refrain from over-complcating a simple problem.

The point you make is an interesting one, and one which I have come across myself on many occasions.

One possible solution is to use %%A instead of %%a but that may not be foolproof due to uncertainty regarding case. Another is to use %%~a!append! instead of %%~a%append% however, this requires delayed expansion to be enabled.

There are other ways around it including assigning %%~a to a variable but that defeats the ability to use %%~a!append!.

I would only consider using something like %%$ if there was an absolute need to - and that need has not yet arisen.

Thank you for your response.

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

Re: help needed with interpreting strings and acting in resp

#22 Post by Rileyh » 26 Nov 2011 22:04

I am no expert, hence my seemingly pitifully easy post topic but please excuse my lack of knowledge.

QUESTION:
What is the difference between "%%~a" and "%%a"?

Could someone answer that?

QUESTION 2:
How can I delimit the !$$! variable by its spaces (if it contains them) in this code:

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
)

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#23 Post by paultomasi » 27 Nov 2011 03:44

QUESTION 1:
What is the difference between "%%~a" and "%%a"?
If %%a includes double-quotes as part of it's value then %%~a suppresses them. This is of particular concern when we are adding double-quotes around %%a. If it's value already includes them then we end up with two lots of double-quotes at both ends when %%a is expanded by DOS so, instead of ending up with something like: "text", we end up with: ""text"".

"%%~a" simply translates to: If the first and last character of the value in %%a is a double-quote then suppress them and then include double-quotes around the remaining characters (think about it!).

So, basically, we use %%~a when: (1) we want to suppress double-quotes or: (2) when we are uncertain whether double-quotes are already included - this way, we can suppress them and thereby, provided that certainty.

Because %%~a only 'suppresses' double-quotes they aren't actually removed - they're still part of the value assigned to %%a which is why we continue to use %%~a and which is one of the reasons you might see lot's of squiggly 'tilde' characters ('~') dotted throughout the code.

For example, we simply can't do the following:

set %%a=%%~a

and then procede to use just %%a.

Does that make it clearer?
Last edited by paultomasi on 27 Nov 2011 04:08, edited 1 time in total.

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#24 Post by paultomasi » 27 Nov 2011 04:07

QUESTION 2:
How can I delimit the !$$! variable by its spaces (if it contains them) in this code:

For example:

Code: Select all

ECHO !$$:~ =!
will display the value of $$ with all of it's spaces suppressed.

If you want to actually remove the spaces then you can do the following:

Code: Select all

SET $$=!$$:~ =!

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

Re: help needed with interpreting strings and acting in resp

#25 Post by Rileyh » 27 Nov 2011 19:06

No I wanted to make each section that is separated by a space a different token, so that I can access each part as a separate variable.

Regards,
Rileyh

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#26 Post by paultomasi » 28 Nov 2011 07:23

when you say you want to access each part of the string separately, you can do the following (although it's not the way I would normally do it):

Code: Select all

for /f "tokens=1-26" %%a in ("%$$%") do (
  echo %%a
  echo %%b
  echo %%c
  :etc...
)

Notice I have no idea how many tokens there are in the string $$ so I assume %%a through %%z when I state 'tokens=1-26'.

If you know HOW MANY tokanes there will be then this can be simplified.

If you know the LENGTH of each token AND their POSITION in $$ then the task would be even simpler.

Let me know if you're satisfied with the above or whether you need a more specific approac in which case give me an example value for $$.

Incidentally, I've used "%$$%" however, "!$$!" can be used if delayed expansion is enabled.

Also, I have assumed "%$$%" does not already include double-quotes around the value of $$ becuase you can't do "%$$~%" as with %%~a or %~1 etc...

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

Re: help needed with interpreting strings and acting in resp

#27 Post by Rileyh » 28 Nov 2011 22:31

Here is an example scenario for the use of this code and the original question.
IN A TEXT FILE:

Code: Select all

printline /a Hello world.

I wish to take the previous code:

Code: Select all

@echo off &setlocal enableDelayedExpansion

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

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


Now, here is the "tricky" bit:
I know that the variable !$$! contains the value "/a Hello World". I need to make it be (an example) "%%a(could equal)=/a" and "%%b(could equal)=Hello World"
And then I could make the previous code do this:

Code: Select all

@echo off &setlocal enableDelayedExpansion

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

for /f "usebackq tokens=*" %%? in (
   "!$file!"
) do    set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
   ::
   echo %%b
)

So that it skips out the /a part which could be used in another occasion:

Code: Select all

@echo off &setlocal enableDelayedExpansion

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

for /f "usebackq tokens=*" %%? in (
   "!$file!"
) do    set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
   ::
   REM The /a part could be made to do something other than if there is a normal occurrence of "printline":
   echo "Printline" exists in the file "input.txt"
   REM The above is the alternate action taken when there is a value in the variable "%%a"
)


Do you see what I mean?


Thank you for helping,
Rileyh

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: help needed with interpreting strings and acting in resp

#28 Post by orange_batch » 29 Nov 2011 04:15

paultomasi wrote:Ed Dyreen

I've looked at your code and in my years of experience I can state, if it's your intention to help people then why attempt to make your code obscure and unconventional?

I suggested this before, it confuses, misleads and alienates people about batch. Please try to respond more in layman's terms depending on the user's skill level, most questions seek to learn alongside a solution.

As for jeb's code, I don't think he intended to overcomplicate by using for /f "delims=" more than a force of habit in his example.

As for that example, that's true but as we know, if one prefixes all their variables with a specific character as they probably should in larger scripts, no conflict should occur.

Rileyh sorry I won't read through the entire thread, but here is a more conventional version of Ed's code including saving the content to the right of the match... (Space and tab are for /f's default delimiters, so no need to set delims.)

Code: Select all

@echo off&setlocal enabledelayedexpansion

set "file=input.txt"
set "match=printline "

for /f "usebackq tokens=1*" %%a in ("%file%") do (
set "check=%%~a"
set "check=!check:%match%=!"
if /i "!check!" NEQ "%%~a" (
echo:Found %match%. Put code here.
set "myvar=%%b"
))

tokens=1* means token 1 (%%a) will match the first delimited piece of text, and token * (%%b) will contain all remaining text on the line (including the delimiters).

Also, there's no need for the ~ character in that script unless any lines in input.txt contain surrounding quotation marks.

Lastly, please come sooner than 35 days to ask for help, that's what we're here for.
Last edited by orange_batch on 14 Dec 2011 01:51, edited 2 times in total.

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

Re: help needed with interpreting strings and acting in resp

#29 Post by paultomasi » 29 Nov 2011 05:03

orange_batch

I agree with you on the force-of-habit thing...

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: help needed with interpreting strings and acting in resp

#30 Post by orange_batch » 29 Nov 2011 05:18

Yup, jeb is a good guy around here. He expands the knowledge of command prompt by extensively experimenting with its behaviour.

Post Reply