# Batch File: For Loop Help!!!!!!!!!!



## closerwalk (Mar 20, 2008)

Here we go: 
I have the following batch file:


```
ipconfig | find "IP Address. . . . . . . . . . . . : 10" > ip1.txt

for /f "tokens=1-2 delims=:" %i in (ip1.txt) do echo %%j >ip2.txt
for /f "tokens=1-4 delims=." %i in (ip2.txt) do set IPADDR= %i.%j.%k.%l

echo %username%, %computername%, %date%,%time%,%IPADDR%,LOGGED ON>>\\servername\data\audit\LOGON_LOGOFF_EVENTS.txt
```
On a windows xp machine this works fine if you past it into a command prompt.

You can test it by just pasting into a command prompt or cmd prompt: 
:up: You will see that it works as advertised ... 

```
ipconfig | find "IP Address. . . . . . . . . . . . : 10" > ip1.txt

for /f "tokens=1-2 delims=:" %i in (ip1.txt) do echo %%j >ip2.txt
for /f "tokens=1-4 delims=." %i in (ip2.txt) do set IPADDR= %i.%j.%k.%l

echo %username%, %computername%, %date%,%time%,%IPADDR%,LOGGED ON
```
:down:
Heres the But: If I run it using the batch file it produces this error:

C:\>ipconfig | find "IP Address. . . . . . . . . . . . : 10" 1>ip1.txt
j was unexpected at this time.
C:\>for /f "tokens=1-2 delims=:" j >ip2.txt)

Any suggestions ???


----------



## devil_himself (Apr 7, 2007)

Greetings closerwalk , Welcome To TSG

Inside A Batch Script Two "%'s" in %%f, are required .The first % escapes the second %, so that the for command to actually work. On the command line itself, you only use one %.


```
@echo off
ipconfig | find "IP Address. . . . . . . . . . . . : 10" > ip1.txt

for /f "tokens=1-2 delims=:" %%i in (ip1.txt) do echo %%j >ip2.txt
for /f "tokens=1-4 delims=." %%i in (ip2.txt) do set IPADDR= %%i.%%j.%%k.%%l

echo %username%, %computername%, %date%,%time%,%IPADDR%,LOGGED ON
```


----------



## closerwalk (Mar 20, 2008)

Thanks, I total overlooked that...


----------



## devil_himself (Apr 7, 2007)

Glad We Could Help 

You Can Use The "Thread Tools" At The Top To Mark The "Thread" As "Solved"


----------

