0

I have a BATCH.BAT and when I run it I need to ask my how many folders I want to create:

echo How many folders you want? (enter below)
SET /P "ANSWER=" 

then I need to input number (that is not grater then 50 but bigger then 1) and when I hit enter, I need to create folders in this folder %~dp0..\batch\. e.g. if I enter number 12 this will be created:

%~dp0..\batch\
          |_____ 01
          |
          |_____ 02
          |
          |_____ 03
          |
          |_____ 04
          |
          |_____ 05
          |
          |_____ 06
          |
          |_____ 07
          |
          |_____ 08
          |
          |_____ 09
          |
          |_____ 10
          |
          |_____ 11
          |
          |_____ 12

and when folders are created I need to put this in every created folder:

IF EXIST "%~dp0..\batch\01\" (
ROBOCOPY "%~dp0..\scripts" "%~dp0..\batch\01" "script.1s" /Z /B
BREAK>"%~dp0..\batch\01\t.ini"
BREAK>"%~dp0..\batch\01\k.txt"
BREAK>"%~dp0..\batch\01\s.txt" )

IF EXIST "%~dp0..\batch\02\" (
ROBOCOPY "%~dp0..\scripts" "%~dp0..\batch\02" "script.1s" /Z /B
BREAK>"%~dp0..\batch\02\t.ini"
BREAK>"%~dp0..\batch\02\k.txt"
BREAK>"%~dp0..\batch\02\s.txt" )

IF EXIST "%~dp0..\batch\03\" (
...

how can I achieve this creating of folders based on input number ??

and also how can I avoid to have 49x IF EXIST "%~dp0..\batch\XY\" ( ??

EDIT: here is my atempt:

rem @echo off
setlocal enabledelayedexpansion

:0001
echo How many folders you want? (enter below)
SET /P "ANSWER=" 

set ANSWER="%%F"
IF  %ANSWER% LSS 2  GOTO :0001
IF  %ANSWER% GTR 50 GOTO :0001
SET batch="%~dp0..\batch\"
SET max=25
SET min=2

FOR /L %%F IN (1,%max%,%min%) DO (
    IF NOT EXIST "%batch%\0-%%F" ( 
    md "%batch%\0-%%F")
    )
  • What is your question????? – Eric F May 25 '18 at 18:59
  • @DavidPostill well not sure how much more can I explain what is already stated in the post! I've provided everything I have including my code and also I explained where I am stuck how can I achieve this creating of folders based on input number ?. also I am aware that superuser.com is not a free script/code writing service... I seek (any) help due to my issue - it doesnt need to be full-script-copy-paste solution/code. I FAIL to understand why this is "too broad" and what are my options now. – user902300 May 25 '18 at 20:53
  • Your options are explained in my first comment. You've only provided 2 lines of code and not made any attempt at the rest of your requirements. – DavidPostill May 25 '18 at 20:55
  • Hints: for - Conditionally perform a command several times, md - Make Directory - Creates a new folder, if - Conditionally perform a command. – DavidPostill May 25 '18 at 20:57
  • @DavidPostill edited... (now with my failed atempt) – user902300 May 25 '18 at 21:08
1

Your try has unsolved problems:

  • lookup the for /l syntax

  • numbers below 10 need a leading zero (solved by adding 100 and taking the last 2 places)

  • the variable %%F is only valid in the scope of the for command (same line/code block)

The following batch doesn't need delayed expansion due to placing the code in a called subroutine, passing the number as an argument.


:: Q:\Test\2018\05\26\SU_1325998.cmd
@Echo off
SET min=2
SET max=50

:0001
Set "ANSWER="
echo How many folders do you want? (enter below)
SET /P "ANSWER=" 
If not defined ANSWER Exit /B
IF %ANSWER% LSS %min% (Echo %ANSWER% is not enaugh min=%min%& GOTO :0001 )
IF %ANSWER% GTR %max% (Echo %ANSWER% is too much   max=%max%& GOTO :0001 )

FOR /L %%F IN (1,1,%ANSWER%) DO Call :Sub %%F
Echo Done
Pause
Goto :Eof

:Sub
Set /A "N=100 + %1"
SET "batch=%~dp0..\batch\%N:~-2%"
IF NOT EXIST "%batch%" md "%batch%" >NUL

:: IMO RoboCopy is overkill here
COPY "%~dp0..\scripts\script.1s" "%batch%" >NUL
for %%A in (t.ini k.txt s.txt) Do if not exist "%batch%\%%A" Break>"%batch%\%%A"

> SU_1325998.cmd
How many folders do you want? (enter below)
1
1 is not enaugh min=2
How many folders do you want? (enter below)
99
99 is too much   max=50
How many folders do you want? (enter below)
3

Done
Drücken Sie eine beliebige Taste . . .

> tree \ /F
├───batch
│   ├───01
│   │       k.txt
│   │       s.txt
│   │       script.1s
│   │       t.ini
│   │
│   ├───02
│   │       k.txt
│   │       s.txt
│   │       script.1s
│   │       t.ini
│   │
│   └───03
│           k.txt
│           s.txt
│           script.1s
│           t.ini
│
├───scripts
│       script.1s
│
└───test
        SU_1325998.cmd
  • 1
    now I understand (and learned) where I was lacking. danke – user902300 May 26 '18 at 13:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.