|
Answer» Okay, this is driving me batty. Can someone tell me what I'm doing wrong here, because I don't see it.
I'm utterly confused. "SET" is acting very strangely within a for loop. Here's my code. PortList is a file with a few pre-formatted lines of text (I'm assuming it has 4 lines for this example).
Code: [Select]set count=1 for /f "tokens=1" %%i in (PortList) do ( echo %count%^) %%i set /A count=%count%+1 ) echo. set /P input=Please select [1-%count%]:
The output I EXPECT is
Code: [Select]1) [first entry in PortList] 2) [2nd entry] 3) [3rd entry] 4) [4th entry]
Please select [1-5]:
(yes, I know I need to decrement count before that last line, but I'll deal with that later).
What I get instead is
Code: [Select]1) [first entry in PortList] 1) [2nd entry] 1) [3rd entry] 1) [4th entry]
Please select [1-2]:
Notice that it DOES increment count at the end, so I get "1-2" instead of "1-1".
What am I missing? No matter what I've tried, the set /A line is ignored within the for loop until AFTER the final iteration of the loop. Even if I simplify it down to the following:
Code: [Select]set count=1 for %%i in (1 2 3 4) do ( set /A count=%count%+1 echo %count% ) echo %count%
The output is: 1 1 1 1 2
Heck, I've even tried this: Code: [Select]count=1 for %%i in (1 2 3 4) do ( set count=hello echo %count% ) echo %count%
And all I get is: 1 1 1 1 hello
Maddening I tell you!
Oh, this is a batch program on WIN 2K3.This is a well know noob trap. You need to find out about delayed expansion. Google is your friend.
Wow. 5 years of batch programming and I've never run across this.
That did it, thanks.I re-read my answer and it kind of looks like I was calling you a noob, which wasn't very polite, and it wasn't my INTENTION to be rude, so sorry if it sounded that way.
A well know trap for progressing batch programmers. No worries. Programming/scripting is an ancillary part of my job that I do only occasionally, so in many ways I am still a n00b. Especially since I bounce around between Windows batch, linux shell, python, perl, etc. I can design an algorithm in my sleep, but when it comes to specific language syntax and compiling/runtime quirks, I can't keep it all straight.Quote from: gdiddy on January 29, 2009, 12:09:05 PM Especially since I bounce around between Windows batch, linux shell, python, perl, etc. I Googled "delayed expansion" just to check that it brought up relevant stuff and I found a blog by someone called Batcheero, who explains it quite clearly, and he ALSO shows how to make a mixed batch/perl script
QuotePerl In Batch Clothing
As someone who writes a lot of scripts, Perl is my language of choice. But it does not mean I can live without Windows batch. Sometimes though, it is necessary to combine the two by disguising a Perl script as a batch file.
When might this come in handy? Well, to be honest I can't really think of any right now, but this is certainly cool. This trick is not a batch file trick per se. It's really a Perl trick, and you'll see soon enough why that is. But I'd like to put it here just the same because hey, it's got some batch scripting in it.
To write your Perl script as a batch file, all you have to do is add a few lines at the top of your Perl script, rename the script extension to .bat or .cmd, and you're done.
@echo off
perl -x %~dpf0 %*
EXIT /b %errorlevel%
#!perl
use strict;
print "Hey $ARGV[0] I'm in Perl!\n";
exit 0;
This file, being a batch file, is executed by the command processor as any batch file. It reads each line and executes that line.
The first line is straightforward. The second line is really the trick. Doing perl -x tells Perl to read the file you pass as argument, %~dpf0, which is the complete path of the batch script, and ignore anything in the file until it finds the #!perl line. Then it treats it like any other Perl script.
The last argument on that line is simply passing all the command line arguments you pass to your batch script to the Perl as well.
So try and invoke the script. Let's say you named the script foo.bat.
foo.bat Batcheero
You'll see the following printed output.
Hey Batcheero I'm in Perl!
quite a few tips
http://batcheero.blogspot.com/
Yeah, that's the one I ended up at when I searched.Quote from: Dias de verano on January 29, 2009, 11:46:31 AMI re-read my answer and it kind of looks like I was calling you a noob, which wasn't very polite, and it wasn't my intention to be rude, so sorry if it sounded that way.
A well know trap for progressing batch programmers.
You feeling all right Dias?
FB
|