assuming, there is only one .mdb
per folder and you don't want to process folders, where no .mdb
exists:
echo off
setlocal enabledelayedexpansion
(
for /r %%d in (*.mdb) do (
set first=yes
set d=%%~pd
for %%z in ("!d:\=.!") do set d=%%~xz
<nul set /p =!d:.=!,%%~nd,
for %%f in ("%%~pd\*.mpg") do (
if defined first (
echo %%~nf
set "first="bre
) else (
echo ,,%%~nf
)
)
echo(
)
)>output.csv
same code with explanation (under every line):
@echo off
setlocal enabledelayedexpansion
(
REM enclose the whole code in parantheses ...<1>
for /r %%d in (*.mdb) do (
REM for every found .mdb file in folder or subfolders
REM /r = recursiv (including subfolders)
set first=yes
REM just a flag to identify the first line
set d=%%~pd
REM get path of the file
for %%z in ("!d:\=.!") do set d=%%~xz
REM ugly code for getting the last element of the path
REM by replacing \ with . so it looks like a filename
REM %%~xz gets the "extension" of that fake-"file"
<nul set /p =!d:.=!,%%~nd,
REM using set /p trick to write a line without linefeed
REM write the last path-element (with the dot removed) <comma> name of the mdb-file <comma>
for %%f in ("%%~pd\*.mpg") do (
REM for every .mpg in the found folder
if defined first (
REM if it is the first line (where the folder and the .mdb has to be written too)
echo %%~nf
REM append the name of the .mpg to the already written <folder>,<mdb-name>,
set "first="bre
REM delete the first-line-flag
) else (
echo ,,%%~nf
REM write a complete line
)
)
echo(
REM write the awesome linefeed
)
REM <1>... to write it's complete output to a file
)>output.csv
for the setlocal delayedexpansion
see here.