Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File help  (Read 3449 times)

0 Members and 1 Guest are viewing this topic.

direct

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Batch File help
    « on: September 28, 2011, 10:40:14 AM »
    I have a txt file download in which I need to remove lines of code which contain more than a specified string... The file is comma delimited and contains symbols ie, abc, .... some lines contain more than 3 letters before the comma ie, abcdef,   I want to remove all lines of code which contain more than 3 letters before the "," (comma). I have NO idea how to go about this. Anybody care to help?

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch File help
    « Reply #1 on: September 28, 2011, 01:58:39 PM »
    The fun thing about batch file tokens is that they morph into new identities depending on the context in which they are used.

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion

    if exist output.txt del output.txt

    for /f "tokens=1* delims=," %%i in (input.txt) do (
      set len=0
      call :length %%i
      if !len! LEQ 3 echo %%i,%%j >> output.txt
    )
    goto :eof 

    :length
      set var=%1
    :loop
      if defined var (set var=%var:~1%& set /a len+=1 & goto loop)
      goto :eof

    Any paths to input.txt and output.txt are your responsibility.

    The following data was used for testing. If incorrect, please post a sample of your input.

    Quote
    abc,defgt,fvbghb,nhbgft,df
    adscv,deftg,hyjuii,kjnmko

    If you are running Vista, VBScript would be a better alternative than batch. If you a running Win7, Powershell would probably be the easiest solution.

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    direct

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Batch File help
      « Reply #2 on: September 28, 2011, 02:21:34 PM »
      Fantastic, all I need to do is learn a new language so I can read what you wrote.  :D What is a batch file "TOKEN"? :o   Silly me, I thought writing a batch file might make things easier. Just joking sidewinder, Thanks for the help but I guess manual will have to do. Cheers.....
      « Last Edit: September 28, 2011, 02:32:05 PM by direct »

      direct

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: Batch File help
        « Reply #3 on: September 28, 2011, 03:27:02 PM »
        I am sorry folks I thought this might have been a simple exercise.

        I download a file each day which has has something which looks like the following....

        AAX,20110914,2.55,2.56,2.45,2.47,142344
        AAY,20110914,0.05,0.05,0.05,0.05,0
        AAZPB,20110914,87.85,87.85,87.5,87.52,1253
        ABC,20110914,2.69,2.7,2.61,2.63,3338636
        ABN,20110914,0.001,0.001,0.001,0.001,0

        There are approximately 2500 lines but as you can see, all lines begin with a 3 letter code (stock symbol). I only want to retain the lines of code which have 3 characters in the symbol which ends (always) in a comma "," If I can clean the file it means that my database is not cluttered with 'stuff' that I don't want, saving the need for removal as a maintanence project at the weekend. So what I would like to do is simply look at each line, find the first comma, and if that line has more than 3 characters before it encounters the comma, remove to another file so that I can eyeball to make sure I have not deleted something by mistake...... then I can convert the filtered stuff for use by another piece of software.

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Batch File help
        « Reply #4 on: September 28, 2011, 04:37:37 PM »
        The original code trashed the records which had more than 3 character stock codes. This new and improved (and more expensive ;D)  model pushes the rejects into a file called remove.txt for your review.

        As before, paths to the files are your responsibility.

        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion

        if exist output.txt del output.txt
        if exist remove.txt del remove.txt

        for /f "tokens=1* delims=," %%i in (input.txt) do (
          set len=0
          call :length %%i
          if !len! LEQ 3 (echo %%i,%%j >> output.txt
          ) else (echo %%i,%%j >> remove.txt)
        )
        goto :eof

        :length
          set var=%1
        :loop
          if defined var (set var=%var:~1%& set /a len+=1 & goto loop)
          goto :eof

        Good luck.  8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        direct

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Unknown
          Re: Batch File help
          « Reply #5 on: September 28, 2011, 04:52:41 PM »
          Thanks Sidewinder,

          I will just accept and test for my purposes. I promise I will try not to understand it.  ;D Your help and Greg's is much appreciated.

          direct

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Unknown
            Re: Batch File help
            « Reply #6 on: September 28, 2011, 05:16:49 PM »
            Created a file in Notepad using your code.... named it Alternative (to use for testing) and pasted the data of one of my real files to that file.



            @echo off
            setlocal enabledelayedexpansion

            if exist C:\Shares\Cooltrader Data\output.txt del C:\Shares\Cooltrader Data\output.txt
            if exist C:\Shares\Cooltrader Data\remove.txt del C:\Shares\Cooltrader Data\remove.txt

            for /f "tokens=1* delims=," %%i in C:\Shares\Cooltrader Data\Alternate.txt do (
              set len=0
              call :length %%i
              if !len! LEQ 3 (echo %%i,%%j >> output.txt
              ) else (echo %%i,%%j >> remove.txt)
            )
            goto :eof

            :length
              set var=%1
            :loop
              if defined var (set var=%var:~1%& set /a len+=1 & goto loop)
              goto :eof


            Result did not create output.txt or remove.txt files.

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Batch File help
            « Reply #7 on: September 28, 2011, 06:02:18 PM »
            Your file labels include embedded spaces and therefore require quotes:

            Code: [Select]
            if exist "C:\Shares\Cooltrader Data\output.txt" del "C:\Shares\Cooltrader Data\output.txt"
            if exist "C:\Shares\Cooltrader Data\remove.txt" del "C:\Shares\Cooltrader Data\remove.txt"

            Code: [Select]
            for /f "tokens=1* delims=," %%i in "C:\Shares\Cooltrader Data\Alternate.txt" do (

            Also check the third if statement for the output and remove files. Unless you add paths, they will end up in the current directory (the one you see at the prompt). This is not a problem, just a heads-up.

            I tested with the data you posted, so this should solve any remaining problems.

             8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein