Is there any way to run "dir" directly?
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.