So I'm trying to learn to write batch scripts for work, and this for loop is screwing me up.
First, here are the contents of "file_test.txt":
1 2 3
4 5 6
Second, here are the contents of "test.bat":
@echo off
SetLocal EnableDelayedExpansion
FOR /F "delims=" %%a IN (test_file.txt) DO (
echo a: %%a
set b = %a%
echo b: !b!
)
And this is the results I get:
a: 1 2 3
b:
a: 4 5 6
b:
What on Earth am I doing wrong?
EDIT: I suppose I should say, this is the results I want:
a: 1 2 3
b: 1 2 3
a: 4 5 6
b: 4 5 6
Posts
You can call a subroutine to similar effect: Drawback there is the subroutine interprets %%a as 3 items (delimited by " "), so you need to know how many args are coming in. Probably a way around that, but like I said: I'm rusty.
However. If you're just starting, could I suggest you check out Python instead? The syntax isn't much more complicated, and it's a lot more flexible. I used to be all about a nice bat file, but I've moved on and haven't looked back.
full text of a .py file that does the same thing you're trying to do:
the above is Python 3.2
2.x is a bit different (incompatible), but still widely used. Whichever you choose, if you google up some code and aren't sure which version it is (not usually a problem), check if the print functions use parenthesis:
print("3.x")
print "2.x"
Output:
Of course if it's for work you quite possibly can't. It's not like I use tcsh everyday because it's what I would freely choose.
And condensed!
fuck up once and you break your thumb / if you're happy at all then you're god damn dumb
that's right we're on a fucked up cruise / God is dead but at least we have booze
bad things happen, no one knows why / the sun burns out and everyone dies
The 'a' you're using in your loop is not the same variable as %a%.
Oops. I've never used (or seen) '!b!' before, or used delayed expansion. So I guess that's what it does!
But it's worth mentioning that the %%a variables in FOR loops actually hold more information than you might think.
run 'for /?' for a full list if you get shut down on using an alternative to batch.
Go for python over perl if you can manage it. It's like a billion times more readable!
Not that you should make your decision based on a programming joke, but if you haven't checked out python you definitely should at least take a look at it! Readable as your perl may be, python is quite pretty.
The reason it does that is to actively fight against obfuscated code. It does feel weird not to use braces, especially for a person who primarily uses c-like languages. We share that quality. You just have to think of it this way:
In every single language, whether you can write a whole app on one line or not, white space is the most important factor there is when readability is concerned. It is one of the only things that just about every programmer will do exactly the same (length of indentation notwithstanding, of course), and the reason is because everyone expects everyone else to have identical indentation.
In this way, python is not enforcing whitespace, style, or convention, it is enforcing readability.