Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a Batch File

Status
Not open for further replies.

minckle

Programmer
Mar 17, 2004
142
GB
is it possible to run a batch file with a parameter in it

i.e i want to run a batch file that copied a file the user chose to a directory the user chose.

if this can be done can anyone give me any tips on how to code the batch file with the parameters in, and how to run the batch file from delphi and set the parameters accordingly

Thanks
 
To run a program or a batch file, you'll do something like this:
Code:
function SpawnRunBatch(const sPath, sFile: String): Boolean;
var
  CmdFile: string;
  CmdParm: string;
  RetVal: LongWord;
begin
  try
    CmdFile := '"c:\MyBatch.bat"';
    CmdParm := '"' + sPath + '" "' + sFile + '"';
    RetVal := WinExec(PChar(CmdFile + ' ' + CmdParm), SW_SHOW);
    Result := RetVal > 32;
    if not Result then
    begin
      ShellApiError(RetVal);
    end;
  except
    on E: Exception do
    begin
      Result := False;
      MessageDlg(E.Message + #13#10 + CmdFile + #13#10 + CmdParm, mtError, [mbOK], 0);
    end;
  end;
end;
The sPath parameter will be the destination path that the user choses and the sFile parameter is the file to be copied. It's been a long time since I've worked in batch files but, IIRC, your batch file should look something like this:
Code:
IF %1=="" GOTO ERR1
IF %2=="" GOTO ERR2

copy %2 %1
GOTO ENDCOPY

:ERR1
ECHO Destination Path has not been specified
PAUSE Press Enter to Continue
GOTO ENDCOPY

:ERR2
ECHO Source File has not been specified
PAUSE Press Enter to Continue

:ENDCOPY

-Dell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top