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

Author Topic: how to asign dir result to the variable  (Read 10001 times)

0 Members and 1 Guest are viewing this topic.

animo

    Topic Starter


    Starter

    how to asign dir result to the variable
    « on: October 30, 2009, 03:15:11 AM »
    how could i  asign dir result to the variable
    this dosnot works :(

    set /a result=dir /b /o:n

    something like this dosnnot work  either

    set /a result=`dir /b /o:n`


    billrich

    • Guest
    Re: how to asign dir result to the variable
    « Reply #1 on: October 30, 2009, 04:38:50 AM »

    C:\batextra>type  sortdir.bat
    Code: [Select]
    @echo off

    setLocal EnableDelayedExpansion

    dir /b /o:n *.bat >  sortdir.txt
    set /a c=0
    for /f "delims=" %%a in (sortdir.txt) do (
    set /a c+=1

      set result!c!=%%a

    echo result!c! = %%a

    if  !c! equ  10   goto eof
    )

    :eof

    Output:


    C:\batextra> sortdir.bat
    result1 = 2line.bat
    result2 = 2pdfdos.bat
    result3 = A.bat
    result4 = anecho.bat
    result5 = ans.bat
    result6 = AUTOEXEC.BAT
    result7 = B.bat
    result8 = Batch1.bat
    result9 = batchname.bat
    result10 = batexe.bat

    C:\batextra>

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: how to asign dir result to the variable
      « Reply #2 on: October 30, 2009, 05:19:01 AM »
      how could i  asign dir result to the variable
      this dosnot works :(

      set /a result=dir /b /o:n

      something like this dosnnot work  either

      set /a result=`dir /b /o:n`



      i have a question for you. what do you want to do with those directory listing results? are you going to find files ? If say you have 10000 files under the directory, are you going to assign all these files a variable each ??

      billrich

      • Guest
      Re: how to asign dir result to the variable
      « Reply #3 on: October 30, 2009, 08:46:10 AM »
      how could i  asign dir result to the variable
      this dosnot works :(

      set /a result=dir /b /o:n

      something like this dosnnot work  either

      set /a result=`dir /b /o:n`


      We cannot assign all the filenames in a directory to one variable.

      Usually only one line from a file to each variable.

      « Last Edit: October 30, 2009, 07:50:31 PM by billrich »

      billrich

      • Guest
      Re: how to asign dir result to the variable
      « Reply #4 on: October 30, 2009, 04:21:45 PM »
      how could i  asign dir result to the variable
      this dosnot works :(

      set /a result=dir /b /o:n

      something like this dosnnot work  either

      set /a result=`dir /b /o:n`



      Where did Animo go?  Ask a question and leave the room?

      A variable is a storage location in RAM ( Random Access Memory ).
      The amount of information we can store as a variable is limited ( usually one line from a text file  with batch. ).  When you shut down the information disappears.

      A file is a memory location on the Hard Disk.  The information remains after shut down.

      Why would anyone expect to store all the information from a file in a variable location?

      Your basic understanding of computers is not present.

      « Last Edit: October 30, 2009, 07:46:10 PM by billrich »

      Salmon Trout

      • Guest
      Re: how to asign dir result to the variable
      « Reply #5 on: October 30, 2009, 05:37:59 PM »
      Quote
      Your basic understanding of computers is not present.

      And some people can't separate a quote from their own reply.

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: how to asign dir result to the variable
        « Reply #6 on: October 30, 2009, 06:39:13 PM »


        separate your quotes from your replies. Its annoying.

        Quote
        The amount of information we can store as a variable is limited ( usually one line from a text file  with batch. ). 
        this has nothing to do with the question. Besides, the above is statement is wrong. The amount of information we can store in a variable is as equivalent to how much memory the computer system has.  If you read a text file line by line and store into different variables for each line, you ARE basically using memory for the whole file. Get your perspective right. Also, there are functions in programming languages that reads the whole file into one big variable for the purpose of convenience, but ultimately, storing to many variables or one variable are both the same, they are both still using up memory.

        Quote from: billrich
        Your basic understanding of computers is not present.
        the pot calling the kettle black.

        billrich

        • Guest
        Re: how to asign dir result to the variable
        « Reply #7 on: October 30, 2009, 08:17:50 PM »
        Pot and Kettle.
         

        Animo(OP) wrote
        "set /a result=`dir /b /o:n"`


        Animo wrote:
        "How could I  assign dir result to the variable
        The above does not work."


        Bill wrote:
        "We cannot assign all the filenames in a directory to
        one variable with the for loop in Batch."

        "Usually only one line from a file to each variable."

        Bill Wrote:
        The amount of information we can store as a variable is limited

        ( usually one line from a text file  with batch. ). 


        Ghost wrote (Fed by BC) :

        "This has nothing to do with the question."

        Bill explains:

        Read what Animo (OP) wrote above: Animo was trying  to
        assign all the files in a directory to one variable ( Result ).


        Ghost wrote
        "Besides, the above is statement is wrong.
        The amount of information we can store in a variable is
        equivalent to how much memory the computer system has.  If you read a text file line by line and store into different variables for each line,
        you ARE basically using memory for the whole file.

        Also, there are functions in programming languages
        that reads the whole file into one big variable for the purpose of convenience, but ultimately, storing to many variables or one variable are both the same, they are both still using up memory."

        Bill wonders:

        "What does the above have to do with Batch?"


        No, Ghost you are wrong but you do not use Batch. The Batch for loop will allow only  one line to be assigned to each variable.

        So, what The Ghost writes has nothing to do with Animo's (OP) question. 

         GHOST, What is this about a Pot and Kettle?
        « Last Edit: October 30, 2009, 08:30:10 PM by billrich »

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: how to asign dir result to the variable
        « Reply #8 on: October 30, 2009, 08:26:14 PM »
        This has come up before.
        If you have a huge amount to information that you need to store, common practice is to put into some kind of database that can be stored as a file.
        For practical reasons the number of variables you can have is limited.
        If you use a FOR loop and the variables are part of the FOR structure, the limit is 26 variables.
        A vailable should not be over 128 characters., says one reference. Some versions of ODS put a limit to the amount of memory that will be assigned to variables.

        Yet if the OP wants to put the names of all the directories into some variables, he can do that. Maybe we don't know why, but it can be done.

        Anyway, being rude to anyone has little value here.  :)

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: how to asign dir result to the variable
          « Reply #9 on: October 30, 2009, 08:56:50 PM »
          Bill Wrote:
          The amount of information we can store as a variable is limited
          ( usually one line from a text file  with batch. ). 
          i say again. The amount of information you can store in a variable is unlimited, its only limited by the amount of physical  RAM you have on your system!!! Whether or not DOS batch only read one line each time from a file is  not the problem. I don't do batch , BUT i certainly know how to program.
          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          set var=
          for /f "delims=" %%a in (file) do (
            set line=%%a
            set var=!var! !line!
          )
          echo The file contents are: %var%
          Since you are a so called batch guru, i assume you can see that %var% is one big variable, now containing all the lines of the file. Now, do you understand what this means???

          Quote
          GHOST, What is this about a Pot and Kettle?
          now i have to give you english lessons? go figure out yourself.

          billrich

          • Guest
          Re: how to asign dir result to the variable
          « Reply #10 on: October 30, 2009, 09:20:18 PM »
          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          set var=
          for /f "delims=" %%a in (file) do (
            set line=%%a
            set var=!var! !line!
          )
          echo The file contents are: %var%

          Since you are a so called batch guru , you can see
          one big variable, now containing all the lines of the file.

          Who wrote the code? Salmon Trout? Excellent.  Did you provide a copy to Animo, the original Poster?

          I never claimed to be a "Batch Guru."  I had not used batch until I logged on here several months ago.

          I'm 72. I don't work.

          Where does Ghost work as a Programmer? 

          Ghost, read post #8 by Geek above.




          gh0std0g74



            Apprentice

            Thanked: 37
            Re: how to asign dir result to the variable
            « Reply #11 on: October 30, 2009, 09:33:40 PM »
            Who wrote the code? Salmon Trout? Excellent.  Did you provide a copy to Animo, the original Poster?
            anyone who knows how to write batch will know its a "standard way" to concatenate lines to one variable. Why should i provide a copy to OP? you are ridiculous. whether i want to provide solutions is my own business.

            Quote
            I never claimed to be a "Batch Guru."  I had not used batch until I logged on here several months ago.
            that's why i have written "so called batch guru". which implicitly means i am saying you are not.

            Quote
            I'm 72. I don't work.
            that's why you have plenty of time trolling around.

            Quote
            Where does Ghost work as a Programmer? 
            what gives you the idea that i am a programmer? Does it mean i need to be a programmer in order to program?



            billrich

            • Guest
            Re: how to asign dir result to the variable
            « Reply #12 on: October 31, 2009, 12:01:28 PM »
            how could i  asign dir result to the variable
            this doesnot work :(

            set /a result=dir /b /o:n

            something like this does not work  either

            set /a result=`dir /b /o:n`


            C:\batextra>type vardir.bat

            Code: [Select]
            @echo off
            REM code by  unknown
            setlocal enabledelayedexpansion
            dir /b /o:n  >  dirvar.txt
            set result=
            for /f "delims=" %%a in (dirvar.txt) do (
              set line=%%a
              set result=!result! !line!
            )

            echo.
            echo The directory contents assigned to result variable: %result%

            OUTPUT:

            C:\batextra>vardir.bat

            The directory contents assigned to result variable:  .uvwrap_v301.swf .uvwrap_v3
            02.swf .uvwrap_v303.swf .uvwrap_v304.swf .uvwrap_v305.swf 2009.January.blanks 20
            09.January.dotfile 2line.bat 2pdfdos.bat A.bat anecho.bat ans.bat AUTOEXEC.BAT B
            .bat Batch1.bat batchname.bat batexe.bat batfile.bat bc.bat bcalc.bat beep.bat b
            est.bat bill0910.bat bill0911.bat bill72.bat bill72.txt Blues.wma brost.bat BSET
             Bver.bat caavsetupLog.txt caisslog.txt carbon.bat caret.bat caseyville.gif chan
            gename.bat chkqo.bat choice.bat christmas.txt clear.bat clock.bat CONFIG.SYS cop
            yfiles.bat copytxt.bat cosmic.bat count.txt countf.bat countword.bat dalete.bat
            data.csv data06.txt date.txt datefile.bat datename.bat daydir.bat db.txt del3sec
            .bat delnet.bat desk.bat dev.bat devstr.bat devtok.bat devtwo.bat devtxt.bat dev
            u.bat Dias.bat digit.bat dir dirtree.bat dirvar.txt disklog.txt Divide.bat Docum
            ents dostimer.bat drive.bat echprom.bat envvar.bat err.bat evaluate.bat evaluate
            .vbs expand.bat extractfolder.bat field1.bat fiels1.bat fifty.bat file.bat file.
            txt filelen.bat fileprop.bat filetovariable.bat finalstr.bat finalziplist.txt fi
            nddir.bat findstring.bat findxfiles.bat FlashSavingPluginSetup.exe fold.bat fold
            1 fold2 fold3 fold4 fold5 folder1 folder2 folder3 folder4 folder5 for.txt found.
            bat FP.vbs g.bat game.bat gee.bat generatebat.bat ghost.bat gkata.bat go.bat goo
            glemath.bat gotoline.bat h3.bat hello.bat helpme.bat helppetty.bat hi.bat his.ba
            t hist hist.txt hmath.c homewk.txt homework3.bat hope.bat htmldrive.bat inc.bat
            indent Instruct_for.txt jak.bat kaus.bat kay.bat Keeptime.bat lewbat.bat line.ba
            t list_of_program_files.txt list2a.txt listone.txt log log.txt longshot.doc lw.b
            at mac.bat macdir.bat main.bat map.bat map2.bat mapodd.bat match.bat math.bat ma
            th2.bat menu.bat Menuhope.bat MenuHope.txt mon.bat mon.txt monnum.txt mpgname.ba
            t MSDIR.bat msin.bat mwc.bat mydate.bat namedate.bat New Stories (Highway Blues)
            .wma newbat.bat newname.bat nomodname.bat noperiod.txt notime.bat null old.bat o
            ldfiles.bat orastr.bat outmenu.txt parsedate.bat particular.bat pdfdos.bat perio
            d.bat period.txt PID_446170_AFIQ3Q4_RMOLA_MMY_160x600.sw f PID_446170_AFIQ3Q4_RMO
            LA_MMY_160x600_Parent.swf pipe.txt playsong.bat pp.bat preloader.swf preloader01
            .swf preloader02.swf progressbar.bat prtbat.bat psexec.exe quote.bat renamejpg.b
            at renjpg.bat reno.bat replace.bat reploop.bat reveille.mp3 rmext.bat rmnum.bat
            rmnum2.bat runany.bat safexp.zip save screenclean.swf sendfile.bat session.txt s
            etvar.bat sfc SH_HISTO shakeit.gif show.bat show.txt shut.bat Sig.gif signal.txt
             signature signature.htm sigop.gif sleep.exe sortdir.bat sortdir.txt sound.swf s
            tartsig.bat str.bat str2.bat str3.txt strcat.bat striptime.bat strlen.bat succes
            s.bat sumnum.bat TAPS.mp3 task.txt taskkill.exe tasklist.exe Tata.jpg tehthe.bat
             temp.tmp temp1.txt tes3.bat tes4.bat test.bat test06.bat test07.bat test07.txt
            test10.bat test18.bat testextract.bat testfilnam.txt TestLog.csv testnum.bat tes
            tver19.bat testwhat.bat textsearch.bat timediff.bat timeinseconds.bat timest.bat
             tips.txt tooktime.bat try.bat try06252009.bat try07182009.bat try07302009.bat t
            wodate.bat uvwrap_v3.swf val.bat val20.bat valarie.bat vardir.bat vars.bat vartx
            t.bat vbprop.bat verp.bat volis.bat wait1.bat wait10.bat wait2.bat wait5.bat wai
            ttime.bat walkthetalk.swf wget.bat whatd.bat whatis.bat win.ini winder.bat windo
            wsupdateagent30-x86.exe wmp.bat writetofile.bat x.txt x3.bat xc.bat xcc.bat xfif
            ty.bat xinc.bat xlw.bat xmas.txt xplaysong.bat xsignature.htm xtry.bat xxsignatu
            re.htm xxtestfl.bat xxx.txt xxxmenu.bat xxxxx.bat xy.bat y y.htm yourfile.bat z.
            txt zeke.txt zip.bat zip2.bat ziplist.txt zzero.bat

            C:\batextra>