|
|
|
BATCH - set varible with findstr
|
Original Message
|
Name: beastathon
Date: December 14, 2006 at 00:33:22 Pacific
Subject: BATCH - set varible with findstr OS: xpCPU/Ram: 3ghz , 512ramModel/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 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 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 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 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 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 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 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 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 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 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
|
Use following form to reply to current message:
|
|

|