Solved

Automate pascal program command line input

Posted on 2009-04-07
8
2,056 Views
Last Modified: 2012-05-06
I am looking to automate a pascal program to accept the readlnI() input automatically either via a parameter file or as parameters when the program is executed on the command line.

I am not really familiar with pascal and I am wondering how best to modify this source code such that the program will accept it's 'input' and 'output' filenames as parameters or if no parameters are supplied, prompt for the user to input them as the program is executing.
Program SPMake;
var
  f : text;
  w : text;
  In_file : String[12];
  In_pref : String[8];
  out_file : String[12];
  lines   : longint;
  suffix  : String[3];
  Alldone : Boolean;
  file_no : integer;
  c : char;
  asc : longint;
 
begin
  Write('Name of SPOOL files -> ');
  Readln(In_file);
  if (length(In_file)=0) or (pos('.',In_file)>0) then
  begin
    writeln('Invalid SPOOL file name.');
    halt;
  end;
 
  Write('File to be created -> ');
  Readln(Out_file);
 
  if (length(Out_file)=0) or (pos('.',Out_file)>9)
  or ((pos('.',In_file)=0) and (length(In_file)>8)) 
  or (length(Out_file)>12)   then
  begin
    writeln('Invalid output file name.');
    halt;
  end;
  In_Pref:=In_file;
 
  Assign(w, out_file);
  Rewrite(w);
    
 
 
  Alldone := False;
 
  file_no:=0;
  while not alldone do 
  Begin
    file_no:=file_no+1;
    str(file_no,suffix);
    while length(suffix)<3 do suffix:='0'+suffix;
 
    In_file:=In_pref+'.'+suffix;  
    Assign(f, In_file);
    {$I-}
    Reset(f);
    {$I+}
    If IOResult<>0 then
    begin
      Alldone:=True;
      if suffix='001' then 
      begin
        write('File ',in_file,' not found.');
        halt;
      end;
    end else
    begin  
      lines:=0;
      writeln('Reading ', In_file, ' into ',out_file);
      while not eof(f)  do
      begin
        read(f,c);
        asc:=ord(c);
        if (asc>9) and (asc<>29) then write(w,c);
 
        {if (asc>= 13) and (asc <= 126) then write(w,c);}
      end;
      close(f);
      erase(f);
    end;
  end;    
  Close(w);
  Writeln('Files combined.');
end.

Open in new window

foxbase-screenshot.JPG
0
Comment
Question by:james_j_hedges
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 28

Assisted Solution

by:Bill Bach
Bill Bach earned 100 total points
ID: 24088694
In the C language, the main() function accepts two arguments, known commonly as argc and argv, which contains the count and the values or each command-line argument.  I would expect that your PASCAL compiler will have a similar feature.  A quick search seems to point to using an array called ParamStr[n] when n=0 is the program name, n=1 is the first parameter, and so on.  However, checking your specific compiler manual is the best way to figure this out.  Here's some links to get you started, though:
    http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas074.htm
    http://www.mirrorservice.org/sites/www.gnu-pascal.de/gpc/Accessing-Command-Line-Arguments.html
0
 
LVL 48

Expert Comment

by:dbrunton
ID: 24095242
This looks like Turbo Pascal.

In which case the code you want is the ParamCount and ParamStr functions.

if Paramcount < 1 then
writeln('No parameters on command line')
else
writeln(Paramcount, ' paramete(s)');


for i := 1 to Paramcount do
writeln(ParamStr(i));

Paramstr(index) is an array of the command line parameters.  Blanks and tabs separate the parameters on the command line.
0
 

Author Comment

by:james_j_hedges
ID: 24095282
Thanks for the advice. How did you determine whether the code is 'Pascal' or 'Turbo Pascal'? I'm assuming that I will need to use a different compiler depending on the language?

Thanks
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 48

Accepted Solution

by:
dbrunton earned 400 total points
ID: 24095295
This is the type of code you are wanting.

GetBothFileNames is a procedure that does what you are doing now.  Your present file name getting code goes into this procedure.

GetOutFileName only gets the file name for the out file.  It is a subset of your present code.

These two lines assign the command line parameters to the in and out files.

InFile := ParamStr(1);
OutFile := ParamStr(2);


if ParamCount = 0 then
	GetBothFileNames
else  if ParamCount = 1 then
begin
	InFile := ParamStr(1);
	GetOutFileName
end
else
begin
	InFile := ParamStr(1);
	OutFile := ParamStr(2);
end;

Open in new window

0
 
LVL 48

Assisted Solution

by:dbrunton
dbrunton earned 400 total points
ID: 24095375
Actually it could be standard Pascal.

I assumed the IOResult code was a Turbo Pascal function.  It's not.  It's in a couple of other Pascals as well.

This compiler directive

{$I+}

is Turbo Pascal plus it's used in other Pascal compilers such as Free Pascal and GNU Pascal.

The code for your file names looks like it's designed for DOS from the way the filenames are being created and checked.  The most common DOS Pascal compiler was the Turbo Pascal range.

But which compiler are you using for your code?  Several of the Turbo Pascal compilers are free and readily available.
0
 

Author Comment

by:james_j_hedges
ID: 24095483
I'm not really sure which compiler is being used. I would imagine maybe Turbo Pascal. Do have any suggestions as to which specific compiler to use?

Thanks
0
 
LVL 48

Assisted Solution

by:dbrunton
dbrunton earned 400 total points
ID: 24095552
Turbo Pascal 5.5

http://edn.embarcadero.com/article/20803

You'll need to create a free account to get to the download site.  There's a few tricks involved to get it running on Windows 2000 and XP but all of those are fixable/solvable.

Under Windows 9x or DOS there are no problems.  You may strike problems with fast CPUs but those are fixable as well.


Free Pascal would probably do the job as well.  http://www.freepascal.org/

Shouldn't be any problems getting it running under Win 2000 and XP.
0
 

Author Comment

by:james_j_hedges
ID: 24097084
I've managed to update the pascal program to successfully command line parameters.

Thanks for your help!
Program SPMake (Output);
var
	arguments : integer;
  f : text;
  w : text;
  In_file : String[12];
  In_pref : String[8];
  Out_file : String[12];
  lines   : longint;
  suffix  : String[3];
  Alldone : Boolean;
  file_no : integer;
  c : char;
  asc : longint;
 
begin
 
	if ParamCount = 0 then
		begin
		Writeln('Please enter Input SPOOL files and Output SPOOL file names as parameters');
		end
	else if ParamCount = 1 then
					begin
			 		Writeln('Please enter both Input SPOOL files and Output SPOOL file names as parameters');
			 		end
			 else 
			 begin
  				In_file := ParamStr(1);
  				Out_file := ParamStr(2);
	end;
	
  if (length(In_file)=0) or (pos('.',In_file)>0) then
  begin
    writeln('Invalid SPOOL file name.');
    halt;
  end;

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question