CMD File For /f help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
psytae
Posts: 9
Joined: 02 Sep 2011 10:23

CMD File For /f help

#1 Post by psytae » 02 Sep 2011 11:26

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: CMD File For /f help

#2 Post by Ed Dyreen » 02 Sep 2011 12:24

'

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.
Last edited by Ed Dyreen on 02 Sep 2011 14:12, edited 1 time in total.

psytae
Posts: 9
Joined: 02 Sep 2011 10:23

Re: CMD File For /f help

#3 Post by psytae » 02 Sep 2011 13:25

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

psytae
Posts: 9
Joined: 02 Sep 2011 10:23

Re: CMD File For /f help

#4 Post by psytae » 02 Sep 2011 13:35

why the ~ in the do if /i statement?

Keep in mind I am very new writing this kind of code.

Thanks

psytae
Posts: 9
Joined: 02 Sep 2011 10:23

Re: CMD File For /f help

#5 Post by psytae » 02 Sep 2011 13:45

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: CMD File For /f help

#6 Post by Ed Dyreen » 02 Sep 2011 14:05

'
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.
Last edited by Ed Dyreen on 02 Sep 2011 14:39, edited 1 time in total.

psytae
Posts: 9
Joined: 02 Sep 2011 10:23

Re: CMD File For /f help

#7 Post by psytae » 02 Sep 2011 14:38

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
)
Last edited by psytae on 02 Sep 2011 14:45, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: CMD File For /f help

#8 Post by Ed Dyreen » 02 Sep 2011 14:45

'
This writes every output to the file four times in a row
Whoops :mrgreen: :

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.

psytae
Posts: 9
Joined: 02 Sep 2011 10:23

Re: CMD File For /f help

#9 Post by psytae » 02 Sep 2011 15:20

That worked for me. It did exactly what I was looking for. Thank you very much Ed.

Post Reply