List all directories in a folder and pass those each to another script

Issue

I am working on a deduplication script and decided it would be easier to hand it a parent directory and let it work on each child folder, rather than doing each folder separately. I was assuming this was going to be simple, but I can’t seem to get this to work properly. This is roughly what I have now:

SET base_folder=%1

for /d %%d in ("%1\*") do (
    set current_dir=%%d
    -- here is where I want to call my script with current_dir as the %1 --
)

Solution

You’re don’t appear to be using base_folder, so there’s no need to set it.

You don’t need to set current_dir; just use %%d directly. If the script you want to use to process each directory is also a batch file, you’ll need to CALL it:

for /d %%d in ("%1\*") do (
    CALL myotherscript "%%d"
)

See SS64 on CALL.

Answered By – Jeff Zeitlin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published