Command to run a bat script against all filename in a folder and segmented databases and merging output
<p>I have a random number (and with random name) of .txt files within a folder named 'seq' as:</p>

<pre><code>NP_4500.1.txt
NP_4568.1.txt
NP_45981.3.txt
XM_we679.txt
36498746.txt
</code></pre>

<p>in another folder named 'db', I made a database fragmented in 20 segments (due to my computational limitations) which are arranged as:</p>

<pre><code>hg.part-01.db
hg.part-02.db
hg.part-03.db
..
..
hg.part-20.db
</code></pre>

<p>now I want to run the following command in each  .txt file against each fragmented database and generate fragmented result, as for one .txt file (NP_4500.1.txt):</p>

<pre><code>script.exe ./seq/NP_4500.1.txt -db ./db/hg.part-01.db -out NP_4500.1_part-01.out
script.exe ./seq/NP_4500.1.txt -db ./db/hg.part-02.db -out NP_4500.1_part-02.out
script.exe ./seq/NP_4500.1.txt -db ./db/hg.part-03.db -out NP_4500.1_part-03.out
...
...
script.exe ./seq/NP_4500.1.txt -db ./db/hg.part-20.db -out NP_4500.1_part-20.out
</code></pre>

<p>After that, I want to merge the results in a single file as:</p>

<pre><code>join NP_4500.1_part-001.out NP_4500.1_part-002.out .. NP_4500.1_part-00200.out &gt; NP_4500.1.out
</code></pre>

<p>similarly for next .txt file:</p>

<pre><code>NP_4568.1.txt

...
</code></pre>

<p>Now, I can run a cmd script for each .txt file as:</p>

<pre><code>for %%F in ("*.txt") do script.exe ./seq/%%F .......
</code></pre>

<p>But my question is, how can I integrate this command with each of the fragmented database and merge the .out files to generate result for a single .txt file before proceeding to the next.</p>

<p>I am using windows 7 (32 bit machine). I can use cmd, perl or python script. Thanks for ur consideration.</p>

<pre class="lang-dos prettyprint-override"><code>@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*.txt" '
 ) DO (
 SET "join="
 FOR /L %%d IN (101,1,120) DO (
  SET /a segment=%%d
  SET "segment=!segment:~-2!"
  ECHO(script.exe %sourcedir%\%%a -db .\db\hg.part-!segment!.db -out %%~na_part-!segment!.out
  SET "join=!join! %%~na_part-!segment!.out"
 )
 ECHO(join !join! ^&gt;%%~na.out
)


GOTO :EOF
</code></pre>

<p>You would need to change the setting of <code>sourcedir</code> to suit your circumstances.</p>

<p>Note that in windows the directory-separator is <code>\</code> - '/` is a switch-indicator.</p>

<p>The outer <code>for</code> assigns each filename in turn to <code>%%a</code>.</p>

<p><code>%%d</code> is assigned 100..120 in turn. This is assigned to <code>segment</code> and then only the last 2 characters of <code>segment</code> are used.</p>

<p>The <code>script.exe</code> is then run for each segment, and the <code>join</code> string builds up each <code>.out</code> filename.</p>

<p>Finally, the <code>join</code> command is executed.</p>

<p>The required commands are merely <code>ECHO</code>ed for testing purposes. <strong>After you've verified that the commands are correct</strong>, delete the string <code>ECHO(</code> which appears before your command to actually execute the commands. You would also need to change the <code>^&gt;</code> in the final <code>echo</code> to <code>&gt;</code> - the caret escapes the special meaning of <code>&gt;</code> for the purpose of being <code>echo</code>ed.</p>

<p>The <code>%~na</code> Hieroglyph means the name part only of the string in <code>%%a</code></p>

python perl batch-file cmd