J.Carter
J.Carter

Reputation: 331

No output in cmd script to merge batch file contents

In a windows cmd I am running an exe command like:

for %F in ("*.fa") do  "../program.exe" -input %F -output %F.fasta| type %F %F.fasta > %F.txt

Here, say in the current folder, there are three input files as:

x.fa
y.fa
z.fa

The file content of x.fa is:

hi

the file content of auto-generated x.fasta is:

all

What I want is to generate a final .txt file (here x.txt) that will contain:

hi
all

but although I get the .fasta file (filecontent ->all), in the final .txt file I only get the content of .fa file (filecontent ->hi).

What's wrong in my script?

Upvotes: 0

Views: 189

Answers (2)

MC ND
MC ND

Reputation: 70923

When you use | you are creating a pipe, and the commands on both sides of the pipe are executed concurrently, with the standard output stream of the command in the left side piped into the standard input stream of the command in the right side.

So, you start to execute the type before the program.exe ends, so the .fasta file has not been generated and type can not read its contents.

Just concatenate the commands with & to execute the type after program.exe ends.

                                                               v
for %F in (*.fa) do  ..\program.exe -input %F -output %F.fasta & type %F %F.fasta > %F.txt
                                                               ^

Upvotes: 1

cyberbrain
cyberbrain

Reputation: 5075

The %F in your code always expands to the full filename: x.fa, y.fa or z.fa

So %F.fasta will be expanded to x.fa.fasta, y.fa.fasta, z.fa.fasta

and %F.txt will be expanded to x.fa.txt and so on.

To get the filename without extension you should use %~dpnF, for example %~dpnF.txt or %~dpnF.fasta

In addition you should use the copy command to concatenate files:

copy %F + %~dpnF.fasta %dpnF.txt

The full script would look like:

for %F in ("*.fa") do  "../program.exe" -input %F -output %~dpnF.fasta| copy %F + %~dpnF.fasta %dpnF.txt

You will find the full explanation of the for command variable expansions at https://technet.microsoft.com/en-us/library/cc754900.aspx#Anchor_2

PS.: please take care that the name of variables inside a script file (.cmd or .bat) have to be preceeded by %% instead of %

Upvotes: 1

Related Questions