How can I run this command from my Delphi application?
C:\myapppath\appfolder>appname.exe /stext save.txt
I tried the following code:
ShellExecute(0, nil, 'cmd.exe', 'cd C:\myapppath\appfolder', nil, SW_Hide);
ShellExecute(0, nil, 'cmd.exe', 'appname.exe /stext save.txt', nil, SW_Hide);
But it didn't work. Can anyone provide a solution?
To run a CMD command, you need to use the /C flag of cmd.exe:
ShellExecute(0, nil, 'cmd.exe', '/C cd C:\myapppath\appfolder', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', '/C appname.exe /stext save.txt', nil, SW_HIDE);
However, this will create two different sessions, so it will not work. But you can use ShellExecute to run appname.exe directly, like so:
ShellExecute(0, nil, 'appname.exe', '/stext save.txt', nil, SW_HIDE);
But you need to specify the filenames properly.
I would do
var
path: string;
begin
path := ExtractFilePath(Application.ExeName);
ShellExecute(0, nil, PChar(Application.ExeName), PChar('/stext "' + path + 'save.txt"'), nil, SW_HIDE);
end;
in case appname.exe is the current application. Otherwise, replace Application.ExeName with the full path of appname.exe.
Related
I have the following function to create a process and wait for its execution:
procedure TdmTransfer.ExecNewProcess(ProgramName, aParams: string);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CreateOK: Boolean;
begin
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CreateOK := CreateProcess(nil,
PChar(ProgramName + ' ' + aParams),
nil,
nil,
False,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil,
nil,
StartInfo,
ProcInfo);
if CreateOK then
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
CloseHandle(ProcInfo.hProcess);
end;
I'm executing psftp.exe -b "download.cmd" -pw "password" user#domain.com which runs fine if I call it from a desktop application.
When I run the code from within a service application the process gets created, but then psftp.exe doesn't finish and my service waits indefinitely.
The file "download.cmd" looks like this:
lcd download
cd directory
mget ./*.XML
quit
What can I do to make it to run inside my service application?
I am trying to backup my mysql database from delphi by using ShellExecute to run mysqldump.
This is how I am using mysqldump:
MySqlDump.exe -u root -p[password] [databasename] > TheOutputFile.Sql
This works from command prompt.
I call this command line using ShellExecute:
ShellExecute(
0,
nil,
'cmd.exe',
'C:\SmartRetail\Test System\Periodic database backup\MySqlDump.exe -u root -p123 smartretailprogramdata > TheOutputFile2.Sql',
nil,
SW_SHOW
);
This does not work, instead it opens command prompt.
What am i doing wrong?
It opens the command interpreter because that's what cmd.exe is. You need to specify /C to tell the cmd process to close after it has interpreted your commands.
On top of that you need to take care of the working directory. Unless the .sql file is in the working directory, the dump process will not find it. And the working directory is inherited from the parent process since you did not specify it. You may need to specify the working directory.
Finally, ShellExecute is really the wrong solution here. You are only using it to get the stdout redirection. But that should really be done explicitly. Use CreateProcess instead.
Of curse you can :
var
BatFile : TStringList;
str: string;
const
EXEC = 'MySqlDump.exe -u root -p[password] [databasename] > TheOutputFile.Sql' ;
begin
BatFile := TStringList.Create;
BatFile.Text := EXEC ;
str := ExtractFilePath(Application.ExeName) + 's.bat';
BatFile.SaveToFile(str);
BatFile.Free;
ShellExecuteW(Handle, 'open', PWideChar(str), nil, nil, SW_HIDE);
Sleep(200);
ShowMessage('Done.');
DeleteFile(str);
end;
I am running a .bat file from delphi(2010).
procedure TForm1.Button2Click(Sender: TObject);
var sCmd: String;
Begin
sCmd := Pwidechar('b4a_c2dm.bat' +' ' +'send ' + Trim(Edit1.Text)+' ' + Trim(edit2.Text ));
ShellExecute(0, 'open', 'b4a_c2dm.bat', PChar(sCmd), nil, SW_SHOWMAXIMIZED);
end;
This opens the cmd.exe and passes the correct string in the cmd.exe , BUT
Some how the line in the .bat file (java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM %*) is showing up in the cmd.exe window and not letting the .bat file do its job.
Can someone help me with this.
In order to execute a batch file, the program to be called is 'cmd' and its parameter should be the name of the batch file.
Regarding your program,
ShellExecute (application.handle, 'open', 'cmd', PChar(sCmd), nil, SW_MAXIMIZE)
How can I schedule a task using delphi 7 like Google updater?
I'm not using the registry because it gets detected by Kaspersky antivirus as a false alarm.
Anything I add in the registry as a start-up item gets detected as a trojan so I decided to use task schedule
The following piece of code shows how to delete and create the task which will run the application at system startup with system privileges. It uses the following command line:
However the Task Scheduler since Windows Vista supports force creation of tasks, I wouldn't use it for backward compatibility with Windows XP, where this flag doesn't exist.
So the example below tries to delete the task (if already exists) and then create the new one.
It executes these commands:
schtasks /delete /f /tn "myjob"
schtasks /create /tn "myjob" /tr "C:\Application.exe" /sc ONSTART /ru "System"
/delete - delete the task
/f - suppress the confirmation
/create - create task parameter
/tn - unique name of the task
/tr - file name of an executable file
/sc - schedule type, ONSTART - run at startup
/ru - run task under permissions of the specified user
And here is the code:
uses
ShellAPI;
procedure ScheduleRunAtStartup(const ATaskName: string; const AFileName: string;
const AUserAccount: string);
begin
ShellExecute(0, nil, 'schtasks', PChar('/delete /f /tn "' + ATaskName + '"'),
nil, SW_HIDE);
ShellExecute(0, nil, 'schtasks', PChar('/create /tn "' + ATaskName + '" ' +
'/tr "' + AFileName + '" /sc ONSTART /ru "' + AUserAccount + '"'),
nil, SW_HIDE);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ScheduleRunAtStartup('myjob', 'C:\Application.exe', 'System');
end;
Figured Out the problem here it works fine
Tested on windows 7 Pro if any one can test for me on XP PRO would b appreciated
procedure ScheduleRunAtStartup(const ATaskName: string; const AFileName: string;
const GetPCName: string ; Const GetPCUser: String);
begin
ShellExecute(0, nil, 'schtasks', PChar('/delete /f /tn "' + ATaskName + '"'),
nil, SW_HIDE);
ShellExecute(0, nil, 'schtasks', PChar('/create /tn "' + ATaskName + '" ' + '/tr "' + QuotedStr(AFileName) + '" /sc ONLOGON /ru "' + GetPCName+'\'+GetPCUser + '"'), nil, SW_HIDE)
end;
How can i send parameters to CMD? for example send a path and start it from that path? How can i execute CMD commands?
Thanks
To start cmd.exe and immediately execute a command, use the /K flag:
procedure TForm1.FormCreate(Sender: TObject);
begin
ShellExecute(Handle, nil, 'cmd.exe', '/K cd C:\WINDOWS', nil, SW_SHOWNORMAL);
end;
To run a command in cmd.exe and then immediately close the console window, use the /C flag:
procedure TForm1.FormCreate(Sender: TObject);
begin
ShellExecute(Handle, nil, 'cmd.exe', '/C del myfile.txt', nil, SW_SHOWNORMAL);
end;
You can also use the Process class - see an example below
AProcess := TProcess.Create(nil); // Create process
AProcess.Executable := 'cmd'; // Executable to run
AProcess.Parameters.Add('/T:B0'); // Set background colour
AProcess.Parameters.Add('/K'); // Keep open
AProcess.Parameters.Add('title'); // A title for cmd
AProcess.Parameters.Add('My Console'); // Title
AProcess.Parameters.Add('&&'); // Start a new command line
AProcess.Parameters.Add('cd'); // Change directory
AProcess.Parameters.Add('D:\X\'); // Path to Folder
{Set environment variable}
AProcess.Parameters.Add('&&'); // Start a new command line
AProcess.Parameters.Add('HOME='+MYSQL_DIR); // Set env example
AProcess.Parameters.Add('&&'); // Start a new command line
AProcess.Parameters.Add('mysql.exe'); // run mysql.exe
AProcess.Parameters.Add('--host=' + VAR_HOST); // Parameter server
AProcess.Parameters.Add('--port=' + VAR_PORT); // Parameter mysql server port
AProcess.Execute; // execute detatched process command window remains visible
AProcess.Free; // free memory