How do I create a batch file to create many layers of folders from 2 different variable lists?

StackOverflow https://stackoverflow.com/questions/20714716

  •  20-09-2022
  •  | 
  •  

Question

I have ZERO experience with this. Any and all help would be greatly appreciated.

Right now I have it so I can see the list of folders I am creating and edit which ones I want to create. I want to be able to create Fund Family folders and in each of those Fund Family folders have various entities and then within each entity create folders. I think this requires referencing many different outputlists. So like at the top the fundfamily list is fine...i think i need serval outpistlits for each funfamily that will contain the entities so entitylist01...entitylist02... if that makes sense

There are 32 fund families with around 600 entities in total. The entity list changes for each fund family.

this what I have so far...

mkdir C:\2013\"2 - November Projections"
pause
dir Z:\2013\"2 - November Projections"\"*" /b /a:d >fundfamily.txt
start C:\Windows\System32\notepad.exe "fundfamily.txt"
pause
for /f "tokens=*" %%a in (fundfamily.txt) do mkdir C:\2013\"2 - November Projections"\"%%~a"
pause
for /f "tokens=*" %%a in (fundfamily.txt) do dir Z:\2013\"2 - November Projections"\"%%~a"\"*" /b /a:d >entitylist.txt
start C:\Windows\System32\notepad.exe "entitylist.txt"
pause
for /f "tokens=*" %%a in (entitylist.txt) do mkdir C:\2013\"2 - November Projections"\"*"\"%%~a"\2013\"November Estimates"
pause
Was it helpful?

Solution

@echo off
setlocal
SET "sourcedrive=Z"
SET "destdrive=C"
SET /a year=2013
SET "projections=2 - November Projections"
SET "Estimates=November Estimates"
DIR /b /ad "%sourcedrive%:\%year%\%projections%" >fundfamily.txt
start /wait "Fund Family" C:\Windows\System32\notepad.exe "fundfamily.txt"
FOR /f "delims=" %%a IN (fundfamily.txt) DO (
 ECHO MD "%destdrive%:\%year%\%projections%\%%~a"
 DIR /b /ad "%sourcedrive%:\%year%\%projections%\%%~a" >entitylist.txt
 start /wait "%%~a entities" C:\Windows\System32\notepad.exe "entitylist.txt"
 FOR /f "delims=" %%b IN (entitylist.txt) DO ECHO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
)

It's incredibly difficult to solve a problem stated as ""my code doesn't do what I want it to do" without an indication of sample data or desired outcome.

For instance, you could provide a partial listing such as

fundfamily.txt

Jones
Brown
de la Vedova
Smith

entitylist.txt

Stock - Confusion Industries
Sweatshop Industrial Inc.
Royalties - Useless Invention
123, Slum St., Gloomsville

and a note like the entity list changes for each fundfamily - or renaims the same, or whatever. You can Edit this data into your question - 'tis what the Edit link under the tags is used for.

As for the above code - I've assumed that the structure is as I have indicated. I would suggest that it would be quite an onerous task if you have hundreds of fundfamily entries, but we have no further data to go on...so it's "air" code - untried for absence of available data.

Note that the first quoted string in the start/wait line becomes the window title. If you don't want a window title, use an empty string ""

I've left C:\Windows\System32\notepad.exe in place, but it's quite likely that simply notepad would suffice, or even that it could be omitted entirely. The /wait is the magic - this waits until the executable is finished before the batch proceeds to the next step.

Also note that the MD commands (MD and mkdir are synonyms) are merely echoed - so that you may test without actually changing anything. You'd need to remove the echo keyword before the md to actually create the directories.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top