Reputation: 294
I need to create a batch script to get a list of all subfolders (not recursive!) of the directories located in a specific given location, for which the directory names start with one of a few predefined values. The list should only contain absolute paths. I can't use Powershell or other additional tools.
@echo off
set "yourDir=C:\FileStore"
for %%b in ("AB*","FG*") do (
for /d %%a in ("%yourDir%\%%b") do (
echo %%~fa
)
)
This results in an empty list and doesn't work...
Upvotes: 0
Views: 59
Reputation: 34909
The for %%b
loop in your code searches for files matching the patterns AB*
and FG*
in the current directory.
You could do this:
@echo off
set "yourDir=C:\FileStore"
pushd "%yourDir%" && (
for /D %%a in ("AB*","FG*") do (
echo %%~fa
)
popd
)
You might alternatively write "%yourDir%\AB*","%yourDir%\FG*"
behind in
but using pushd
and popd
prevents you from having to redundantly specify the root directory.
Or you could do that:
@echo off
set "yourDir=C:\FileStore"
for %%b in ("AB","FG") do (
for /D %%a in ("%yourDir%\%%~b*") do (
echo %%~fa
)
)
Since there are no wildcards in the in
clause of the for %%b
loop any more, the items are returned untouched without searching the file system for matching files. The for /D %%a
loop now contains the wildcard, so it actually searches matching immediate sub-directories.
Upvotes: 1