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

Author Topic: Using a for loop to make unlimited folders  (Read 22727 times)

0 Members and 1 Guest are viewing this topic.

BatchFileCommand

    Topic Starter


    Hopeful
  • Thanked: 1
    Using a for loop to make unlimited folders
    « on: February 14, 2009, 07:18:31 PM »
    This is my program which makes folders. The user just types in how many folders he wants to make and he can make that many.


    Code: [Select]
    @echo off
    title Folder Maker
    color 0a

    cls
    set /p folder=How many folders would you like to make:
    for /l %%a in (1,1,%folder%) do (
    cls
    set /p foldername=What would you like to name folder #%%a:
    mkdir "%foldername%"
    set foldername=
    )

    msg * All folders created!

    But this throws back an error message saying "The system could not find the enviroment option that was entered"  . Any way to fix this ?
    οτη άβγαλτος μεταφ βαθμολογία

    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: Using a for loop to make unlimited folders
    « Reply #1 on: February 14, 2009, 07:22:11 PM »
    QuickBasic, Freebasic, whatev.
    Code: [Select]
    Dim NumFolders as Long
    Dim CurrFolder as String,I as long
    Const FolderName="FOLDER"
    numfolders=val(COMMAND$)
    chDrive "C:"
    chDir "\"
    For I = 1 to numfolders
        mkDir CurrDIR$ + "FOLDER"
        chDir CURRDIR$ + "FOLDER"

    next I



    Sorry, couldn't resist  ;D



    where does it throw the error? Could you place trace echos in the batch?
    I was trying to dereference Null Pointers before it was cool.

    BatchFileCommand

      Topic Starter


      Hopeful
    • Thanked: 1
      Re: Using a for loop to make unlimited folders
      « Reply #2 on: February 14, 2009, 07:44:08 PM »
      After I name the folder to make it throws the error message.
      οτη άβγαλτος μεταφ βαθμολογία

      computeruler



        Egghead

        Thanked: 63
        • Yes
        • Yes
      • Experience: Experienced
      • OS: Mac OS
      Re: Using a for loop to make unlimited folders
      « Reply #3 on: February 14, 2009, 07:45:51 PM »
      And why would you need something like this?  More computer pranks?

      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: Using a for loop to make unlimited folders
      « Reply #4 on: February 14, 2009, 08:00:33 PM »
      maybe he wants to see how deep it will go?
      I was trying to dereference Null Pointers before it was cool.

      BatchFileCommand

        Topic Starter


        Hopeful
      • Thanked: 1
        Re: Using a for loop to make unlimited folders
        « Reply #5 on: February 14, 2009, 08:25:38 PM »
        And why would you need something like this?  More computer pranks?

        This is quite far from computer pranks. Think before you speak. This is so you don't have to spend
        you time making a menu to ask how many folders you want to make.
        οτη άβγαλτος μεταφ βαθμολογία

        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: Using a for loop to make unlimited folders
        « Reply #6 on: February 14, 2009, 08:26:36 PM »
        And why would you need something like this?  More computer pranks?

        This is quite far from computer pranks. Think before you speak. This is so you don't have to spend
        you time making a menu to ask how many folders you want to make.


        of which the practical applications are?
        I was trying to dereference Null Pointers before it was cool.

        BatchFileCommand

          Topic Starter


          Hopeful
        • Thanked: 1
          Re: Using a for loop to make unlimited folders
          « Reply #7 on: February 14, 2009, 08:35:41 PM »
          Quote
          of which the practical applications are?

          I don't quite get what you mean there. But, back to the question. Is there a way to fix it so it won't throw out that error message and make the folders.
          οτη άβγαλτος μεταφ βαθμολογία

          GuruGary



            Adviser
            Re: Using a for loop to make unlimited folders
            « Reply #8 on: February 14, 2009, 09:05:33 PM »
            When you use environment variables that were set inside a FOR loop, you typically need to use the "setlocal enabledelayedexpansion" and then access them with ! instead of %.  Here is your script with those 2 changes:

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            title Folder Maker
            color 0a

            cls
            set /p folder=How many folders would you like to make:
            for /l %%a in (1,1,%folder%) do (
            cls
            set /p foldername=What would you like to name folder #%%a:
            mkdir "!foldername!"
            set foldername=
            )

            msg * All folders created!

            BatchFileCommand

              Topic Starter


              Hopeful
            • Thanked: 1
              Re: Using a for loop to make unlimited folders
              « Reply #9 on: February 15, 2009, 08:23:11 AM »
              Thanks, that worked.
              οτη άβγαλτος μεταφ βαθμολογία

              computeruler



                Egghead

                Thanked: 63
                • Yes
                • Yes
              • Experience: Experienced
              • OS: Mac OS
              Re: Using a for loop to make unlimited folders
              « Reply #10 on: February 15, 2009, 10:43:30 AM »
              BC ment why would you need to make all those folders

              BatchFileCommand

                Topic Starter


                Hopeful
              • Thanked: 1
                Re: Using a for loop to make unlimited folders
                « Reply #11 on: February 15, 2009, 12:23:24 PM »
                It's not about how many folders I can make. It's that I don't want to make a big menu asking how many folders I want to make.
                οτη άβγαλτος μεταφ βαθμολογία

                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: Using a for loop to make unlimited folders
                « Reply #12 on: February 15, 2009, 12:27:14 PM »
                It's not about how many folders I can make. It's that I don't want to make a big menu asking how many folders I want to make.


                why would you want a menu that asks you to create folders when it's far easier to do so using md and cd commands?
                I was trying to dereference Null Pointers before it was cool.

                computeruler



                  Egghead

                  Thanked: 63
                  • Yes
                  • Yes
                • Experience: Experienced
                • OS: Mac OS
                Re: Using a for loop to make unlimited folders
                « Reply #13 on: February 15, 2009, 01:20:15 PM »
                And I still want to know why you need to make so many folders

                Dias de verano

                • Guest
                Re: Using a for loop to make unlimited folders
                « Reply #14 on: February 15, 2009, 01:20:41 PM »
                why would you want a menu that asks you to create folders when it's far easier to do so using md and cd commands?

                Because beginning batch programmers love to create 150 line batch files with 56 labels that do the same thing as typing "DIR", maybe?