Computing.Net > Forums > Programming > BATCH - set varible with findstr

BATCH - set varible with findstr

Reply to Message Icon

Original Message
Name: beastathon
Date: December 14, 2006 at 00:33:22 Pacific
Subject: BATCH - set varible with findstr
OS: xp
CPU/Ram: 3ghz , 512ram
Model/Manufacturer: Intel
Comment:

I've got a problem with FINDSTR using a batch file in XP.

I'm trying to read a text file, and then set a certain token in that text file to a varible.
This is the code I've got so far,

[code]
FOR /F "tokens=3" %%A IN (' FINDSTR /B "." test.txt ') DO SET y=%%A
echo %y%
pause
[/code]

The text in test.txt is:

.hello world test

When I run this batch file, it just hangs and doesn't do anything.
Any help would be greatly appreciated.

Cheers,
beastathon


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: December 14, 2006 at 00:54:32 Pacific
Reply: (edit)

When I try it, it doesn't hang.



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 2
Name: beastathon
Date: December 14, 2006 at 01:04:29 Pacific
Reply: (edit)

Does it echo ' test ' ? (%y%)


Report Offensive Follow Up For Removal

Response Number 3
Name: tonysathre
Date: December 14, 2006 at 01:25:27 Pacific
Reply: (edit)

@echo off
FOR /F "tokens=3" %%A IN (text.txt) DO SET y=%%A
echo %y%
pause

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 4
Name: tonysathre
Date: December 14, 2006 at 01:31:04 Pacific
Reply: (edit)

My script echos 'test' (without the quote).

Hey M2, do you know of any forums that are devoted to batch scripting? I would love to start one if there wasn't.
"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 5
Name: beastathon
Date: December 14, 2006 at 01:36:13 Pacific
Reply: (edit)

Thanks a lot tony. :D

I thought you had to put the command in the
IN ( )

Thanks for that.


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: December 14, 2006 at 01:36:51 Pacific
Reply: (edit)

Yes, it echos 'test'. No quotes.

The test.txt I used contains only the line you provided. Does your contain more?


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 7
Name: beastathon
Date: December 14, 2006 at 01:39:27 Pacific
Reply: (edit)

No, mine contains the same as yours.

Although the problem is fixed, I would like to know IF you can use the FINDSTR command in the IN ( ) ?
I want to know this becuase I would like the use the '/E' switch with it.


Report Offensive Follow Up For Removal

Response Number 8
Name: tonysathre
Date: December 14, 2006 at 01:39:27 Pacific
Reply: (edit)

Mine contain the following string:

hello world test

The only time you need to use '' is when you are parsing the output of a command.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 9
Name: Mechanix2Go
Date: December 14, 2006 at 01:48:27 Pacific
Reply: (edit)

[1] How did you fix it?

[2] Clearly you CAN use findstr within IN() because the bat you posted works.


tony,

Did you lose the DOT at the beginning of the line?



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 10
Name: tonysathre
Date: December 14, 2006 at 01:51:52 Pacific
Reply: (edit)

Yes, I left the dot out.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 11
Name: tonysathre
Date: December 14, 2006 at 01:55:45 Pacific
Reply: (edit)

I didn't even try his script. I just rewrote it how I would have wrote it. So does his original script even work. Sorry, I'm too lazy to try it myself, It's 4 AM here. lol ;-)

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 12
Name: Mechanix2Go
Date: December 14, 2006 at 02:06:39 Pacific
Reply: (edit)

Hi tony,

The point of FINDSTR /B "." is to look at lines beginning with .

Yours will put out the 3rd token regardless of line beginning char.

Have a cappuccino on me.



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 13
Name: tonysathre
Date: December 14, 2006 at 02:21:54 Pacific
Reply: (edit)

I see now. I looked up the /b switch so now I know why the period and the command were in IN().

Thanks for the drink! ;-)

Hey M2, we should start a scripting forum. That would be fun.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 14
Name: Mechanix2Go
Date: December 14, 2006 at 02:36:16 Pacific
Reply: (edit)

I thought this was the scripting forum. LOL



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 15
Name: tonysathre
Date: December 14, 2006 at 02:41:18 Pacific
Reply: (edit)

Lol, yeah, but I mean our own "dedicated" scripting forum.

Just a thought.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 16
Name: beastathon
Date: December 14, 2006 at 02:44:43 Pacific
Reply: (edit)

What still stumps me is why my code dosent work on my computer...

It's quite odd.


Report Offensive Follow Up For Removal

Response Number 17
Name: tonysathre
Date: December 14, 2006 at 02:57:03 Pacific
Reply: (edit)

When I run your script it doesn't hang and it echoes test.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 18
Name: Mechanix2Go
Date: December 14, 2006 at 02:58:45 Pacific
Reply: (edit)

b,

Is your test.txt STRICTLY text; not wordpad or some goofy word processor?


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 19
Name: beastathon
Date: December 14, 2006 at 03:03:12 Pacific
Reply: (edit)

I wrote ' .hello world test ' in Notepad and saved it as test.txt

and by hang, the 'cursor'(not sure of the word) in cmd just flashes over and over.


Report Offensive Follow Up For Removal

Response Number 20
Name: tonysathre
Date: December 14, 2006 at 03:07:39 Pacific
Reply: (edit)

Post everything that is displayed in the command shell.


"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 21
Name: beastathon
Date: December 14, 2006 at 03:17:40 Pacific
Reply: (edit)

C:\Documents and Settings\MyName\Desktop>FOR /F "tokens=3" %A IN (' FINDSTR /B "." test.txt ') DO SET y=%a

(this is with echo on. If echo is off, it just displays the flashing cursor)

and then the blinking cursor.

I have also found out that after I close the batch file it creates three files; echo, FOR, and SET. All these files are invalid and 0 bytes.


Report Offensive Follow Up For Removal

Response Number 22
Name: Mechanix2Go
Date: December 14, 2006 at 03:30:04 Pacific
Reply: (edit)

For starters, %%A is *NOT* equivalent to %%a


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 23
Name: beastathon
Date: December 14, 2006 at 03:32:14 Pacific
Reply: (edit)

Oh sorry, they both are capital As

I do need to take more care when I type, sorry. :(


Report Offensive Follow Up For Removal

Response Number 24
Name: tonysathre
Date: December 14, 2006 at 03:33:22 Pacific
Reply: (edit)

I have no idea why that would happen.

I have never seen anything like that before.

Have you M2?

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 25
Name: Mechanix2Go
Date: December 14, 2006 at 03:53:12 Pacific
Reply: (edit)

No

Obviously I've lost the plot.

b,

Are you in CMD.exe? To find out, type:

set c

and look at comspec.

BTW, to save typing and errors, just copy and paste the screen contents.



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 26
Name: tonysathre
Date: December 14, 2006 at 04:16:48 Pacific
Reply: (edit)

I just ran his script with command.com and it say:

FINDSTR: Cannot open text.txt

CMD.exe runs the script just fine.

My guess is that your using the wrong command interpreter. Try using cmd.exe instead of command.com.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 27
Name: tonysathre
Date: December 14, 2006 at 04:21:25 Pacific
Reply: (edit)

Lol, nevermind, I was in the wrong directory. It worked fine from both shells.

This doesn't make any sense to me.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 28
Name: beastathon
Date: December 14, 2006 at 04:34:03 Pacific
Reply: (edit)

Yep, I'm using CMD.EXE

Hmmm, I wonder why those zero byte files are being created... My guess is that they have something to do with this problem.


Report Offensive Follow Up For Removal

Response Number 29
Name: beastathon
Date: December 14, 2006 at 04:39:01 Pacific
Reply: (edit)

Also, sometimes not all of the three files get created.

Sometimes one, or two, and sometimes even none.

I tried running the line:

FOR /F "tokens=3" %%A IN (' FINDSTR /B "." test.txt ') DO SET y=%%A

on its on in cmd.exe and it came up with this message:

%%A was unexpected at this time.


Report Offensive Follow Up For Removal

Response Number 30
Name: Mechanix2Go
Date: December 14, 2006 at 04:46:41 Pacific
Reply: (edit)

b,

If you run a line like that at the prompt, you need to change each %% to % ; because a BAT strips one %.

I have no idea what's going on here. Try creating a new directory and putting just the bat and the test.txt in it. BTW, what is the EXACT name of the BAT?


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 31
Name: tonysathre
Date: December 14, 2006 at 04:48:41 Pacific
Reply: (edit)

When you run that as a command instead of from a script you have to use %a instead of %%a.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 32
Name: beastathon
Date: December 14, 2006 at 04:54:50 Pacific
Reply: (edit)

Oh my,
The filename was filestr.bat
It must of been trying to find that file. Ive renamed it and now it works like a charm.

I can't believe that...

Many thanks for help tony and M2, you've taught me some new things as well.

Thank you very much. :D


Report Offensive Follow Up For Removal

Response Number 33
Name: Mechanix2Go
Date: December 14, 2006 at 05:01:33 Pacific
Reply: (edit)

I'm bamboozled. I tried it as filestr.bat and it worked just the same.

Are you sure you were not naming it FINDSTR.BAT?

If so, it would likely re-invoke itself and lose it's way.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 34
Name: beastathon
Date: December 14, 2006 at 05:05:08 Pacific
Reply: (edit)

It WAS named:

filestr.bat

in lowercase and everything, then I renamed it to something different and now it works fine.

:)


Report Offensive Follow Up For Removal

Response Number 35
Name: beastathon
Date: December 14, 2006 at 05:08:05 Pacific
Reply: (edit)

Oh Damn.

It was called FINDSTR.BAT

It's getting late here, I really think I need to get to bed and stop making so many mistakes.

Argh, I'm sorry for all this trouble.


Report Offensive Follow Up For Removal

Response Number 36
Name: Mechanix2Go
Date: December 14, 2006 at 05:10:23 Pacific
Reply: (edit)

It's a good practice to avoid naming any script, or executable, the same name as any internal or external command.

Also, avoid 'reserved words' like COM, LPT, NUL.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 37
Name: tonysathre
Date: December 14, 2006 at 14:35:23 Pacific
Reply: (edit)

Finally, we solved it. Lol.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 38
Name: beastathon
Date: December 15, 2006 at 07:14:08 Pacific
Reply: (edit)

Sorry to revive this, but I'm having trouble with another FOR command.

I've checked the script for syntax over and over and over, I can't find any reason why its doing what its doing.

FOR /F "tokens=2" %%A IN ('PING -a 192.168.0.1 -n 1') DO SET output=%%A

This sets %%A to every SECOND line in the ping command, when I thought it was meant to read only the first line.
Ive attached a img so you can see,
http://img72.imageshack.us/img72/71...

Any help would be great, thanks :D


Report Offensive Follow Up For Removal

Response Number 39
Name: beastathon
Date: December 15, 2006 at 07:16:19 Pacific
Reply: (edit)

Oops, btw, I need it to only find the name of the specified ip.
Only the second word

Thanks


Report Offensive Follow Up For Removal

Response Number 40
Name: Mechanix2Go
Date: December 15, 2006 at 12:52:00 Pacific
Reply: (edit)

Hi b,

The "tokens=2" tells it to use every 2nd 'chunk' NOT 'the 2nd line'.

Try this:

@echo off
FOR /F "tokens=2" %%A IN ('PING -a 69.46.230.40 -n 1 ^| find "Pinging"') DO SET output=%%A
echo %output%


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 41
Name: beastathon
Date: December 15, 2006 at 15:45:22 Pacific
Reply: (edit)

Thanks a lot mate, worked a treat.

:D


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: BATCH - set varible with findstr 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge