Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:35:48+00:00 2026-05-31T17:35:48+00:00

I want to use a batch script to loop through folders and subfolders, and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:35:49+00:00Added an answer on May 31, 2026 at 5:35 pm

    This problem may be solved via this process:

    1- For each one of the top-level folders:
    2-   Initialize result with top-level folder name
    3-   For each one of the subfolders below it
    4-      If subfolder contains *.xml files: gather its name in result
    5-   Show the result
    

    This is the Batch file:

    @echo off
    setlocal EnableDelayedExpansion
    set catdir=%CD%\catalog\
    cd %catdir%
    for /D %%m in (*) do (
       set "result=%%m - "
       pushd "%%m"
       for /D %%n in (*) do (
          if exist "%%n\*.xml" (
             set "result=!result!%%n;"
          )
       )
       popd
       if not "!result!" == "%%m - " (
          echo !result:~0,-1!
       )
    )
    

    The :~0,-1! part in last echo command delete the semicolon of the last subfolder name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch script that I want to call from an MSBuild project,
I'm trying to loop through the arguments that I am passing to a batch
I have a transaction log file in CSV format that I want use to
Below is my stored procedure. I want use stored procedure select all row of
I want to use the MultipleLookupField control in a web page that will run
I have a batch script that eventually starts another batch file and waits for
I'm trying to write a batch script that errors if port 1099 is already
RyanWilcox had posted a script at here , that can use the following command
I want to use Powershell in order to call a batch file on remote
I have a perl script that runs a series of batch scripts for regression

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.