help needed with interpreting strings and acting in response

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: help needed with interpreting strings and acting in resp

#31 Post by paultomasi » 29 Nov 2011 05:55

Rileyh

Apart from processing the one line in a file (the line containing: 'printline /a Hello world.'), do you actually need to process all the other lines too?

If you don't then you could use FINDSTR to 'pick out' 'printline' as in the following code:

NOTE: Just copy & paste the code into notepad, save it as a batch file and run it as it is to see the demonstration.

Code: Select all

@echo off
setlocal enabledelayedexpansion

rem Set up testfile
(
  echo line 1
  echo printline /a Hello world.
  echo line 3
  echo This printline will not be detected.
  echo line 5
  echo printline /a That's all folks.
  echo line 7
)>testfile.txt

rem Point to the testfile
set $file=testfile.txt

rem Display contents of testfile
echo.
echo Contains of testfile:
echo ======================================
more<%$file%
echo ======================================

set /p .=Press any key to see output...<nul
pause>nul
echo.
echo.

rem THIS IS THE ACTUAL BIT YOU NEED

rem Find occurances of 'printline' at start of text lines & tokenise
for /f "tokens=1-2*" %%a in ('findstr /b /i "printline" "!$file!"') do (
  set command=%%a
  set switch=%%b
  set value=%%c

  rem !command! will be set to 'printline'
  rem !switch! will be set to '/a'
  rem !value! will be set to 'Hello world.'

  rem Now do something with !command!, !switch! and !value!
  echo !command!
  echo !switch!
  echo !value!
  echo.
)

rem Delete the testfile
del "%$file" 2>nul

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

Re: help needed with interpreting strings and acting in resp

#32 Post by paultomasi » 29 Nov 2011 06:29

Ed Dyreen

Good program design is not just about getting code to work right - it also includes good layout.

What I mean by that is:

- consistency (comes with good practice and experience)
- properly formatted layout using indentations etc.
- good balance of white space
- remarks
- meaningful variable names
- logical program flow
- EASY to read and to determine what the code is doing

Now take a look at my previous reply to the author and take heed. He is an absolute beginner and therefore, I have no reason to dazzle him with my expertise.

My intention is to bring a close to this question so that my free time is nolonger taken up by this asker so that I can move onto another question, and so that he leaves with a sense of satisfaction of having his question ansered and perhaps having learned something in the process.

So, please take a look at my previous code.

Now, if you really want a challenge (because you're so skilled), then using nothing other than just standard DOS commands, write a batch file which enables a user to move his mouse to select one of three options "One", "Two" and "Three" from a text-based menu displayed on the screen and confirm his selection.

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

Re: help needed with interpreting strings and acting in resp

#33 Post by Rileyh » 29 Nov 2011 19:22

@paultomasi
I intend that this code will be used multiple times. Such as:

Code: Select all

(piece of code)

(same piece of code but again)

(same piece of code again)
 
(etc)


For each occurrence of this code the %match% variable will contain a different value:

Code: Select all

set "$match=printline"
(follows the rest of the code)

set "$match=pause"
(etc)

set "$match=somethingelse"


@orange_batch
I don't blame you for not reading an entire 30+ post thread.
Is your code the entire solution, because seemingly Ed must have amazingly overcomplicated the solution for yours to be so short.
Input.txt will may contain a string surrounded by quotes, but does that mean that I could just make the !$$! variable be !~$$! or something to suppress the quotes?
Lastly, with the %%b variable containing the rest of the string, could I repeat the same process splitting if it contains spaces?

Thank you guys,
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

#34 Post by Ed Dyreen » 29 Nov 2011 20:26

'
Rileyh wrote: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

echo. question 1:
for %%? in ( "This works" ) do echo.?=%%~?=%%?_

echo. question 2:
set "$var=This works" &for %%? in ( %$var% ) do echo.?=%%?_

pause
exit /b 0
There ya go, simple and unstable, you should be able to understand this not ?

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

Re: help needed with interpreting strings and acting in resp

#35 Post by Rileyh » 29 Nov 2011 20:32

I understand what you mean. And since I am so poor at batch scripting I did not understand the point of using all the complications: I did not know that they were necessary.
NOTE- I did not ask you to make your code simpler for user purposes.

Regards,
Rileyh

@paultomasi
When stating about Ed Dyreen, please refrain from being conceited. BTW- Ed Dyreen- expert Paultomasi- not expert
So stating about the way that Ed codes in a conceited manner from your position is not reasonable- you are less experienced than Ed.

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

Re: help needed with interpreting strings and acting in resp

#36 Post by paultomasi » 30 Nov 2011 21:01

(Some comments were edited out by paultomasi)

Rileyh
@paultomasi
When stating about Ed Dyreen, please refrain from being conceited. BTW- Ed Dyreen- expert Paultomasi- not expert
So stating about the way that Ed codes in a conceited manner from your position is not reasonable- you are less experienced than Ed.

Ed Dyreen- expert Paultomasi- not expert
I have no idea how this forum assigns 'Expert' status although I have requested advice on this.

As for me being an expert or not, I think if you navigate yourself over to http://www.experts-exchange.com (where I spend a lot of my time) you'll be more than satisfied with my credentials as one of the Worlds leading Advanced Batch File programmers with some 22 years of programming experience behind me. I used to go by the name 't0t0' and at one point last year I was ranked No.2 in the World. At that time, I decided to reap the glory in my real name (paultomasi) so now I'm currently ranked 12th (due to not being able to port my previous ranking across).

I have only just (6 days ago) started to contribute to this forum so don't be under any illusion about Ed Dyreen being a more capable batch file programmer than myself. You may call that conceit if you want to.

Before I leave you in Ed Dyreen's 'capable' hands, please take a moment to reflect on the following:

You said:
I did not understand the point of using all the complications: I did not know that they were necessary.

Ed Dyreen's latest offering is:
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: Select all

@echo off

echo. question 1:
for %%? in ( "This works" ) do echo.?=%%~?=%%?_

echo. question 2:
set "$var=This works" &for %%? in ( %$var% ) do echo.?=%%?_

pause
exit /b 0
There ya go, simple and unstable, you should be able to understand this not ?

Let's remind ourselves of some key words here: understand, necessary, complications and simple.

Now let's look at Ed Dyreen's code line-by-line...

Code: Select all

@echo off
This is standard for all batch files. Nothing wrong here!

Code: Select all

echo. question 1:
Absolutely NO NEED for 'echo.' when just 'echo' would have sufficed!

Infact, Ed Dyreen makes a mistake here by inserting a space between 'echo.' and 'question 1:'. This is because unlike just 'echo question 1:' (without the fullstop or period character), DOS actually displays a leading space character in front of 'question 1:'.

Code: Select all

for %%? in ( "This works" ) do echo.?=%%~?=%%?_
Again, absolutely NO NEED for 'echo.' and this time, NO NEED for the trailing underscore ('_') character either.

The ouptut from this line of code is:

?=This works="This works"_

which means absolutely nothing by itself and which should have infact read:

%%?="This works"
%%~?=This works

Code: Select all

echo. question 2:
Again, absolutely NO NEED for 'echo.' when just 'echo' would have sufficed. And as before, DOS inserts a space character in front of 'question 2:'.

Code: Select all

set "$var=This works" &for %%? in ( %$var% ) do echo.?=%%?_
Firstly, there is absolutely NO NEED to enclose both Lvalue and Rvalue of the SET statement in double-quotes.

Also, there is absolutely NO REASON why 'var' should be preceded with a dollar ('$') sign. I cannot ever recall seeing Microsoft using this convention in any of their batch file examples. A simple alphanumeric variablename would have sufficed such as just 'var'.

Furthermore, There is absolutely NO NEED to use the ampersand ('&') character and place two statements onto a single line. The following is much clearer:

set var=This works
for %%? in (%var%) do echo ?=%%?

There is absolutely NO NEED for 'echo.' nor the trailing underscore ('_') character again.

Code: Select all

pause
Not necessary.

Code: Select all

exit /b 0
Not necessary.

So. Simple? - NO!

Now let's look at the output from Ed Dyreen's code:

C:\
 question 1:
?=This works="This works"_
 question 2:
?=This_
?=works_
Press any key to continue . . .
C:\

Not the most aspiring (in my opinion) however, I'll leave you to be the judge of that.
Last edited by paultomasi on 01 Dec 2011 15:08, edited 2 times in total.

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

#37 Post by orange_batch » 01 Dec 2011 00:45

Rileyh wrote:@orange_batch
I don't blame you for not reading an entire 30+ post thread.
Is your code the entire solution, because seemingly Ed must have amazingly overcomplicated the solution for yours to be so short.
Input.txt will may contain a string surrounded by quotes, but does that mean that I could just make the !$$! variable be !~$$! or something to suppress the quotes?
Lastly, with the %%b variable containing the rest of the string, could I repeat the same process splitting if it contains spaces?

Thank you guys,
Rileyh

The code I wrote only does two things:
1. Checks the first "word" of each line (space/tab-delimited) to see if it is the same as %match%.
2. If it is the same as %match%, it will execute whatever code you write in there, such as saving the rest of the line (%%b) to a variable (myvar).

@paultomasi 22 years huh, out of curiosity have you written any particularly large or unique scripts? I'm always looking for new ideas.
Last edited by orange_batch on 12 Dec 2011 14:19, edited 2 times in total.

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

Re: help needed with interpreting strings and acting in resp

#38 Post by Rileyh » 01 Dec 2011 06:16

@paultomasi
Due to your lack of experience on this forum (DO NOT get the idea that I am referring to your batch skill!) and that you have only been around for a few weeks here, I assumed that Ed Dyreen's code was the "obvious" solution.
But when you put it the way that you did I have noticed a few things that he does frequently that is unreasonable
%%?_
(I am referring to the underscore)

But from my point of view you seemed conceit and uptight, so you can see why my choice of words was reasonable from this point of view.

Regards,
Rileyh

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

Re: help needed with interpreting strings and acting in resp

#39 Post by paultomasi » 01 Dec 2011 14:23

Rileyh

Apologies. I did intend to remove my previous reply when jeb pointed out your age to me - something I didn't even think to check myself.

Given the circumstances, I apologies for the manner in which I conducted myself - it was inappropriate.

I would prefer not to dwell on this matter any further.

I am only too happy to assist if and when I am able to.

Once again. Apologies :)

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

Re: help needed with interpreting strings and acting in resp

#40 Post by paultomasi » 01 Dec 2011 15:04

orange_batch

Yes. I do a lot of stuff often requiring arrays, multi-dimensional arrays, recursion, data crunching algorithms and code generation.

Some of my projects include text-based games, compilers, parsers, filters, editors and library functions.

My main interests are writing DOS extensions in Assembly code (mostly .COM files), batch files with embedded Assembly code functions, TSRs, multi-tasking batch files in the simulation of game play and multi-tasking batch files over a distributed system.

My other interests are program design, validation, testing (mostly designing test data and test environments), optimisation and user interface.

I've probably left a lot of stuff out but you get the general idea I'm sure.

If you don't mind me asking, what are your strengths?

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

Re: help needed with interpreting strings and acting in resp

#41 Post by paultomasi » 01 Dec 2011 15:22

Ed Dyreen

I hope we can learn to see eye-to-eye in future...

The reasons behind your style and their purpose have not gone unnoticed by me but I feel there is a time and place for complexity - just as there is a need for simplicity in some situations.

:)

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

#42 Post by orange_batch » 01 Dec 2011 18:10

@paultomasi

Assembly scares me. :o

Much of the same - arrays, recursion, data algorithms, encryption algorithms, logical shifts/bitwise, complex file/folder operations, unicode, optimizations, various tricks, VBScript, WMIC, UI as far as command prompt goes.

I've been working on a secret project for a year 8) it's built in modules and has a custom library. Something cool I wrote a while ago allows batch to rediscover any unicode in paths read from a text file, since it seems impossible to pass full-on unicode to command prompt except via arguments when Windows initializes it. Without sarcasm I'd still like to be proven wrong on that.

Mostly I seem to be good at finding bugs and working them out.

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

Re: help needed with interpreting strings and acting in resp

#43 Post by Rileyh » 01 Dec 2011 18:45

Since you all are talking about what your personal projects are I might as well share my (limited, and probably absolutely novice :)) project (the entire purpose of this thread, I cannot do it!). I am attempting to make a interpreter for a simplified, command-based language of my own. And the path that I have tried to use to accomplish it is the code that I obtained earlier about finding a command (hence the "printline" command that I made, it is going to be used as an "echo"-like command.) So maybe this sheds some light on the purpose of this thread.

Regards, (and apologies about previous words said :) )
Rileyh (who can I point out is 13!)

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

#44 Post by orange_batch » 01 Dec 2011 20:48

Many people have done projects like that. It's possible but limited, batch code cannot fully be reinterpreted/parsed bug-free.

Also try doskey /?

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

Re: help needed with interpreting strings and acting in resp

#45 Post by Rileyh » 01 Dec 2011 23:32

@orangebatch,
Could you provide examples of said "projects like that" please? I would love to view the sourcecode.
And how does Doskey work?

Regards,
Rileyh

Post Reply