-2

So I am relatively new with creating batch files, and I would say I know a fair amount when it comes to programming but I am just trying to get a grasp on the syntax. So hopefully I don't butcher this explanation: I am trying to do a for loop through a directory to retrieve the names of folders, not the folder path but the name as a string. I believe that is relatively straight-forward, but I am wanting for every folder name retrieved - perform an action using that folder name string then move on to the next string. A simple for loop, but batch seems a little different. Any help or recommendations would be greatly appreciated. Thank you.

Edit - When I say action I mean a list of actions to perform. So a little more than just a simple echo.

Harry Manback
  • 41
  • 1
  • 6
  • `for /d %%a in (*) do ...` or recursive `for /d /r "S:\tart folder" %%a in (*) do ...` is the "official" way, but the `for /f ... in ('dir...') do ...` method (as in Joe's answer) has its advantages (especially if folders might be removed, renamed or added during the process) Use modifers to get parts of the FQFN (like `%%~nxa` for name/extension only - see `for /?` for more of them) – Stephan Jun 20 '22 at 15:14
  • Open a Command Prompt window, type `for /?`, press the `[ENTER]` key, then read the output. You could use the `FOR /D` option. However, my recommendation is that you instead open a Command Prompt window, type `dir /?`, press the `[ENTER]` key, and read the output. You could then use `DIR` with its `/A:D` and `/B` options wirhin a `FOR /F` loop. There are literally thousands of examples of both of those methods already posted throughout the pages tagged [[tag:batch-file]] within this site. Please use the seatch facility at the top of this page to locate and adapt them instead of duplicating. – Compo Jun 20 '22 at 15:14

1 Answers1

0
FOR /F "usebackq delims=" %%A IN (`DIR /B /A:d "C:\MyFolder"`) DO (
    ECHO %%A
)

DIR will list all the files within the specified directory, /B will make it only output names (instead of full paths), /A:d will make it only scan for folders.

As with any for loop, %A must be used instead of %%A if you're not using it in a script.

Styris
  • 133
  • 1
  • 7
  • More simply, remove the `usebackq` and replace the backticks with single quotes `'` – Magoo Jun 20 '22 at 15:11
  • So, how difficult would it be to instead of just echo every loop, I want to perform a series of actions using the string retrieved for each folder. – Harry Manback Jun 20 '22 at 15:18
  • That's what the code block is for (starting with `(` and ending with `)`. You can add more than just one code line there. Be aware of [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) where needed. – Stephan Jun 20 '22 at 15:24
  • @HarryManback, as I've already told you, "There are literally thousands of examples" "already posted throughout the pages tagged [[tag:batch-file]] within this site. Please use the search facility at the top of this page to locate and adapt them". – Compo Jun 20 '22 at 15:42