-1

I have a few folders and they contain files with extension .mdb and .mpg (and more). I need a csv file that outputs the folder name (1st column), .mdb file name (2nd column) and .mpg file name (third column). .mpg is multiple in count

So the output would be something like:

output.csv

    A        B       C
    folder1 abc     xyz
                    hgf
                    pqr

    folder2 lala    didi
                    loc

A,B and C being the columns. Also, the line break would be awesome to get if possible. Using Windows 7.

I'm doing

    for /f "tokens=*" %%g in ('dir /b /a:d "*"') do (
    forfiles /m *.mdb /c "cmd /c echo @fname>>output.csv"
    )
Gaurav Goenka
  • 152
  • 12
  • If you're not programming yourself then "please do it for me" questions should be asked on superuser.com. – wOxxOm Jul 18 '15 at 21:43
  • I've always taken SuperUser to be more of a "how would I...?" site. If he wants it done for him, I'd ask on an actual programming forum that specializes in batch, like SS64. – SomethingDark Jul 18 '15 at 23:50
  • I did not want it done for me. I had tried it for four days trying to understand batch programming and for looping in it. This is what I tried `for /d %%D in (cd fol*) do( for /f %%F in (*.mdb) do echo %%~nF)>>output.csv` but its looking for the .mdb file in the current directory without traversing inside the directory. Couldn't find my way around this issue, so posted the question here. – Gaurav Goenka Jul 19 '15 at 07:19

1 Answers1

0

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.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • In column1 its giving output like \abc\sample - 14.07.2015\fname\ instead of just fname. Column2 is correct. Column 3 has sample and - in different lines. (the sample - 14.07.2015 has spaces in the name, if it matters, not sure). It should be .mpg files and not the current directory name. – Gaurav Goenka Jul 19 '15 at 12:24
  • Yes this works. Thanks so much. However, two things 1) would it be possible for you to explain this a little? I am just trying to understand batch programming. Its fine if you can't manage that now. 2) Its creating the output.csv in every subfolder which is not desired. – Gaurav Goenka Jul 19 '15 at 13:22
  • 1) I'll explain that tomorrow. 2) in every folder? It creates only ONE csv. You can give a path with it: `>"c:\my path\output.csv"` – Stephan Jul 19 '15 at 13:28
  • Yeah deleting the output.csv in all folders solved that. I think that may have been created by the last script. Not sure. Thanks a lot for the answer. :) – Gaurav Goenka Jul 19 '15 at 13:35
  • added explanation of code – Stephan Jul 20 '15 at 06:36
  • Thanks for the explanation. Helps a lot. Not sure why the question is being downvoted :( – Gaurav Goenka Jul 21 '15 at 10:20
  • I think, it was downvoted because the first two versions didn't show any effort, so it looked like a "do-my-work-for-me-question". The downvoter doesn't get a message when you edit your question, so he has no chance to undo his vote. Don't [take it personally](http://meta.stackoverflow.com/a/253230/2152082), it's just the way, this site tries to differ between useful and bad questions (or answers). ([more info](http://stackoverflow.com/help) – Stephan Jul 21 '15 at 13:26