title: Need to combine all csv data into single file of nested folder using batch script
body: <p>I need to combine all csv files data into single csv file using batch script</p>
  <p>the folder structure is as follows</p>
  <p>C:\Users\username\Desktop\DLog\Export\20200727\AL-Comp_Outbound\20200726\ (multiple csv's present here)</p>
  <p>So under &quot;Export&quot; there are multiple folders like &quot;20200727...20200678&quot; and in those folders the common sub folder name is  AL-Comp_Outbound which further has a sub folder  &quot;20200728...20200728&quot; that contain csv files.</p>
  <p>I need to find the folder with name AL-Comp_Outbound navigate to it and then parse through all the sub folders that has csv files and combine it together.</p>
  <p>i tried to do but it with below query but it search for all the folders present in the directory, I need only need csv files from all folders having common name as &quot;AL-Comp_Outbound&quot;. Also need to remove repetition header</p>
  <pre><code>@echo off 
  
  SETLOCAL ENABLEDELAYEDEXPANSION
  PUSHD &quot;%~dp0&quot;
  
  SET &quot;SUMMARY_FILE=sumfile.csv&quot;
  DEL /F &quot;%SUMMARY_FILE%&quot; 2&gt;nul
  
  SET &quot;LINE_COUNT=1&quot;
  
  FOR /F &quot;usebackq delims=&quot; %%p IN (`dir c:\ /s /b /ad ^| find &quot;AL-Comp_Outbound&quot;`) DO (
  
  FOR /F &quot;tokens=*&quot; %%f IN ('DIR /S /B *.csv 2^&gt;nul') DO (
      FOR /F &quot;usebackq tokens=* eol=ÿ&quot; %%s IN (&quot;%%~f&quot;) DO (
          &gt;&gt;&quot;%SUMMARY_FILE%&quot; ECHO !LINE_COUNT!%%s
          SET /A LINE_COUNT+=1
      )
  )
  )
  
      POPD
  
      ENDLOCAL
  </code></pre>
  <p>Any help is truly appreciated !!!</p>
  <p>===============================================================</p>
  <p>Below is the code which i tried to get it work but it keeps on going to another folder  &quot;AL-Comp_NetworkShare&quot; to fetch CSV file while i only need csv files from sub directories in &quot;AL-Comp_Outbound&quot;</p>
  <pre><code>REM @echo off 
  
  SETLOCAL ENABLEDELAYEDEXPANSION
  PUSHD &quot;%~dp0&quot;
  
  SET &quot;SUMMARY_FILE=sumfile.csv&quot;
  DEL /F &quot;%SUMMARY_FILE%&quot; 2&gt;nul
  
  SET &quot;LINE_COUNT=1&quot;
  
  
  For /D %%G In (&quot;%UserProfile%\Desktop\DLPLog\Export\*&quot;) Do For /D %%H In (&quot;%%G\AL-Comp_Outbound\*&quot;)Do (PushD %%H,
  
  FOR /F &quot;tokens=*&quot; %%f IN ('DIR /S /B *.csv 2^&gt;nul') DO (
      FOR /F &quot;usebackq tokens=* eol=ÿ&quot; %%s IN (&quot;%%~f&quot;) DO (
          &gt;&gt;&quot;%SUMMARY_FILE%&quot; ECHO !LINE_COUNT!%%s
          SET /A LINE_COUNT+=1
      )
  )
  ,POPD)
  
  ENDLOCAL
  </code></pre>
  <p>==================================output=================</p>
  <pre><code>PS C:\Users\username\Desktop\DLPLog\Export&gt; .\me.bat
  
  C:\Users\username\Desktop\DLPLog\Export&gt;REM @echo off
  
  C:\Users\username\Desktop\DLPLog\Export&gt;SETLOCAL ENABLEDELAYEDEXPANSION
  
  C:\Users\username\Desktop\DLPLog\Export&gt;PUSHD &quot;C:\Users\username\Desktop\DLPLog\Export\&quot;
  
  C:\Users\username\Desktop\DLPLog\Export&gt;SET &quot;SUMMARY_FILE=sumfile.csv&quot;
  
  C:\Users\username\Desktop\DLPLog\Export&gt;DEL /F &quot;sumfile.csv&quot;  2&gt;nul
  
  C:\Users\username\Desktop\DLPLog\Export&gt;SET &quot;LINE_COUNT=1&quot;
  
  C:\Users\username\Desktop\DLPLog\Export&gt;For / %G In (&quot;C:\Users\username\Desktop\DLPLog\Export\*&quot;) Do For / %H In (&quot;%G\AL-Comp_Outbound\*&quot;) Do (
  PushD %H,
   FOR /F &quot;tokens=*&quot; %f IN ('DIR /S /B *.csv 2&gt;nul') DO (FOR /F &quot;usebackq tokens=* eol= &quot; %s IN (&quot;%~f&quot;) DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
   POPD
  )
  
  C:\Users\username\Desktop\DLPLog\Export&gt;For / %H In (&quot;C:\Users\username\Desktop\DLPLog\Export\20200722\AL-Comp_Outbound\*&quot;) Do (
  PushD %H,
   FOR /F &quot;tokens=*&quot; %f IN ('DIR /S /B *.csv 2&gt;nul') DO (FOR /F &quot;usebackq tokens=* eol= &quot; %s IN (&quot;%~f&quot;) DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
   POPD
  )
  
  C:\Users\username\Desktop\DLPLog\Export&gt;For / %H In (&quot;C:\Users\username\Desktop\DLPLog\Export\20200723\AL-Comp_Outbound\*&quot;) Do (
  PushD %H,
   FOR /F &quot;tokens=*&quot; %f IN ('DIR /S /B *.csv 2&gt;nul') DO (FOR /F &quot;usebackq tokens=* eol= &quot; %s IN (&quot;%~f&quot;) DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
   POPD
  )
  
  C:\Users\username\Desktop\DLPLog\Export&gt;For / %H In (&quot;C:\Users\username\Desktop\DLPLog\Export\20200724\AL-Comp_Outbound\*&quot;) Do (
  PushD %H,
   FOR /F &quot;tokens=*&quot; %f IN ('DIR /S /B *.csv 2&gt;nul') DO (FOR /F &quot;usebackq tokens=* eol= &quot; %s IN (&quot;%~f&quot;) DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
   POPD
  )
  
  C:\Users\username\Desktop\DLPLog\Export&gt;For / %H In (&quot;C:\Users\username\Desktop\DLPLog\Export\20200725\AL-Comp_Outbound\*&quot;) Do (
  PushD %H,
   FOR /F &quot;tokens=*&quot; %f IN ('DIR /S /B *.csv 2&gt;nul') DO (FOR /F &quot;usebackq tokens=* eol= &quot; %s IN (&quot;%~f&quot;) DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
   POPD
  )
  
  C:\Users\username\Desktop\DLPLog\Export&gt;(
  PushD C:\Users\username\Desktop\DLPLog\Export\20200725\AL-Comp_Outbound\20200715,
   FOR /F &quot;tokens=*&quot; %f IN ('DIR /S /B *.csv 2&gt;nul') DO (FOR /F &quot;usebackq tokens=* eol= &quot; %s IN (&quot;%~f&quot;) DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
   POPD
  )
  The system cannot find the path specified.
  
  C:\Users\username\Desktop\DLPLog\Export&gt;(FOR /F &quot;usebackq tokens=* eol= &quot; %s **IN (&quot;C:\Users\username\Desktop\DLPLog\Export\20200722\AL-Comp_NetworkShare\20200702\20200702_Ncomp-fjt03063.csv&quot;)** DO (
  ECHO !LINE_COUNT!%s 1&gt;&gt;&quot;sumfile.csv&quot;
   SET /A LINE_COUNT+=1
  ) )
  </code></pre>