Optimize batch to find words in text file

September 20, 2010 at 14:45:16
Specs: Windows Server 2008, 4GB
Hi. With this batch find a specific word in the file temdata.txt and i need some help to optimize this script because all words are in the same file. Is this possible? Thanks.

@echo on & setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in ('find /i "HOSTNAME="^<tempdata.txt') do (
set %%a
set sdname=!HOSTNAME!
echo %sdname%>>info33.txt

for /f "tokens=*" %%b in ('find /i "IP="^<tempdata.txt') do (
set %%b
set sdip=!IP!
echo %sdip%>>info33.txt

for /f "tokens=*" %%c in ('find /i "PORT="^<tempdata.txt') do (
set %%c
set sdport=!PORT!
echo %sdport%>>info33.txt
)
)
)


See More: Optimize batch to find words in text file

Report •


#1
September 20, 2010 at 20:32:27
the least you could do is show your input file.

GNU win32 packages | Gawk


Report •

#2
September 21, 2010 at 15:05:28
Ok, here is the file tempdata.txt


HOSTNAME="Server Name"
IP=127.0.0.1
PORT=80
UPATH=C:\User\DataInfo
STATUS=0


Report •

#3
September 21, 2010 at 15:46:39
@ECHO OFF

FOR /f "tokens=2 delims==" %%a IN (tempdata.txt) DO (
	SET sdname=%%a
	GOTO Next
)

:Next

FOR /f "skip=1 tokens=2 delims==" %%a IN (tempdata.txt) DO (
	SET sdip=%%a
	GOTO Next1
)

:Next1

FOR /f "skip=2 tokens=2 delims==" %%a IN (tempdata.txt) DO (
	SET sdport=%%a
	GOTO Next2
)

:Next2

ECHO %sdname%>>info33.txt
ECHO %sdip%>>info33.txt
ECHO %sdport%>>info33.txt

I'm sure there is an easier way, but this will work.

Can I put a question here?


Report •

Related Solutions

#4
September 21, 2010 at 16:32:53
Matt123: I'm sure there is an easier way
There's always an easier way. Case in point:
setlocal
for /f "delims=" %%a in (tempdata.txt) do set %%a
>info33.txt (@echo %hostname%
@echo %IP%
@echo %port%)


Report •

#5
September 21, 2010 at 16:58:36
Thanks for all replies.

Razor2.3 Definitely that's the best way to optimize the batch file. Thanks so much.


Report •

#6
September 21, 2010 at 18:41:09
Aye, that it is. Nicely done Razor.

Can I put a question here?


Report •


Ask Question