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

Author Topic: Need to pull info from a large number of text files in a directory  (Read 4778 times)

0 Members and 1 Guest are viewing this topic.

dec27

  • Guest
I need to pull info from a large number of textfiles in a directory and aggregate this to another textfile. The info needed is two lines (line 1 and line 9) in the textfile, and these lines are consistently lines 9 and 16 across all the textfiles.

I need this info from all the files. The textfiles however are unique to that folder structure e.g
1st textfile = U:\abailey\NetLogon.txt
2nd textfile = U:\ablay\NetLogon.txt
3rd textfile = U:\abreen\NetLogon.txt etc...

Can you please help me with this one, or even let me know if its possible to do, because I really want to get this now at this stage :-) Thanks!

Dias de verano

  • Guest
Re: Need to pull info from a large number of text files in a directory
« Reply #1 on: March 18, 2008, 10:59:48 AM »
Yes it is possible to do.

Is the folder structure like this

Root folder of drive U\aname\Netlogon.txt

Also in your question, first of all you said

Quote
The info needed is two lines (line 1 and line 9) in the textfile,

Then you said

Quote
these lines are consistently lines 9 and 16 across all the textfiles.

Which is it?



               

macdad-



    Expert

    Thanked: 40
    Re: Need to pull info from a large number of text files in a directory
    « Reply #2 on: March 18, 2008, 05:25:03 PM »
    Dias, i think he wants to take the info from lines 1 and 9 in the text files then copy the info, and then paste the info to lines 9  and 16 on one txt file.
    If you dont know DOS, you dont know Windows...

    Thats why Bill Gates created the Windows NT Family.

    macdad-



      Expert

      Thanked: 40
      Re: Need to pull info from a large number of text files in a directory
      « Reply #3 on: March 18, 2008, 07:43:20 PM »
      dec27 i will find how to do that but in the mean while i need to know what operating sys u ar using so i can get the correct code.
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      Dias de verano

      • Guest
      Re: Need to pull info from a large number of text files in a directory
      « Reply #4 on: March 19, 2008, 01:06:53 AM »
      Dias, i think he wants to take the info from lines 1 and 9 in the text files then copy the info, and then paste the info to lines 9  and 16 on one txt file.

      That's a very odd requirement indeed, if that is so.

      macdad-



        Expert

        Thanked: 40
        Re: Need to pull info from a large number of text files in a directory
        « Reply #5 on: March 19, 2008, 11:20:22 AM »
        try this.

        Code: [Select]
        echo off
        cls
        md admin
        copy U:\abailey\NetLogon.txt \admin
        copy U:\ablay\NetLogon.txt \admin
        copy U:\abreen\NetLogon.txt \admin
        echo Login Information Archived
        pause
        exit
        i tried doing wat u said by copying specific lines but it cant be done automaticly. the code above will copy the "NetLogon" files from each of the folders to a folder that will be created automaticly called "admin". make sure u copy the code above to notepad and save it as a bat file.
        hope this works for u. 8)
        If you dont know DOS, you dont know Windows...

        Thats why Bill Gates created the Windows NT Family.

        Dias de verano

        • Guest
        Re: Need to pull info from a large number of text files in a directory
        « Reply #6 on: March 19, 2008, 11:26:01 AM »
        i tried doing wat u said by copying specific lines but it cant be done automaticly.

        Yes it can.

        macdad-



          Expert

          Thanked: 40
          Re: Need to pull info from a large number of text files in a directory
          « Reply #7 on: March 19, 2008, 11:36:34 AM »
          how? the only way i can think of copying specific lines is by using edlin but u can only do that manually.
          If you dont know DOS, you dont know Windows...

          Thats why Bill Gates created the Windows NT Family.

          Dias de verano

          • Guest
          Re: Need to pull info from a large number of text files in a directory
          « Reply #8 on: March 19, 2008, 11:55:10 AM »
          Set a counter variable equal to 1 using set /a

          Read the text file line by line in a FOR loop, adding 1 to the variable each time with set /a

          If the variable value is equal to the line number number desired, do something with that line.

          Or you can use SED but that is not part of Windows so some people might think it was cheating.



          guardian



            Beginner

            Re: Need to pull info from a large number of text files in a directory
            « Reply #9 on: March 19, 2008, 12:06:01 PM »
            I am not trying to hijack this or anything but I am having the same problem but I am looking at only trying to find a certain lien that could be anywhere in the text file but I am looking through at this time about 4,000 files.  I am not ever sure where to start on the coding of this.  Is there a way to do this since It is a bit different than what the original poster is trying to do.

            Dias de verano

            • Guest
            Re: Need to pull info from a large number of text files in a directory
            « Reply #10 on: March 19, 2008, 12:13:08 PM »
            This is how you get a selected line from a text file without using SED

            Code: [Select]

            @echo off
            setlocal enabledelayedexpansion

            REM create test file

            echo 1 Line one > test.txt
            echo 2 Line two >> test.txt
            echo 3 Line three >> test.txt
            echo 4 Line four >> test.txt
            echo 5 Line five >> test.txt
            echo 6 Line six >> test.txt
            echo 7 Line seven >> test.txt
            echo 8 Line eight >> test.txt
            echo 9 Line nine >> test.txt
            echo 10 Line ten >> test.txt
            echo 11 Line eleven >> test.txt

            REM show test file
            echo.
            echo Here is the test file:
            echo.
            type test.txt
            echo.
            set /p chosenline=Choose a line to display?
            echo.
            set /a line=1
            for /f "delims==" %%L in (test.txt) do (
            if !line!==%chosenline% echo %%L
            set /a line=!line!+1
            )
            echo.


            Dias de verano

            • Guest
            Re: Need to pull info from a large number of text files in a directory
            « Reply #11 on: March 19, 2008, 12:14:02 PM »
            I am not trying to hijack this or anything but I am having the same problem but I am looking at only trying to find a certain lien that could be anywhere in the text file but I am looking through at this time about 4,000 files.  I am not ever sure where to start on the coding of this.  Is there a way to do this since It is a bit different than what the original poster is trying to do.

            Describe some features of the line you are looking for

            macdad-



              Expert

              Thanked: 40
              Re: Need to pull info from a large number of text files in a directory
              « Reply #12 on: March 19, 2008, 01:26:59 PM »
              but wont it be easier to just copy the files.
              If you dont know DOS, you dont know Windows...

              Thats why Bill Gates created the Windows NT Family.

              Dias de verano

              • Guest
              Re: Need to pull info from a large number of text files in a directory
              « Reply #13 on: March 19, 2008, 01:34:34 PM »
              Quote
              but wont it be easier to just copy the files.

              If you just want one line out of each file, why clutter up your disk space uselessly copying hundreds or thousands of files? When you still have to look through them to find the lines that you want?


              ghostdog74



                Specialist

                Thanked: 27
                Re: Need to pull info from a large number of text files in a directory
                « Reply #14 on: March 20, 2008, 11:14:52 PM »
                I need to pull info from a large number of textfiles in a directory and aggregate this to another textfile. The info needed is two lines (line 1 and line 9) in the textfile, and these lines are consistently lines 9 and 16 across all the textfiles.

                I need this info from all the files. The textfiles however are unique to that folder structure e.g
                1st textfile = U:\abailey\NetLogon.txt
                2nd textfile = U:\ablay\NetLogon.txt
                3rd textfile = U:\abreen\NetLogon.txt etc...

                Can you please help me with this one, or even let me know if its possible to do, because I really want to get this now at this stage :-) Thanks!
                if you can  download and use gawk from here:

                Code: [Select]
                #save the below script as script.awk
                # assumes you only want to get line 1 and 5
                {
                  i=1
                  while( ( getline line < $0 ) > 0 ) {   
                    if (i==1 || i==5) print line > "newfile"
                    i+=1
                  }
                }

                output:
                Code: [Select]
                C:\test>dir /B  /S NetLogon.txt | gawk -f script.awk