Announcement

Collapse
No announcement yet.

Copy part of a txt file

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

  • Copy part of a txt file

    Im trying to copy part of a txt file to a new file or delete everything except the part of the file I need. The only problem is that the part I want will be changing on a daily basis.

    I had thought to use somehting like
    Code:
    @echo off
    setlocal enabledelayedexpansion
    
    FOR /F "usebackq delims=" %%G IN ("textfile1.txt") DO (
      Set "line=%%G" & echo !line:"=!
    )>>"textfile2.txt"
    but i cant figure out how to delete everything except what i want.

    here is what the content of the file looks like
    Code:
    [DefDates]
    CurDefs=20090401.003
    LastDefs=20090331.019
    all i want to keep is that ever CurDef equals.

  • #2
    Re: Copy part of a txt file

    Some Powershell saves the day. This one reads source.txt and appends each line that begins with Cur into dst.txt.

    Code:
    $src = get-content "source.txt"
    
    $current = ""
    foreach ($l in $src) {
        if($l -like "Cur*") {
            add-content "dst.txt" $l
        }
    }
    -vP

    Comment


    • #3
      Re: Copy part of a txt file

      Maybe something like this too if you want to stay with the batch script...I don't know if my syntax is right, I'm not that good with batch scripting:

      Code:
      @echo off
      setlocal enabledelayedexpansion
      
      For /f "tokens=1,*" %%G in (
           'textfile1.txt ^| find /i "CurDefs"') do (
           Set "line=%%G" & echo !line:"=!)>>"textfile2.txt"
      Last edited by ekrengel; 3rd April 2009, 21:30. Reason: Took out "/v" - displays all lines not containing specified string

      Comment


      • #4
        Re: Copy part of a txt file

        I have a script that writes stderror and stdout to a text file (lets call it results.txt). I would like to be able to parse through results.txt to find the string for the word "Connecting" and redirect that output to another text file called "output.txt". I can't seem to get it working with the methods or syntax shown in the last post. I'm still working on it. I will post the answer if I get it to work. Please post if you find the answer.

        Thanks,

        Comment


        • #5
          Re: Copy part of a txt file

          I finally got it to work the way I wanted. Here is what I came up with

          Code:
          @echo off
          setlocal enabledelayedexpansion
          For /f "tokens=2 delims='='" %%A IN ('type "definfo.txt" ^| find /i "curdefs"') DO echo %%A >> "textfile2.txt"
          Pause
          It looks for the second toekn in the line with curdefs and then ourputs that to a new file.

          Comment

          Working...
          X