开发者

Is there any way to run "dir" directly?

In my answer to this question, where the asker needed a fast way to get a directory listing o开发者_JAVA技巧f a folder on a network drive, I suggested using the DOS "dir" command. Unfortunately, it's a command, not a program, so you can't execute it with CreateProcess and so I had to put it in a batch file. I don't really like that solution. It feels like a hack to me. Does anyone know a way to run dir from Delphi instead of from an external batch file?


Invisible Solution (Create a pipe, execute the command, redirect stdout to the pipe, read out the pipe):

function Console(const Command: String): String; 
var 
  StartupInfo: TStartupInfo; 
  ProcessInfo: TProcessInformation; 
  SecurityAttr: TSecurityAttributes; 
  OutputPipeRead, OutputPipeWrite: THandle; 
  Res: Boolean; 
  BufSize: Cardinal; 
  Buffer: String; 
  BytesRead: Cardinal; 
begin 
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0); 

  FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0); 
  SecurityAttr.nLength := SizeOf(SecurityAttr); 
  SecurityAttr.bInheritHandle := true; 
  SecurityAttr.lpSecurityDescriptor := nil; 

  CreatePipe(OutputPipeRead, OutputPipeWrite, @SecurityAttr, 0); 

  FillChar(StartupInfo, SizeOf(TStartupInfo), 0); 
  StartupInfo.cb:=SizeOf(StartupInfo); 
  StartupInfo.hStdInput := 0; 
  StartupInfo.hStdOutput := OutputPipeWrite; 
  StartupInfo.hStdError := OutputPipeWrite; 
  StartupInfo.wShowWindow := SW_HIDE; 
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; 

  Res := CreateProcess(nil, PChar(command), nil, nil, true, 
                   CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or 
                   NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo); 

  CloseHandle(OutputPipeWrite); 

  Result := ''; 

  if Res then 
  begin  
    SetLength(Buffer, 5000); 
    BufSize := Length(Buffer); 
    repeat 
      Res := ReadFile(OutputPipeRead, Buffer[1], BufSize, BytesRead, nil); 
      Result := Result + Copy(Buffer, 1, BytesRead); 
    until not Res; 

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE); 
    CloseHandle(ProcessInfo.hProcess); 
  end; 

  CloseHandle(OutputPipeRead); 
end;

Sample Usage:

ShowMessage(Console('cmd /c dir C:\'));


You don't need a batch file, you can just execute:

cmd.exe /c dir

You can check the option to command with cmd /?:

c:> cmd /?
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C  Carries out the command specified by string and then terminates
: : : : :


Why not FindFirstFile? It returns an attribute for each item that you can compare with FILE_ATTRIBUTE_DIRECTORY to check for a directory. I highly suspect that dir doesn't do anything else. Using the dir command is like using a bar to type on the keyboard.


Using DSiWin32:

sl := TStringList.Create;
if DSiExecuteAndCapture('cmd.exe /c dir', sl, 'c:\test', exitCode) = 0 then
  // exec error
else
  // use sl
sl.Free;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜