Announcement

Collapse
No announcement yet.

DOS Batch file to pull certain info from a text file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • DOS Batch file to pull certain info from a text file

    Guys,

    I'm having trouble getting a dos batch file to work correctly to pull only certain lines from a text document. I need it to read through a .txt file and copy the lines that have an O (oh) in the 14th field and a three character value in the 19th comma delimited field to a blank txt document. Example text is below. I appreciate any help I can get with this.


    "111111","02","111476","Customer1","xxS48","001535 32601","111210","101110","1740.00",".00","011513", "P",2013-03-28 13:20,"S","","","044LP","111476","LAK"

    "222222","02","111089","Customer2","xx2584","00153 270702","111610","081110","540.10",".00","092712", "P",2013-03-27 10:33,"O","","","044LP","111089","LAK"

  • #2
    Re: DOS Batch file to pull certain info from a text file

    @echo off
    Set Src=file.txt
    Set Dest=Out.txt
    for /f "UseBackQ tokens=14,19 delims=" %%a in ("%Src%") do (
    if "%%~a"=="O" echo %%~b>> "%Dest%"
    )

    Comment


    • #3
      Re: DOS Batch file to pull certain info from a text file

      Originally posted by Dragokas View Post
      @echo off
      Set Src=file.txt
      Set Dest=Out.txt
      for /f "UseBackQ tokens=14,19 delims=" %%a in ("%Src%") do (
      if "%%~a"=="O" echo %%~b>> "%Dest%"
      )
      This isn't comma delimited however, and you're not checking for the 3 character value.

      By 3 character value, you mean anything A-Z? Or just 3 in length? Try this:

      Code:
      @echo off & setlocal enabledelayedexpansion
      
      set inputFile="Z:\file.txt"
      set outputFile="Z:\file_out.txt"
      
      if exist %outputFile% del /f/q %outputFile%
      
      set i=1
      set n=1
      for /f "UseBackQ tokens=14,19 delims=," %%G in (%inputFile%) do (
      	if /i %%~G==O (
      		call :GetLength %%~H
      		if !length!==3 (
      			set line!n!=!i!
      			set /a n+=1
      		)
      	)
      	set /a i+=1
      )
      
      set i=1
      set n=1
      for /f "UseBackQ delims=" %%G in (%inputFile%) do (
      	call set line=%%line!n!%%
      	if !i!==!line! (
      		echo %%G >> %outputFile%
      		set /a n+=1
      	)
      	set /a i+=1
      )
      
      goto :eof
      
      :GetLength
      set str=%1
      set /a length=0
      :loop
      set indexedChar=!str:~%length%,1!
      if defined indexedChar set /a length+=1 & goto loop

      Comment

      Working...
      X