I want to use a batch script to loop through folders and subfolders, and return all folders that contain xml files in a single string. (I need to be able to send this as a parameter)
My folder structure is relatively simple, I have a ‘parent’ folder, containing subfolders, and each of these contain xml files. So folder structure looks as follows :
MasterFolder1 > Subfolder1 > file1.xml – file2.xml … more files
Subfolder2 > file1.xml – file2.xml … more files
… more subfolders
MasterFolder2 > Subfolder1 > file1.xml – file2.xml … more files
Subfolder2 > file1.xml – file2.xml … more files
… more subfolders
and so on
What I was already able to construct is the batch data that will loop through all the folders, and only cares about the ones with xml files. It looks as follows :
@echo off & setLocal enableDELAYedexpansion
set catdir=%CD%\catalog\%
cd %catdir%
FOR /f %%G in ('dir /ad/s/b') DO (
if exist %%G\*.xml (
for /f "tokens=1-6 delims=\/" %%i in ("%%G") do (
set model=%%m REM 'model' is the master folder name
set locale=%%n REM 'locale' is the sub folder name
echo %%m - %%n
)
)
)
pause
Which gives me an output like
Model1 – DE
Model1 – FR
Model1 – ES
Model2 – DE
Model2 – FI
Model2 – DK
and so on
Now, What I like to get is something like this :
Model1 – DE;FR;ES
Model2 – DE;FI;DK
and so on, so I could send my main folder as a single variable, and all subfolders for the main folder as a grouped variable to an application.
Hope I’m a bit clear here, I’m an absolute beginner to batch files.
This problem may be solved via this process:
This is the Batch file:
The
:~0,-1!part in last echo command delete the semicolon of the last subfolder name.