Page 1 of 1
CMD File For /f help
Posted: 02 Sep 2011 11:26
by psytae
I am trying to write a text file called nodes.txt with only the information I don't exactly know could be in the token 2 spot in the output.txt file already. I can't seem to get it. I have tried a few different ways and nothing will work I could really use some help
below is the line I just can't seem to get to work.
Code: Select all
FOR /F "tokens=2" %%A in (C:\output.txt) do (IF "%%A" == "-dns" (SET write=false)
IF "%%A" == "Network" (SET write=false)
IF "%%A" == "Directory" (SET write=false)
IF "%%A" == "NODE" (SET write=false)
IF "%%A" == "Manager" (SET write=false) ELSE (SET write=true)
IF "%write%" == "true" echo %%A >> C:\nodes.txt)
I have also tried something like an OR but it wouldn't work either.
Code: Select all
FOR /F "tokens=2" %%A in (C:\output.txt) do (IF NOT "%%A" == "-dns" OR NOT "%%A" == "Network" OR NOT "%%A" == "Directory" OR Not "%%A" == "NODE" OR NOT "%%A" == "Manager" echo %%A >> C:\nodes.txt)
Basically I am trying to output a list of server nodes that I can ping in a later step of the CMD file, and the Server names will most likely be different for every place I run this with so I don't know exactly what they will be.
Re: CMD File For /f help
Posted: 02 Sep 2011 12:24
by Ed Dyreen
'
Code: Select all
@echo off &SetLocal EnableExtensions EnableDelayedExpansion
set "$in.FullPathFile=C:\output.TXT"
set "$ou.FullPathFile=C:\nodes.TXT"
::
> "!$ou.FullPathFile!" (
for /f "tokens=2" %%? in (
"!$in.FullPathFile!"
) do for %%! in (
"-dns"
"Network"
"Directory"
"NODE"
"Manager"
) do if /i ["%%~?"] == ["%%~!"] (
::
echo.%%~?
)
)
EndLocal
pause
exit /b 0
! UNTESTED !After reviewing my code &your question; I am guessing you want the output of the 3th token ?!
If so change %%? back to alphanumeric so u can access "tokens=2,3" as %%A,%%B.
Re: CMD File For /f help
Posted: 02 Sep 2011 13:25
by psytae
Here is the output currently of my nodes.txt file
keep in mind I don't want it to write to the file when it shows -dns, Network, Directory, Node, and Manager. everything else I would like it to actually write to the nodes.txt file.
Code: Select all
-dns
Network
Directory
Node
tobias
handel
mahler
chopin
straub
dvorak
wagner
Manager
Node
Manager
Node
Manager
Node
Manager
Node
Manager
Node
Manager
Node
Re: CMD File For /f help
Posted: 02 Sep 2011 13:35
by psytae
why the ~ in the do if /i statement?
Keep in mind I am very new writing this kind of code.
Thanks
Re: CMD File For /f help
Posted: 02 Sep 2011 13:45
by psytae
Ed I just tested your script and it did the exact oposite of what I was wanting it to do. It stripped out the part I actually wanted to write to the file and wrote to the file the part I didn't want it to write. So it is closer then I have been able to do so far however I just need to get it to write to the file only when it doesn't match any of the words in the list set up.
-dns
Network
Directory
Node
Manager
Re: CMD File For /f help
Posted: 02 Sep 2011 14:05
by Ed Dyreen
'
evaluate true:
Code: Select all
if /i ["true"] == ["true"] echo.evaluate true
if /i ["true"] equ ["true"] echo.evaluate true
evaluate false:
Code: Select all
if /i not ["true"] == ["false"] echo.evaluate false
if /i ["true"] neq ["false"] echo.evaluate false
for help type /? after any command.
Re: CMD File For /f help
Posted: 02 Sep 2011 14:38
by psytae
This works but oposite I want it to strips out the words I want written to the file and writes the words I don't want written to the file.
Code: Select all
For /F "tokens=2" %%B in (C:\output.txt) do FOR %%C in (
"-dns"
"Network"
"Directory"
"Node"
"Manager"
) do IF /i ["%%~B"] == ["%%~C"] (
ECHO %%~B >> C:\nodes.txt
)
This give the same output as above. Exact oposite of what I want in the file.
Code: Select all
For /F "tokens=2" %%B in (C:\output.txt) do FOR %%C in (
"-dns"
"Network"
"Directory"
"Node"
"Manager"
) do IF /i ["%%~B"] equ ["%%~C"] (
ECHO %%~B >> C:\nodes.txt
)
This does not work I keep getting an error and it crashes out of the code
["%~B"] was unexpected at this time.
Code: Select all
For /F "tokens=2" %%B in (C:\output.txt) do FOR %%C in (
"-dns"
"Network"
"Directory"
"Node"
"Manager"
) do IF NOT /i ["%%~B"] == ["%%~C"] (
ECHO %%~B >> C:\nodes.txt
)
This writes every output to the file four times in a row, and five times if it doesn't actually match the list I set.
Code: Select all
For /F "tokens=2" %%B in (C:\output.txt) do FOR %%C in (
"-dns"
"Network"
"Directory"
"Node"
"Manager"
) do IF /i ["%%~B"] neq ["%%~C"] (
ECHO %%~B >> C:\nodes.txt
)
This gives me the same output as neq as above.
Code: Select all
For /F "tokens=2" %%B in (C:\output.txt) do FOR %%C in (
"-dns"
"Network"
"Directory"
"Node"
"Manager"
) do IF NOT ["%%~B"] == ["%%~C"] (
ECHO %%~B >> C:\nodes.txt
)
Re: CMD File For /f help
Posted: 02 Sep 2011 14:45
by Ed Dyreen
'
This writes every output to the file four times in a row
Whoops

:
Code: Select all
@echo off &SetLocal EnableExtensions EnableDelayedExpansion
set "$in.FullPathFile=C:\output.TXT"
set "$ou.FullPathFile=C:\nodes.TXT"
::
> "!$ou.FullPathFile!" (
for /f "tokens=2" %%? in (
"!$in.FullPathFile!"
) do (
set /a $error = 0
for %%! in (
"-dns"
"Network"
"Directory"
"NODE"
"Manager"
) do if /i ["%%~?"] == ["%%~!"] (
::
set /a $error = 1
)
if !$error! equ 0 echo.%%~?
)
)
EndLocal
pause
exit /b 0
! UNTESTED !Cause for is a loop and if matches only one or none of the set.
Re: CMD File For /f help
Posted: 02 Sep 2011 15:20
by psytae
That worked for me. It did exactly what I was looking for. Thank you very much Ed.