Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting Dos Result

Posted on 2003-11-18
16
Medium Priority
?
266 Views
Last Modified: 2010-04-05
Greeting...
How can I make command in dos & get the result in textfile ?
and what if i have many commands ?

example :
C:\cd New Folder
C:\New Folder\Dir

thank you.
0
Comment
Question by:TheLeader
[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
  • 6
  • 6
  • 3
  • +1
16 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9776065
Look at my previous answer to this, almost, same question:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20756036.html
0
 
LVL 2

Author Comment

by:TheLeader
ID: 9778131
well i just saw this question, but its kind of, but not the same.
I want to run many commands in dos, and get the result in text file.
and one more thing i want to control the working directory, not as that example.

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9778404
C:\New Folder\Dir >text_file.txt
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 26

Expert Comment

by:EddieShipman
ID: 9779233
Then create a batch file and run it with the > You can run the batch file just by using ShellExecute.
I have a memo on my form and I type in:
  cd\eddie
  dir >>result.txt
  cd \mrbaseba;;\mrbaseball
  dir >> result.txt
  cd\
  dir >> result.txt
 
  Then in a buttonclick I have this:
  Memo1.Lines.SaveToFile('c:\blah.bat');
  // Change sw_show to sw_hide if you don't want the window to show.
  ShellExecute(Handle, 'open', 'c:\blah.bat', '', 'c:\', SW_SHOW);

Runs perfectly and creates the file with the results of the dir command.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9779254
oops, typo...

cd \mrbaseball\mrbaseball
NOT
cd \mrbaseba;;\mrbaseball

0
 
LVL 2

Author Comment

by:TheLeader
ID: 9779938
ok, After I run the Batch with shellexecute wheres the Result ?
as i can see, the command only in memo not the result of command.
From your example, lets say I want the result in the memo, how ?
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9780254
To get the results you need to use CreateProcess and you need to redirect stdout and stderr streams of the created process.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9780269
Or just read the files std_out and str_err, if you run command like the following:

dir >std_out  2>str_err
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9781507
Then load the result.txt file into the memo using:
  Memo1.Lines.LoadFromFile('c:\result.txt');

0
 
LVL 2

Author Comment

by:TheLeader
ID: 9781909
ok i got the idia.
but can anyone show with code please ?
thank you
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9789875
Damn, man, I showed you the code above, do you want me to hold your hand, too?
Next time, you can pay for us to write your application for you.

Actually, you will need to wait until the ShellExecute is finished.

function WinExecAndWait32(FileName: string; Visibility: integer): Cardinal;
 { returns 9999 if the Exec failed, otherwise returns the process' exit
   code when the process terminates }
var
  zAppName: array[0..512] of char;
  zCurDir: array[0..255] of char;
  WorkDir: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  StrPCopy(zAppName, FileName);
  GetDir(0, WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  if not CreateProcess(nil,
    zAppName, { pointer to command line string }
    nil, { pointer to process security attributes }
    nil, { pointer to thread security attributes }
    False, { handle inheritance flag }
    CREATE_NEW_CONSOLE or { creation flags }
    NORMAL_PRIORITY_CLASS,
    nil, { pointer to new environment block }
    nil, { pointer to current directory name }
    StartupInfo, { pointer to STARTUPINFO }
    ProcessInfo) then { pointer to PROCESS_INF }
    Result := 9999
  else
  begin
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
  end;
end;

procedure TForm1.Button1click(Sender:TObject);
begin
  Memo1.Lines.SaveToFile('c:\blah.bat');
  // Change sw_show to sw_hide if you don't want the window to show.
  WinExecAndWait32('c:\blah.bat', SW_HIDE);
  Memo1.Lines.LoadFromFile('c:\result.txt');
end;
0
 
LVL 2

Author Comment

by:TheLeader
ID: 9795798
"EddieShipman" I Appreciate your work, but in the first place, I ask this question because I don't know how !
you have some skill -> (keep it, or share it)
be nice, no hard feelings.
anyway I'll try your code, thank you.
0
 

Expert Comment

by:ProgramMaker
ID: 9795865
I agree with you TheLeader
EddieShipman be mr.nice guy
0
 
LVL 2

Author Comment

by:TheLeader
ID: 9796060
"EddieShipman", well i think you miss something...
i know this function is the same function in the link, I test it before there was no problem, BUT you keep saying "result.txt", How can I get the result in result.txt if theres no file called result.txt, ok I created it, but it still empty.
So, How can I take the result from your function into the memo ?
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 9799625
Did you create the blah.bat file with the commands that you want to run?
If you don't, you won't get the result.txt file.

blah.bat:
  cd\eddie
  dir >result.txt
  cd \mrbaseball\mrbaseball
  dir >> result.txt
  cd\
  dir >> result.txt

This worked flawlessly for me. REMEMBER TO USE >> insted of >
in the second and third times writing to the file because otherwise
it will overwrite the results each time you are outputting to the file.

I have given you enough information to accomplish your goal, here.
Read each post and put the steps together and it will help.


0
 
LVL 2

Author Comment

by:TheLeader
ID: 9799964
the problem was ">" after the command.
thank you EddieShipman.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

715 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