This is my first post in here. I'm quite noob in this field, so sorry if I ask any uber basic questions...
The case...
So, I need to create a batch file which asks for a project number and name, and creates a folder with the set number and name, and a set of specific sub folders inside. Something like this tree (names of Sub Folders are simplified and number of elements reduced)
number name
├───01.SF A
│ ├───01.SF A1
│ └───02.SF A2
├───02.SF B
│ ├───01.SF B1
│ ├───02.SF B2
│ └───03.SF B3
├───03.SF C
│ ├───01.SF C1
│ ├───02.SF C2
│ ├───03.SF C3
│ ├───04.SF C4
│ └───05.SF C5
├───04.SF D
└───05.SF E
├───01.SF E1
├───02.SF E2
├───03.SF E3
└───04.SF E4
Basically it will be used everytime a new project starts.
The code...
So, following @Pimp Juice IT's answer in this post I created this code for a batch file:
@ECHO Off
SET /P "NUM=Project number "
SET /P "NOM=Project name "
SET "PNUM=%NUM% "
SET RootDir=%userprofile%\Desktop%PNUM%%NOM%
SET SubA="01.SF A","02.SF B","03.SF C","04.SF D","05.SF E"
SET Sub1="01.SF A1","02.SF A2"
SET Sub2="01.SF B1","02.SF B2","03.SF B3"
SET Sub3="01.SF C1","02.SF C2","03.SF C3","04.SF C4","05.SF C5"
SET Sub5="01.SF E1","02.SF E2","03.SF E3","04.SF E4"
FOR %%A IN (%SubA%) DO IF NOT EXIST "%RootDir%%%~A" MD "%RootDir%%%~A"
FOR %%1 IN (%Sub1%) DO IF NOT EXIST "%RootDir%\01.SF A%%~1" MD "%RootDir%\01.SF A%%~1"
FOR %%2 IN (%Sub2%) DO IF NOT EXIST "%RootDir%\02.SF B%%~2" MD "%RootDir%\02.SF B%%~2"
FOR %%3 IN (%Sub3%) DO IF NOT EXIST "%RootDir%\03.SF C%%~3" MD "%RootDir%\03.SF C%%~3"
FOR %%5 IN (%Sub5%) DO IF NOT EXIST "%RootDir%\05.SF E%%~5" MD "%RootDir%\05.SF E%%~5"
EXIT
Which, for my surprise, worked like charm! (thanks Pimp Juice IT!)
the actual RootDiris for testing purposes, and SET "PNUM=%NUM% " is the way I came to add an space in between number and name :(
The idea is to not have to add a new line of MD... for every folder when the structure changes/growns with time.
But it seems to me, the way I scaled the example, turns in a very inefficient and static code... Specially having scalability in mind. And I was asking if it could be improved/simplified to a better and more parametrized code.
The Doubts...
In general, I'd like to parametrize and optimize the process, so it adjust automatically in the event of a folder changing its name, or a new folder appears in between existing ones.
Maybe the code have to be rethinked from the gound!
#1
At some point in the future, the structure is redisigned and a 2nd level folder changes the name (from that point on).
i.e: 01.SF A changes to 01.OTHERNAME A.
It is changed in
SET SubA="01.SF A","02.SF B","03.SF C","04.SF D","05.SF E"
for
SET SubA="01.OTHERNAME A","02.SF B","03.SF C","04.SF D","05.SF E"
But then then needs to be changed in every
"%RootDir%\01.SF A\%%~1"
to
"%RootDir%\01.OTHERNAME A\%%~1"
by hand
Is there a way to parametrize this? For instance refer to the second instance of a set value? like %RootDir%\second value of SubA\%%~1"
#2
In the case the structure changes and a folder is no longer needed, or a new one needs to be added in between in the example structure (from that point on), the numbering of the folders needs to be updated by hand.
i.e: 02.SF C2will be no more in the structure, then
03.SF C3 04.SF C4 and 05.SF C5
have to change to
02.SF C3 03.SF C4 and 04.SF C5
An currently need to be done by hand
Is there a way to automate the numbering increment in the folder's name? So when a new folder appears, the numbering ajusts atumatically
#3
Can a space be added to the filename other than declaring a new variable with this SET "PNUM=%NUM% "
#4
Can the whole process be made in a nested FORLike Pimp Juice IT did in the example? (I've tried to think how and my brain kind of blue screened)
#5
And lastly, is this correct for a batch file? Or it would be more suited for a powershell script?
Thank you so much for your time and patience!