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

Author Topic: Extract info from Dir  (Read 3709 times)

0 Members and 1 Guest are viewing this topic.

Woodman

    Topic Starter


    Beginner
    Extract info from Dir
    « on: December 09, 2008, 09:35:20 PM »
    Win XP H.

    I'm trying to write a batch file to extract some details from a dir listing.  So far I've got:

    Quote
    @echo off
    cls

    for /f "Tokens=* skip=4" %%a in ('dir c:\*.*') do (
        set line=%%a
        echo !line:~0,14!


    )
    which works quite well but the last two lines of the listing contain the files and directory info which I don't want.  The info extracted from those lines is
    Quote
    195 File(s)
    29 Dir(s) 11,

    Is there any way of getting the for loop to ignore the final two lines.  Obviously the contents of the lines will change when another folder is listed.

    Thanks

    diablo416



      Hopeful
      Re: Extract info from Dir
      « Reply #1 on: December 09, 2008, 11:45:31 PM »
      Use the /b switch with dir , the bare format

      diablo416



        Hopeful
        Re: Extract info from Dir
        « Reply #2 on: December 09, 2008, 11:52:11 PM »
        Since you are only setting one variable you dont need to use an array.. and with /b switch you dont need to skip any lines


        for /f "tokens=*" %%a in ('dir c:\*') do set line=%%a

        Woodman

          Topic Starter


          Beginner
          Re: Extract info from Dir
          « Reply #3 on: December 10, 2008, 12:06:27 AM »
          The idea is to be able to extract information from the Dir listing.  That information could be any combination of the information produced by Dir, last access date, created date, last written date, file size, filename etc.  using the /b switch suppresses everything but the file/folder name so how could I extract say the file/folder name and creation date if /b is used???

          Dias de verano

          • Guest
          Re: Extract info from Dir
          « Reply #4 on: December 10, 2008, 12:13:42 AM »
          The idea is to be able to extract information from the Dir listing.  That information could be any combination of the information produced by Dir, last access date, created date, last written date, file size, filename etc.  using the /b switch suppresses everything but the file/folder name so how could I extract say the file/folder name and creation date if /b is used???

          You can still get the file info with dir /b. Type for /? at the prompt and read the part about variable modifiers.



          Dias de verano

          • Guest
          Re: Extract info from Dir
          « Reply #5 on: December 10, 2008, 12:15:17 AM »
          Since you are only setting one variable you dont need to use an array..

          What array? Since when did cmd have arrays?

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: Extract info from Dir
          « Reply #6 on: December 10, 2008, 12:31:58 AM »
          Since you are only setting one variable you dont need to use an array..

          What array? Since when did cmd have arrays?

          don't be so sure, maybe diablo416 knows something you don't. <snicker>

          heh. yeah.... ::)


          When did we start helping Megaman 2 Robot masters anyway?
          I was trying to dereference Null Pointers before it was cool.

          Woodman

            Topic Starter


            Beginner
            Re: Extract info from Dir
            « Reply #7 on: December 10, 2008, 12:57:42 AM »
            You can still get the file info with dir /b. Type for /? at the prompt and read the part about variable modifiers.

            Right on the mark thank you.  I found everything I need except a syntax to return file ownership details.  Do you know of one?  I tried o and q without success.


            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Extract info from Dir
            « Reply #8 on: December 10, 2008, 05:00:52 AM »
            This would be a case of knowing your data up close and personal. You can send the directory list through the pipe, excluding lines which have either an open parenthesis or a close parenthesis. Other characters could be used and you could include characters as well.

            Code: [Select]
            @echo off
            cls
            setlocal enabledelayedexpansion
            for /f "skip=4 tokens=1-6" %%a in ('dir /q /a:-d c:\*.* ^| find /i /v "(" ^| find /i /v ")"') do (
                echo Owner: %%e File: %%f
            )

            To access the other columns in the dir list, you'll need to reference the other variables defined by the tokens parameter.

            Quote
            Quote from: Dias de verano on Today at 02:15:17 AM
            Quote from: diablo416 on Today at 01:52:11 AM
            Since you are only setting one variable you dont need to use an array..

            What array? Since when did cmd have arrays?

            don't be so sure, maybe diablo416 knows something you don't. <snicker>

            Personally I have always thought of batch language as a trip down the rabbit hole, so who better to consult than the White Knight about arrays in batch code:

            Code: [Select]
            set month=12
            for /f "tokens=%month%" %%i in ("January February March April May June July August September October November December") do (
                    set month=%%i
            )
            echo %month%

            Hmmmm, his logic could be backwards, but since he is talking backwards, he got it right! 8)

            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            Dias de verano

            • Guest
            Re: Extract info from Dir
            « Reply #9 on: December 10, 2008, 11:42:13 AM »
            Personally I have always thought of batch language as a trip down the rabbit hole, so who better to consult than the White Knight about arrays in batch code:

            Code: [Select]
            set month=12
            for /f "tokens=%month%" %%i in ("January February March April May June July August September October November December") do (
                    set month=%%i
            )
            echo %month%

            Hmmmm, his logic could be backwards, but since he is talking backwards, he got it right! 8)


            That's a pseudo-array.

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Extract info from Dir
            « Reply #10 on: December 10, 2008, 02:44:03 PM »
            Personally I have always thought of batch language as a trip down the rabbit hole, so who better to consult than the White Knight about arrays in batch code:

            Code: [Select]
            set month=12
            for /f "tokens=%month%" %%i in ("January February March April May June July August September October November December") do (
                    set month=%%i
            )
            echo %month%

            Hmmmm, his logic could be backwards, but since he is talking backwards, he got it right! 8)


            That's a pseudo-array.

            Exactly; I see no subscripts anywhere.
            I was trying to dereference Null Pointers before it was cool.

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Extract info from Dir
            « Reply #11 on: December 10, 2008, 06:28:23 PM »
            Good grief! Tough crowd. ;D

            Quote
            That's a pseudo-array.

            What is a pseudo-array?

            Quote
            Exactly; I see no subscripts anywhere.

            I was under the impression that the month variable acted as an index into the array. However the White Knight suggested the following snippet for all the index lovers out there:

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion

            : Make Array
            for /l %%y in (1, 3, 68) do (
              call set /a idx=%%idx%%+1
              call set array.%%idx%%=%%y
            )

            : Read Array
            for /l %%i in (1,2,11) do (
              for /f  %%x in ("!array.%%i!") do (
                echo Array Index: %%i  Value: %%x
              )
            )

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

            -- Albert Einstein

            Woodman

              Topic Starter


              Beginner
              Re: Extract info from Dir (Resolved)
              « Reply #12 on: December 10, 2008, 08:26:06 PM »
              Thank you Sidewinder - the code in your reply #8 is a great learning experience and of course works splendidly.

              The other comments in various replies about arrays, pseudo-arrays, White Knight et al are way above my level of knowledge.  I cannot find that an array as shown in your reply #11 has anything to do with extracting Dir details but I'll keep looking (or did my thread just get hijacked?).

              Thanks to all for the input, much appreciated.

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: Extract info from Dir
              « Reply #13 on: December 10, 2008, 09:16:02 PM »

              I was under the impression that the month variable acted as an index into the array. However the White Knight suggested the following snippet for all the index lovers out there:



              any attempt to use arrays in batch is a pseudo array. the definition of an array holds that they all have the same name, and are accessed via noncongruous subscripts. In your examples, each variable has a name that is simply formed from the index it refers to, there is no way to reference the array as a whole.

              for example, if you were to list environment variables after creating a "array" as you've described it, it will show all the "elements" of the "array".


              Although I must contend that it is a good workaround that offers array-like access methods.


              A similar thing occured with javascript, java doesn't have intrinsic support for arrays. The original solutions were pretty similar to that which you've created. Anyway, the entire problem dissapeared when JavaScript became an ECMAscript, and the standard (i believe) imposed the creation of an "Array" object.

              Arrays are something that can be simulated, but unless the language specification actually mentions it, you can assume that you can't create anything that can be called an array in formal terms. (VB docs mention array a lot... same with C and a lot of other languages (javascript notwithstanding, lol), but the docs for command line extensions and so forth? Don't think so.)

              What you've done is analogous to creating a file that contains other files, and calling it a file system. While you can access the files, you cannot do so using standard file system methods, just as you can do array-like things with your creation here, but cannot access it using standard array-access methods as supported by numerous array-featuring languages (javascript notwithstanding).

              And finally, I must mention that it is a clever way of adding the ability to work with sets of data, assuming environment space isn't at a premium, of course.
              I was trying to dereference Null Pointers before it was cool.