System.Diagnostics.Process doesn't execute all commands in external file
13 views (last 30 days)
Show older comments
I'm attempting to use the following code to get Matlab to open the Windows command prompt, read all the commands in an external text file, and execute those commands one by one in the command prompt.
Process = System.Diagnostics.Process;
%Replace with path to system command prompt:
Process.StartInfo.FileName = "C:\windows\system32\cmd.exe";
Process.StartInfo.UseShellExecute = false;
Process.StartInfo.RedirectStandardInput = true;
Process.StartInfo.Arguments = "/k"
Process.StartInfo.CreateNoWindow = false;
Process.Start();
stdin = Process.StandardInput;
%Replace with path to file with series of commands:
fid = fopen('C:\Users\User\MATLAB\Matlab.txt ','r+')
txt = fread(fid,'*char');
fclose(fid);
stdin.Write(txt);
% wait for cmd.exe to finish processing
% option 1
pause(1); % pause for a predefined amount of time
I've attached the text file I'm reading the commands from. The issue I'm having is that the command prompt only appears to run until the Date command is executed, and ignores the Time and echo commands on the next two lines. What would I change in my code to fix this? Thanks.
0 Comments
Answers (1)
Pratyush
on 31 Jul 2023
I understand that the script present in the text file is executed partially when you use the above code block.
The issue you're facing is related to how the ‘stdin.Write(txt)’ function handles newline characters in the text file. By default, ‘fread’ reads the text file as a binary file, which may cause issues when writing to the command prompt.
You could try using 'fgetl' function to read your text file. The following code should work for your case.
% Create a cell array to store the commands
commands = {};
% Replace with path to file with series of commands:
fid = fopen('C:\Users\User\MATLAB\Matlab.txt', 'r');
line = fgetl(fid);
% Read each line from the file
while ischar(line)
commands{end+1} = line;
line = fgetl(fid);
end
fclose(fid);
% Start the command prompt process
Process = System.Diagnostics.Process;
Process.StartInfo.FileName = 'C:\windows\system32\cmd.exe';
Process.StartInfo.UseShellExecute = false;
Process.StartInfo.RedirectStandardInput = true;
Process.StartInfo.CreateNoWindow = false;
Process.Start();
% Write each command to the command prompt
stdin = Process.StandardInput;
for i = 1:numel(commands)
stdin.WriteLine(commands{i});
end
% Wait for cmd.exe to finish processing (option 1)
pause(1); % pause for a predefined amount of time
This updated code reads the text file line by line using 'fgetl' and stores each command in a cell array. Then, it iterates over each command and writes it to the command prompt using ‘stdin.WriteLine’. This approach ensures that newline characters are handled correctly, allowing all commands to be executed properly.
You can refer to the following documents to see the difference between 'fgetl' and 'fread':
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!