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

Author Topic: New member, New DOS adventurer.  (Read 19570 times)

0 Members and 1 Guest are viewing this topic.

robertwig

    Topic Starter


    Rookie
  • The Dos and Don'ts of my new life
    • Experience: Beginner
    • OS: Windows 7
    New member, New DOS adventurer.
    « on: January 21, 2014, 05:27:58 PM »
    Good day folks;

    I'm new to this board and to DOS batch programming.
    I have been spending some time with DOS in hopes of gaining the ability to program some batch files for my home pc.
    I have had some success but could use some direction from those who have been at it for some time.

    I would appreciate knowing how to address the problem of "locating a specific directory across all the drives on my system and returning the path (drive:\directoryname) to a variable that I could pass on to other parts of my batch dos program.

    I have used the following to return most of what I need:

    dir /d MyOFFSetupFiles | findstr "\<Directory of\>"
    this returns the following string to the console:  Directory of C:\MyOFFSetupFiles

    What I need to do with this console output is strip of the "Directory of" portion and pass the "C:\MyOFFSetupFiles" on to a variable I can use further on in the batch run.

    I have looked at the FOR command, but the complexity is too big of a jump for me at my stage of programming. I just can't fathom how to implement it but I think it might be what I need. Is there a simple way to parse the portion I need into a variable?

    Any help would be appreciated. Thanks in advance for taking the time to look at this.

    Best Regards






    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: New member, New DOS adventurer.
    « Reply #1 on: January 21, 2014, 06:08:58 PM »
    Assuming I understood you correctly (I'm somewhat brain dead right now) but you want to set the output of ' dir /d MyOFFSetupFiles | findstr "\<Directory of\>" ' as the value of a variable.

    You were correct in your assumption that you need a For loop.  I'll post the code below and walk you through how it works.

    Assuming your command gives the output: "Directory of C:\MyOFFSetupFiles" without the quotes.
    Code: [Select]
    :: So first we must enable delayed expansion, so we can change and use the value of variables inside the for loop.
    setlocal EnableDelayedExpansion

    :: Now we start the first for loop.  This one will seperate the output of the command into two parts based on the possition of (:).
    :: This will allow us to isolate the path, (which will be stored as %%B for the duration of the loop.
    for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<Directory of\>"') do (

    :: Now to deal with the fact that the drive is in the other segment, we must seperate that segment of output further until we only
    :: have the drive letter.  To do this we use another for loop.  This time we seperate the string %%A (the first half of the original output)
    :: based on the possition of spaces, only holding onto the first three segments. They will be stored in %%G, %%H, %%I respectively.
    :: Now since the drive letter is found singularly in the 3rd segment, we move that into a variable until we can join it up with our
    :: path (stored in %%B)
    for /f "tokens=1-3 delims= " %%G in ("%%A") do (
    set "findPath_drv=%%I:"
    )

    :: To complete the operation, we combine the two variables into a single one, and empty our temporary variable used to store
    :: the drive letter
    set findPath=!findPath_drv!%%B
    set findPath_drv=
    )

    :: This is simply to allow us to test to see if the operation was completed successfully.
    echo %findpath%
    pause

    This is possible to do without using 'setlocal EnableDelayedExpansion' via this code:
    Code: [Select]
    for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<Directory of\>"') do (
    for /f "tokens=1-3 delims= " %%G in ("%%A") do (
    set "findPath=%%I:%%B"
    )
    )
    echo %findpath%
    pause
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    robertwig

      Topic Starter


      Rookie
    • The Dos and Don'ts of my new life
      • Experience: Beginner
      • OS: Windows 7
      Re: New member, New DOS adventurer.
      « Reply #2 on: January 21, 2014, 06:37:17 PM »
      Lemonilla;

      It's amazing that once you see an example it seems so clear. I am finding that with most of my exposure to DOS, the command are mind boggling until you see some examples. Could I hit on you for a recommendation of reading material that in your opinion really has some extensive examples that make use of the majority of the features of most commands and also shows how to link them via piping, FOR, and other methods.

      Just looking at the dos commands fields only gets me in the door and frustrates me trying to determine how to mix and match them.
      Very basic stuff comes easy but the more complex mix requires me to understand far more than I can get without a proper guide.

      I am very thankful for what you have provided to me and your description was extremely helpful.

      Thanks again and any suggested reading material would be grateful either  by PM or here.

      Best Regards;

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: New member, New DOS adventurer.
      « Reply #3 on: January 21, 2014, 07:01:32 PM »
      If you read the last section of the FOR /help.
      You might be surprised on how to do this a little simpler with the modifiers.

      Lemonilla



        Apprentice

      • "Too sweet"
      • Thanked: 70
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: New member, New DOS adventurer.
      « Reply #4 on: January 21, 2014, 07:20:48 PM »
      Lemonilla;

      It's amazing that once you see an example it seems so clear. I am finding that with most of my exposure to DOS, the command are mind boggling until you see some examples. Could I hit on you for a recommendation of reading material that in your opinion really has some extensive examples that make use of the majority of the features of most commands and also shows how to link them via piping, FOR, and other methods.

      Just looking at the dos commands fields only gets me in the door and frustrates me trying to determine how to mix and match them.
      Very basic stuff comes easy but the more complex mix requires me to understand far more than I can get without a proper guide.

      I am very thankful for what you have provided to me and your description was extremely helpful.

      Thanks again and any suggested reading material would be grateful either  by PM or here.

      Best Regards;

      I'd love to, but I don't really have much.  I learned all of my knowledge base from http://ss64.com/nt/ and from posting questions on the forum.  Mostly you just have to mess around and write a bunch of goofy stuff until you start to get the hang of it.

      If you read the last section of the FOR /help.
      You might be surprised on how to do this a little simpler with the modifiers.
      But that would require *gasp* reading!
      Noted, I'll look into it.



      On another note, I believe what you are looking for is %cd%, which passively holds the current directories path.
      Code: [Select]
      T:\lib>dir /d
       Volume in drive T is Testing
       Volume Serial Number is 6694-3ABE

       Directory of T:\lib

      [.]                 [Amy Tintera]       [Blake, Kendare]
      [..]                [Ari Harper]        [Tad Williams]
      $lib.bat            [Becca Fitzpatrick]
                     1 File(s)            836 bytes
                     7 Dir(s)   5,231,140,864 bytes free

      T:\lib>echo "%cd%"
      "T:\lib"
      ( I used quotes to help differentiate from the prompt [the default of which is "%cd%>" or $P$G in the 'prompt' command])
      « Last Edit: January 21, 2014, 07:38:39 PM by Lemonilla »
      Quote from: patio
      God Bless the DOS Helpers...
      Quote
      If it compiles, send the files.

      robertwig

        Topic Starter


        Rookie
      • The Dos and Don'ts of my new life
        • Experience: Beginner
        • OS: Windows 7
        Re: New member, New DOS adventurer.
        « Reply #5 on: January 21, 2014, 09:19:32 PM »
        Assuming I understood you correctly (I'm somewhat brain dead right now) but you want to set the output of ' dir /d MyOFFSetupFiles | findstr "\<Directory of\>" ' as the value of a variable.

        You were correct in your assumption that you need a For loop.  I'll post the code below and walk you through how it works.

        Assuming your command gives the output: "Directory of C:\MyOFFSetupFiles" without the quotes.
        Code: [Select]
        :: So first we must enable delayed expansion, so we can change and use the value of variables inside the for loop.
        setlocal EnableDelayedExpansion

        :: Now we start the first for loop.  This one will seperate the output of the command into two parts based on the possition of (:).
        :: This will allow us to isolate the path, (which will be stored as %%B for the duration of the loop.
        for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<Directory of\>"') do (

        :: Now to deal with the fact that the drive is in the other segment, we must seperate that segment of output further until we only
        :: have the drive letter.  To do this we use another for loop.  This time we seperate the string %%A (the first half of the original output)
        :: based on the possition of spaces, only holding onto the first three segments. They will be stored in %%G, %%H, %%I respectively.
        :: Now since the drive letter is found singularly in the 3rd segment, we move that into a variable until we can join it up with our
        :: path (stored in %%B)
        for /f "tokens=1-3 delims= " %%G in ("%%A") do (
        set "findPath_drv=%%I:"
        )

        :: To complete the operation, we combine the two variables into a single one, and empty our temporary variable used to store
        :: the drive letter
        set findPath=!findPath_drv!%%B
        set findPath_drv=
        )

        :: This is simply to allow us to test to see if the operation was completed successfully.
        echo %findpath%
        pause

        This is possible to do without using 'setlocal EnableDelayedExpansion' via this code:
        Code: [Select]
        for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<Directory of\>"') do (
        for /f "tokens=1-3 delims= " %%G in ("%%A") do (
        set "findPath=%%I:%%B"
        )
        )
        echo %findpath%
        pause

        Lemonilla;

        PLease ignore the following!! DUH!! I labelled my file with extension "txt" instead of "bat". I have got to get some sleep!! Sorry.
        Your c ode worked great. Now I have to build on it. Thanks again

        I tried your code but was surprised to get the response I did.  Any ideas of what I did wrong here? See below:

        Microsoft Windows [Version 6.0.6002]
        Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

        C:\>code: [select]
        'code:' is not recognized as an internal or external command,
        operable program or batch file.

        C:\>setlocal EnableDelayedExpansion

        C:\>for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\,D
        irectory of\>"') do (for /f "tokens=1-3 delims= " %%G in ("%%A") do (set "findpa
        th=%%I:%%B"))
        %%A was unexpected at this time.

        C:\>echo %findpath%
        %findpath%

        C:\>dir /d MyOFFSetupFiles ^| findstr "\,Directory of\>"
        The system cannot find the file specified.

        C:\>dir /d MyOFFSetupFiles | findstr "\
         Directory of C:\MyOFFSetupFiles

        C:\>setlocal EnableDelayedExpansion

        C:\>for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<D
        irectory of\>"') do (
        %%A was unexpected at this time.

        C:\>for /f "tokens=1-3 delims= " %%G in ("%%A") do (set "findPath=%%I:%%B"))
        %%G was unexpected at this time.

        C:\>echo %findpath%
        %findpath%

        C:\>pause
        « Last Edit: January 21, 2014, 09:39:35 PM by robertwig »

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: New member, New DOS adventurer.
        « Reply #6 on: January 21, 2014, 09:28:09 PM »
        C:\>code: [select]
        Surely you didn't copy and paste that into your batch file.

        robertwig

          Topic Starter


          Rookie
        • The Dos and Don'ts of my new life
          • Experience: Beginner
          • OS: Windows 7
          Re: New member, New DOS adventurer.
          « Reply #7 on: January 21, 2014, 09:42:07 PM »
          Yes! DUH, and as I said to Lemonilla, I labelled my file extension as "TXT" instead of "BAT". Now that I have made a fool of myself I shall slink out the back door!!!

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: New member, New DOS adventurer.
          « Reply #8 on: January 21, 2014, 09:49:21 PM »
          This should check every drive for the foldername and return the very first match found.

          Code: [Select]
          @echo off
          del "folderlist.txt" 2>nul
          for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
             if exist %%a:\ dir "%%a:\foldername" /ad /b /s >>"folderlist.txt"
          )
          if not exist "folderlist.txt" echo no match found & goto :EOF
              set /p var=<"folderlist.txt"
              echo "%var%"

          robertwig

            Topic Starter


            Rookie
          • The Dos and Don'ts of my new life
            • Experience: Beginner
            • OS: Windows 7
            Re: New member, New DOS adventurer.
            « Reply #9 on: January 21, 2014, 11:13:47 PM »
            Foxidrive;

            I like your code but I really don't want to create a file on disk. I would rather pass the info on to a variable that I can use to position to the found top level directory for further processing. All I want to see in the variable is the high level DRIVELETTER:\DIRECTORYNAME
            I was unable to find any reference to your use of the phrase: >>"folderlist.txt" and would like to substitute a variable here but I'm not sure how to proceed.

            Hope you can clarify this for me.

            Thanks for your response it was appreciated.


            Geek-9pm


              Mastermind
            • Geek After Dark
            • Thanked: 1026
              • Gekk9pm bnlog
            • Certifications: List
            • Computer: Specs
            • Experience: Expert
            • OS: Windows 10
            Re: New member, New DOS adventurer.
            « Reply #10 on: January 21, 2014, 11:36:13 PM »
            Quote
            (c d e f g h i j k l m n o p q r s t u v w x y z)
            Does not include drive A and B. Was that intentional?  ::)

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: New member, New DOS adventurer.
            « Reply #11 on: January 21, 2014, 11:40:59 PM »
            Does not include drive A and B. Was that intentional?  ::)

            Yes, not many people use floppies and USB floppy drives, which are the main things you'd find on A: and B:


            Foxidrive;

            I like your code but I really don't want to create a file on disk.

            Why not?  It's called a temporary file.
            Are you aware that Windows itself could not run without creating temporary files?

            It's really funny to hear people say they don't want a temporary file, without a good reason for not being able to use one.

            Lemonilla



              Apprentice

            • "Too sweet"
            • Thanked: 70
            • Computer: Specs
            • Experience: Experienced
            • OS: Windows 7
            Re: New member, New DOS adventurer.
            « Reply #12 on: January 22, 2014, 05:18:41 AM »
            Foxidrive;

            I like your code but I really don't want to create a file on disk. I would rather pass the info on to a variable that I can use to position to the found top level directory for further processing. All I want to see in the variable is the high level DRIVELETTER:\DIRECTORYNAME
            I was unable to find any reference to your use of the phrase: >>"folderlist.txt" and would like to substitute a variable here but I'm not sure how to proceed.

            Hope you can clarify this for me.

            Thanks for your response it was appreciated.

            It is a redirection method.  ">> DESTINATION" means that it will add the output to destination, normally a temporary file, which can be used later. "> DESTINATION" replaces destination with a new file who's contense is the output of the command. There are also &,&&,|,||.

            Because of the limitations on dos variables, temporary files are sometimes necessary.  You just have to remember to remove them at the end.
            Quote from: patio
            God Bless the DOS Helpers...
            Quote
            If it compiles, send the files.

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: New member, New DOS adventurer.
            « Reply #13 on: January 22, 2014, 06:18:08 AM »

            I was unable to find any reference to your use of the phrase: >>"folderlist.txt" and would like to substitute a variable here but I'm not sure how to proceed.
            The the first line of the temporary file is being put into the variables called VAR.  That is why you see the redirection in the SET /P command and the code then echos that variable to the screen to show you that it was set.

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: New member, New DOS adventurer.
            « Reply #14 on: January 22, 2014, 06:24:59 AM »
            After my tantrum, I whipped this up.   

            Code: [Select]
            @echo off
            for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
                if exist %%a:\ for /f "delims=" %%b in ('dir "%%a:\foldername" /ad /b /s') do set "var=%%b" & goto :done
            )
            :done

            It's just very annoying when people ask for a solution and you provide one, and they didn't say SORRY, I CAN"T USE ANY TEMPORARY FILES in the first place.