Generate many command-line instructions when found a single "word"(file input) in a text/bat file

I am writing a batch file(Say, Primary.bat) that generates a text file(actually .bat, say  Generator.bat) which collects all the text file names from a particular directory(folder) and writes in generator.bat.

The contains of Primary.bat is now -

@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
Rem Following command will write the names of all files in a text file
dir /b "C:\InputOutput\SourceFiles" > "C:\InputOutput\Generator.bat"

Open in new window



So, the contents of Generator.bat looks like -

input1.txt
input2.txt.
input3.txt
unitedstates1.txt
unitedkingdomsales.txt
...
...
majorregion100.txt

Open in new window



(1) Now I need to  add code in the script(Primary.bat) so that all the above lines in Generator.bat can be changed to the following commands -
 
converter.java    C:\InputOutput\SourceFiles\input1.txt     C:\InputOutput\OutFiles\input1.rtg
converter.java    C:\InputOutput\SourceFiles\input2.txt     C:\InputOutput\OutFiles\input2.rtg
converter.java    C:\InputOutput\SourceFiles\input3.txt     C:\InputOutput\OutFiles\input3.rtg
converter.java    C:\InputOutput\SourceFiles\unitedstates1.txt   C:\InputOutput\OutFiles\unitedstates1.rtg
converter.java    C:\InputOutput\SourceFiles\unitedkingdomsales.txt   C:\InputOutput\OutFiles\unitedkingdomsales.rtg
...                              ...
...                              ...
converter.java    C:\InputOutput\SourceFiles\majorregion100.txt   C:\InputOutput\OutFiles\majorregion100.rtg

Open in new window


(2) After having task 1 done I want to have Generator.bat some code so that on clicking on Generator.bat it executes all the command lines.

Please help with simple code and simple logic. It would be really good if you test code before before replying. Thanks in advance
Aryabhatta MAsked:
Who is Participating?
 
Bill PrewCommented:
Here is an updated script to handle (1) of your question.  I didn't understand (2), if you double click Gemerator.bat it will automatically execute the commands in it, so not sure what you were looking for?

@echo off
setlocal

rem Define paths to work with
set BaseDir=C:\InputOutput
set SourceFiles=%BaseDir%\SourceFiles
set OutFiles=%BaseDir%\OutFiles
set BuildFile=%BaseDir%\Generator.bat

rem delete output file if it exists
if exist "%BuildFile%" del "%BuildFile%"

Rem Following command will write the names of all files in a text file
for %%A in ("%SourceFiles%\*.*") do (
    echo converter.java "%%~A" "%OutFiles%\%%~nA.rtg">>"%BuildFile%"
)

Open in new window


»bp
1
 
BillDLCommented:
Two things to suggest and ask.

SETLOCAL=ENABLEDELAYEDEXPANSION

Please note that these are two completely different things.  One is not = to the other.  They would be written separately as:

@echo off
SetLocal EnableDelayedExpansion

It doesn't matter if they are lowercase, uppercase, or title case (as shown above)

You only need Delayed Expansion enabled if you are using variables that must be expanded only at the time they are called.  In Bill Prew's example batch file above the variables are SET at the start, and they are expanded later using %VariableName%.  They will always be expanded to the values that were set at the start.  In cases where variables will contain different values every time they are expanded, you would enable Delayed Expansion and would use !VariableName! instead to expand them at the time.  In your case you do not need to enable Delayed Expansion.

In your question about this in Stack Overflow you will see that somebody has used Delayed Expansion of variables:
https://stackoverflow.com/questions/48399721/batch-script-to-generate-multiple-command-line-instructions

SetLocal is used so that any variables you have set in the batch file only persist within the batch file and are cleared when the batch file ends.

Is converter.java definitely a program executable that runs from the command line?
What happens if you enter just one command for it in a batch file?:

converter.java "C:\InputOutput\SourceFiles\unitedkingdomsales.txt" "C:\InputOutput\OutFiles\unitedkingdomsales.rtg"
1
 
Aryabhatta MAuthor Commented:
Can you please explain the below lines word per word for my learning so that next time I dont ask the same thing and can write efficient loops -

for %%A in ("%SourceFiles%\*.*") do (
    echo converter.java "%%~A" "%OutFiles%\%%~nA.rtg">>"%BuildFile%"
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Aryabhatta MAuthor Commented:
I did not understand  what these two  does.

"%%~A"

"%%~nA"

Can anyone please explain with the example in our context
0
 
Aryabhatta MAuthor Commented:
Also, rather than clicking on the Primary.bat to run and hardcoaded  SourceFiles and OutFiles, i want to make it generic and runnable from interactively from command prompt.

So to produce Generate.bat I shroud run command in the cmd from folder where Primary.bat resides -

C:\InputOutput>Primary.bat     <folder path of source files>  <folder path of out files/some destination path>


How do I make this possible ?
0
 
BillDLCommented:
OK

At the start of the batch file the paths to your working folders were set as variable names:

You can change the path for the first line to whatever you want.
Wherever the variable %BaseDir% is used in the batch file after that point it will be expanded to the path C:\InputOutput.

set BaseDir=C:\InputOutput

So, %SourceFiles% will be expanded to C:\InputOutput\SourceFiles
%OutFiles% will be expanded to C:\InputOutput\SourceFiles\Outfiles
%BuildFile% will be expanded to C:\InputOutput\Generator.bat

set SourceFiles=%BaseDir%\SourceFiles
set OutFiles=%BaseDir%\OutFiles
set BuildFile=%BaseDir%\Generator.bat

for %%A in ("%SourceFiles%\*.*") do (
    echo converter.java "%%~A" "%OutFiles%\%%~nA.rtg">>"%BuildFile%"
)

The FOR command works as a LOOP.  In a batch file you must use %% and you could use any letter from A to Z in lowercase or uppercase.  In this case %%A is used at the start of the loop and is then used again later within the loop.

The part in the (brackets) is used first.  The FOR command will find the first file of any file extension in the folder: C:\InputOutput.
It will echo the line:
converter.java <filename1> C:\InputOutput\SourceFiles\Outfiles\<filename2>.rtg
to the file:  C:\InputOutput\Generator.bat

Once finished with the first file, the FOR loop will do the same with the second file found and will write:
converter.java <filename2> C:\InputOutput\SourceFiles\Outfiles\<filename2>.rtg
to the same *.bat file and append that line below the last.
this will continue until the last file in that folder has been found.

The %%A at the start of the loop will contain the full path and filename of each file that is being processed at that time.
This variable can be modified by adding a ~ in between the %% and the A.
The first modification %%~A is used to remove surrounding double-quotes.
The next modification of the variable %%~nA  will expand the full path of the file but will then remove the path and the file extension.  The "n" means expand to ONLY the file name.  The  .rtg  extension is added onto the file name.
The redirection symbol >> writes the whole line out to the output file.

>> means write to a file but append to existing contents.
> would write the line to the output file, but would overwrite any existing content.
1
 
Aryabhatta MAuthor Commented:
Absolutely excellent explanation BillDL, This helped hugely and I am sure will help any new developer who will come across this post. Long live our veterans !!!
0
 
Aryabhatta MAuthor Commented:
I have one more question-

(1) set BuildFile=%BaseDir%\Generator.bat  //we are creating generator.bat

(2) if exist "%BuildFile%" del "%BuildFile%"   // we are deleting generator.bat, but we wanted to delete any pre-existing file if any, so should not we write this line before (1)

(3)  If (2) exist where it is written by BillDL the how we are able to use it in -

echo converter.java "%%~A" "%OutFiles%\%%~nA.rtg">>"%BuildFile%"

because we just deleted BuildFile
0
 
BillDLCommented:
When you redirect what would be displayed on screen to a text file it will create a new file if it doesn't already exist.
If you use > it will overwrite all existing content.
If you use >> it will append the output after any existing content.

You cannot use the DEL (delete) command BEFORE the SET command because it would not understand what %BuildFile% was referring to.
1
 
Bill PrewCommented:
Some good explanations from BillDL while those of us in UTC-5 catch some sleep :).

One thing I will also mention, most of the "DOS" commands have help available, and a couple worth looking at are:

FOR /?
SET /?

Lots of good info in there, for example the following is taken from the FOR /? help display:

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

Open in new window


One other thing I'm not sure if was addressed is:
So to produce Generate.bat I shroud run command in the cmd from folder where Primary.bat resides -

C:\InputOutput>Primary.bat     <folder path of source files>  <folder path of out files/some destination path>

How do I make this possible ?
First, any BAT file can be executed from a command prompt just by entering it's name, so that already works.

As far as passing the folders as names, that's pretty easy too.  BAT files, and called subroutines in a BAT file, support parameter passing using the special variables %1, %2, %3, etc.  So in your example if you want to pass two parms on the command line when you run the BAT file, you can reference %1 and %2 in the BAT script to get the values from the command line.  So in our case, we can change the script a bit to not hard code the paths, but supply them from command line as follows.

@echo off
setlocal

rem Define paths to work with
set SourceFiles=%~1
set OutFiles=%~2
set BuildFile=Generator.bat

rem delete output file if it exists
if exist "%BuildFile%" del "%BuildFile%"

Rem Following command will write the names of all files in a text file
for %%A in ("%SourceFiles%\*.*") do (
    echo converter.java "%%~A" "%OutFiles%\%%~nA.rtg">>"%BuildFile%"
)

Open in new window


»bp
1
 
Aryabhatta MAuthor Commented:
Sorry I  am late to respond. I was actually able to take inputs from the command line parameters. But my heartiest thanks BillDL and Bill Prew who had always been of great help to newbies like me. My up-votes for all their replies.
0
 
Aryabhatta MAuthor Commented:
Thanks to Bill and Bill for their unending support.
0
 
Bill PrewCommented:
Welcome!


»bp
0
 
BillDLCommented:
Thank you Aryabhatta M.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.