# Solved: batch file to read and run input file



## maa3 (Jan 12, 2013)

Hi, just joined the forum. I am new to programming. Could someone please help me write a batch file that will run an executable file A.exe say 100 times, each time using an input data file case1.dat. The input data also varies by picking up coresponding material properties from concrete.dat and steel.dat.
Sample input file is:
Input.dat
Element type
1,1,1,0,0,0
Concrete props
1,5.2,4110.0,0.2
Steel props
1,68.9,103.6,33
Output
1,2,3
end
Typical concrete.dat and steel.dat files may have say 100 corresponding article lines each, to be placed specifically in say lines 4 and 6 of input.dat for each run of A.exe.
I would be grateful if someone could please help me with this batch file using dos. I do not know how to program in visual basic so hope to get it done in dos.
Typical concrete.dat file;
1,5.2,4110.0,0.2
1,5.0,4125.0,0.23
1,3.5,3298.0,0.15
etc up to 100 lines and
that of steel.dat file:
1,68.9,103.6,33.0
1,56.4,100.2,45.0
1.2,89.1,101.1,40.0
etc up to 100 lines. 
Thanks and hope to get help.


----------



## Squashman (Apr 4, 2003)

I am not understanding how this all links together. You have 3 text files. What are you expecting the output to be?
What is the real name of your executable? That may help us as well.


----------



## foxidrive (Oct 20, 2012)

It seems to me as though the task is to take one data file "case1.dat"
and in lines 4 and 6 he wants to replace each line from two other files.

So the first run takes line 1 from steel and concrete files and puts them in lines 4 and 6 , the 2nd run takes line 2 from steel and concrete files and puts them in lines 4 and 6, and so on.


----------



## maa3 (Jan 12, 2013)

That's exactly what I need to do. The executable is a fortran program which has been compiled to analyse buildings.


----------



## foxidrive (Oct 20, 2012)

This is untested.

a.exe should be able to take casetmp.dat as an argument.
It uses GnuSED.
The two lines that are being replaced must be unique in the case1.dat and are also case sensitive.

If it has issues then reply here.

EDITED: to fix a syntax error


```
@echo off
for /L %%a in (1,1,100) do call :next %%a
echo done
pause
goto :EOF

:next
for /f "delims=" %%b in ('sed %1!d concrete.dat') do set "four=%%b"
for /f "delims=" %%c in ('sed %1!d steel.dat') do set "six=%%c"
sed -e "s/^Concrete props$/%four%/" -e "s/^Steel props$/%six%/" case1.dat >casetmp.dat
a.exe casetmp.dat
del casetmp.dat
```


----------



## maa3 (Jan 12, 2013)

foxidrive said:


> This is untested.
> 
> a.exe should be able to take casetmp.dat as an argument.
> It uses GnuSED.
> ...


Thanks so much. I tried your suggestion and appears to be working to an extent. I tried 5 iterations and the program run 5 times. However, I realised the the same output file was obtained because an output file CYC1.dat in the input file remained the same. So I tried to introduce another statement to read in "CYC1.dat, CYC2.dat etc' to be inserted on line 88 but still got only CYC1.dat. I am not sure of the syntax for reading the string characters in this case.
The following message however appeared after each run for all 'sed' statements:
"[ 'sed' is not recognised as an internal or external command operable program or batch file.]"

The batch file I used is shown below and hope you could help me edit, thanks.

@echo off
for /L %%a in (1,1,5) do call :next %%a
echo done
pause
goto :EOF
:next
for /f "delims=" %%b in ('sed %1!d concrete.dat') do set "twenty-one=%%b"
for /f "delims=" %%c in ('sed %1!d steel.dat') do set "twenty-three=%%c"
for /f "delims=" %%d in ('sed %1!d output.dat') do set "eighty-eight=%%d"
sed -e "s/^CONCRETE PROPERTIES$/%twenty-one%/" -e "s/^STEEL PROPERTIES$/%twenty-three%/" -e "s/%eighty-eight%/" case1.dat >casetmp.dat
idarc2d_6.0.exe casetmp.dat
del casetmp.dat


----------



## foxidrive (Oct 20, 2012)

I'm not sure that you follow the logic flow - do you have a file called output.dat

What is on line 88 in case1.dat?

As for sed, you have to download GnuSED for Windows. It is a free tool.


----------



## maa3 (Jan 12, 2013)

Thanks. Didn't know about Gnused so will find it. 
Line 88 contains only the file name CYC1.dat which is being read from output.dat. So CYC.dat is also numbered up to 100 and stored in output.dat. Once those file names are placed on line 88 of case1.dat, Idarc.exe will have a means of producing that file to store results of say the 48th run as CYC48.DAT.


----------



## maa3 (Jan 12, 2013)

Thanks. Didn't know about Gnused so will find it. 
Line 88 contains only the file name CYC1.dat which is being read from output.dat. So CYC.dat is also numbered up to 100 and stored in output.dat. Once those file names are read and placed on line 88 of case1.dat, Idarc.exe will have a means of producing that file to store results of say the 48th run as CYC48.DAT.


----------



## foxidrive (Oct 20, 2012)

Try this: where "CYC1.dat" is case sensitive too, and should exist on line 88 of case1.dat file.


```
@echo off
for /L %%a in (1,1,5) do call :next %%a
echo done
pause
goto :EOF
:next
for /f "delims=" %%b in ('sed %1!d concrete.dat') do set "twenty-one=%%b"
for /f "delims=" %%c in ('sed %1!d steel.dat') do set "twenty-three=%%c"
for /f "delims=" %%d in ('sed %1!d output.dat') do set "eighty-eight=%%d"
sed -e "s/^CONCRETE PROPERTIES$/%twenty-one%/" -e "s/^STEEL  PROPERTIES$/%twenty-three%/" -e "s/^CYC1.dat$/%eighty-eight%/" case1.dat  >casetmp.dat
idarc2d_6.0.exe casetmp.dat
del casetmp.dat
```


----------



## maa3 (Jan 12, 2013)

Sorry to be a bother. I got the GNUSED tool and installed but doesn't seem to work on Windows 7. It is in my start up but cannot get to open it as an executable file. I have used several hours reading about it today but not sure whether GNUSED windows requires a path to be used in the set up other than the program files directory. I downloaded sed_4.2.1-setup for the installation. Can anyone please help me with this? I am entirely new to this area.


----------



## foxidrive (Oct 20, 2012)

It is a command line program and just has to be available on the path, or replace the lines with sed in them to the correct path and filename, like "c:\program files\gnused\bin\sed.exe"

One way to put it on the path is to copy the sed.exe and any support DLL files to c:\windows\ and then that should be on the path. Open a cmd prompt and type SED and you should get help screen messages if all is well.


----------



## maa3 (Jan 12, 2013)

I tried and got the help messages alright. However when I tried the command line with the script input, the following message appeared:
sed: -e expression #1 char 6: unterminated 's' command


----------



## foxidrive (Oct 20, 2012)

Can you copy and paste the script again? I tested it in a limited fashion and it seems to work fine here.


----------



## maa3 (Jan 12, 2013)

foxidrive said:


> Can you copy and paste the script again? I tested it in a limited fashion and it seems to work fine here.


 I copied and pasted script again but still same result. Just in case I am on the wrong path,please check the command I used:
C:\Program Files\GnuWin32\bin\sed script final.txt


----------



## foxidrive (Oct 20, 2012)

Thanks for the info. Your sed command is wrong.

Try this script:


```
@echo off
for /L %%a in (1,1,5) do call :next %%a
echo done
pause
goto :EOF
:next
for /f "delims=" %%b in ('"C:\Program Files\GnuWin32\bin\sed" %1!d concrete.dat') do set "twenty-one=%%b"
for /f "delims=" %%c in ('"C:\Program Files\GnuWin32\bin\sed" %1!d steel.dat') do set "twenty-three=%%c"
for /f "delims=" %%d in ('"C:\Program Files\GnuWin32\bin\sed" %1!d output.dat') do set "eighty-eight=%%d"
"C:\Program Files\GnuWin32\bin\sed" -e "s/^CONCRETE PROPERTIES$/%twenty-one%/" -e "s/^STEEL  PROPERTIES$/%twenty-three%/" -e "s/^CYC1.dat$/%eighty-eight%/" case1.dat  >casetmp.dat
idarc2d_6.0.exe casetmp.dat
del casetmp.dat
```


----------



## maa3 (Jan 12, 2013)

Tried new script but still gives me previous error message. Not sure of what I am doing wrong. I already have concrete.dat, steel.dat, input.dat and idarc2d_6.0.exe files all in the bin directory.


----------



## foxidrive (Oct 20, 2012)

Are you saving the script in a text file, call it props.bat for arguments sake, and then at a cmd prompt you type 'props' without the quotes.

Put the files all in the bin folder.


----------



## maa3 (Jan 12, 2013)

Did as suggested. All files in bin folder but still receiving same error message. Please what should be the correct sed command line?


----------



## foxidrive (Oct 20, 2012)

The script has the correct sed commands inside it. You don't run sed at all, only the batch file.


----------



## maa3 (Jan 12, 2013)

foxidrive said:


> The script has the correct sed commands inside it. You don't run sed at all, only the batch file.


 Thanks. Run the bat file. Program run only one iteration then message:
could not find C:\program files\gnuwin32\bin\casetmp.dat
access denied.

I then created casetmp.dat in the bin folder but still same message.


----------



## foxidrive (Oct 20, 2012)

Run this bat file. When it hits the pause, examine the folder that has the batch file for a file called casetmp.dat
Look inside the file with notepad to see if the correct information is on the altered lines.
Close notepad and look for any error messages on the screen and the press space and it will run the second loop and repeat.

I need information on the error messages and if the file casetmp.dat is being updated correctly.

It seems that something is not right and we have to determine what that is - error messages will help.

NOTE that case1.dat needs to be in the folder too.


```
@echo off
for /L %%a in (1,1,5) do call :next %%a
echo done
pause
goto :EOF
:next
for /f "delims=" %%b in ('"C:\Program Files\GnuWin32\bin\sed" %1!d concrete.dat') do set "twenty-one=%%b"
for /f "delims=" %%c in ('"C:\Program Files\GnuWin32\bin\sed" %1!d steel.dat') do set "twenty-three=%%c"
for /f "delims=" %%d in ('"C:\Program Files\GnuWin32\bin\sed" %1!d output.dat') do set "eighty-eight=%%d"
"C:\Program Files\GnuWin32\bin\sed" -e "s/^CONCRETE PROPERTIES$/%twenty-one%/" -e "s/^STEEL  PROPERTIES$/%twenty-three%/" -e "s/^CYC1.dat$/%eighty-eight%/" case1.dat  >casetmp.dat
pause
idarc2d_6.0.exe casetmp.dat
del casetmp.dat
```


----------



## maa3 (Jan 12, 2013)

I checked all by running batch file you sent. No casetmp.dat file was found in folder. No changes occurred in case1.dat with respect to the lines to be replaced (lines 21, 23 and 88). Message on screen was:
could not find C:\program files\gnuwin32\bin\casetmp.dat
access denied.
Another trial was made this by creating the casetmp.dat file by myself but still the file when opened remained blank when program paused.


----------



## foxidrive (Oct 20, 2012)

Please zip these files in an attachment to a reply so I can test it for myself.

concrete.dat
steel.dat
output.dat
case1.dat


----------



## maa3 (Jan 12, 2013)

Please find attached the zip files requested for (New Folder). Thank you.


----------



## foxidrive (Oct 20, 2012)

Instructions:

1) Create a folder on your desktop.
2) Copy these files to the folder
concrete.dat
steel.dat
output.dat
case1.dat 
idarc2d_6.0.exe
3) save the file below to a batch file called props.bat into the same folder
4) ensure that there is a file at "C:\Program Files\GnuWin32\bin\sed.exe"
5) open a cmd prompt in the folder on your desktop
6) type props and press enter

Let me know of any error messages, if there are any on the cmd prompt window.


```
@echo off
for /L %%a in (1,1,5) do call :next %%a
echo done
pause
goto :EOF
:next
for /f "delims=" %%b in ('"C:\Program Files\GnuWin32\bin\sed" %1!d concrete.dat') do set "twenty-one=%%b"
for /f "delims=" %%c in ('"C:\Program Files\GnuWin32\bin\sed" %1!d steel.dat') do set "twenty-three=%%c"
for /f "delims=" %%d in ('"C:\Program Files\GnuWin32\bin\sed" %1!d output.dat') do set "eighty-eight=%%d"
"C:\Program Files\GnuWin32\bin\sed" -e "s/^CONCRETE PROPERTIES$/%twenty-one%/" -e "s/^STEEL PROPERTIES$/%twenty-three%/" -e "s/^CYC1.OUT$/%eighty-eight%/" case1.dat  >casetmp.dat
idarc2d_6.0.exe casetmp.dat
del casetmp.dat
```


----------



## maa3 (Jan 12, 2013)

foxidrive said:


> Instructions:
> 
> 1) Create a folder on your desktop.
> 2) Copy these files to the folder
> ...


I tried your suggestion. This time the program seemed to run 5 times before terminating without any error message on the cmd prompt window. However, only one output file CYC1.out was produced. Moreso the contents of Case1.dat remained the same as the beginning, indicating that the content of case1.dat did not change during the runs. If CYC1.OUT changes to CYC2.out during execution, CYC2.out must be produced as an output and there must be CYC1.out, CYC2.out, CYC3.out, CYC4.out and CYC5.out after the 5 runs. There is also another output file the program produces called case1.out which proved that case1.dat did not change at all. I could see this because the original line 21 and 23 in the original case1.dat is different from what I actually gave in the first lines of concrete.dat and steel.dat. According to the contents of case1.out, it is the original case1.dat which was used in running the iteration. I was also wondering why the last lines of concrete.dat and steel.dat were not what remained in case1.dat after running the 5 iterations. It appears there is something wrong.


----------



## foxidrive (Oct 20, 2012)

case1.dat is used as a template each time to create casetmp.dat which the program is meant to use to get its input data from. If it doesn't use it then it will not create the CYC1/2/3/4/5.out files.

Can idarc2d_6.0.exe take an argument of the dat file to use?


----------



## maa3 (Jan 12, 2013)

Thank you very much. It has now worked. I did not seem to understand the syntax you used. There is actually another file called idarc.dat that idarc2d_6.0 calls during execution which I had not mentioned because I thought only case1.dat had to be changed for it to work. That file has only 2 lines with the first line being case1.dat. I changed 'case1.dat' to 'casetmp.dat' in that file and was able to get all the 5 files CYC1.out, CYC2.out etc.
This example is just for 1 member of an entire building. I will therefore use this to develop one for a complete building and see how it goes. Hopefully may be alright with that, otherwise will let you know. Thanks so much 'foxidrive' for your patience in taking me through this problem. Thanks again.


----------

