0

The value coming into this function is: "C:\Export\SFTPPutProd.cmd" "C:\Export\*.csv"

SFTPPutProd.cmd contains one line of code for testing purposes. This executes fine as long as the argument (and command file) doesn't contain double quotes.

copy %1 C:\Export\copy_destination

How can I get it to run? I've tried using the @ symbol to make it work per other Answers here but no luck. Without the double quotes the command runs fine but I need to support the double quotes in case the folder has spaces.

    private string RunCmdAndWait(string command) {
        string result;
        try {
            ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;

            using (Process process = new Process()) {
                process.StartInfo = procStartInfo;
                process.Start();

                // Wait until process does its work
                process.WaitForExit();

                // Then read the result
                result = process.StandardOutput.ReadToEnd();

            }
        }
        catch (Exception ex) {
            _log.WriteWarning($"There was an error running the command file {command}");
            result = "Error";
        }
        return result;
    }
Rich Bianco
  • 4,141
  • 3
  • 29
  • 48
  • Please provide [mre] for both cmd and C# (later one can be more stripped version of what you have with inline values) - I'm 99% sure that it has nothing to do with C# at all, but since you presumably confirmed that `/c` behaves the way you like it would be nice to see confirmation (ideally with links to docs of CMD which from what I see call BS on your statement that it works) – Alexei Levenkov Mar 17 '23 at 20:16
  • @AlexeiLevenkov I added that information. Thank you for looking. Note: When I saw success I did not use the intermediate variable cmd, I just used command as it came into the function. So I modified the function, it runs fine using input without double quotes. – Rich Bianco Mar 17 '23 at 20:27
  • The use of `cmd` may be unnecessary and may not work as intended. The use of double-quotes most likely isn't necessary because the string doesn't contain any spaces. – Tu deschizi eu inchid Mar 17 '23 at 20:38
  • @user09938 You are correct. My example doesn't contain spaces but my code needs to support cases where there might be a space in the folder name., otherwise I'd just strip them off before running the command. I'm green to .NET and find using files very challenging. – Rich Bianco Mar 17 '23 at 20:41
  • If you're just copying files, why not use [System.IO.FIle.Copy](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=net-7.0)? – Tu deschizi eu inchid Mar 17 '23 at 20:43
  • @user09938 I'm not just copying files this is just a minimally reproduceable example. The command file will contain much more functionality like SFTP and other things. But I can't even get it to run using one simple argument. – Rich Bianco Mar 17 '23 at 20:45
  • Can you please confirm that you RTFM (cmd/?): "Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character." (feel free to flag as "rude or unkind" afterwards) – Alexei Levenkov Mar 17 '23 at 20:56
  • If one opens a `cmd` window and types: `copy "C:\Export\*.csv" "C:\Export\copy_destination"` one receives an error. – Tu deschizi eu inchid Mar 17 '23 at 21:28
  • 1
    The following may be helpful: https://stackoverflow.com/a/71344930/10024425 – Tu deschizi eu inchid Mar 17 '23 at 21:28
  • 1
    Most likely you'll need to test each command that you'd like to run and ensure that it's in the proper format prior to attempting to run it (ie: add or remove single or double quotes as necessary). – Tu deschizi eu inchid Mar 17 '23 at 21:31

1 Answers1

0

Posting my own answer here.

Using cmd /s /c and surround the entire command with double quotes as shown below allows your arguments to be surrounded in double quotes. The cmd.exe program removes the outermost quotes and leaves innermost quotes alone.

    private string RunCmdAndWait(string command) {
        string result;
        try {

            var cmd = $"\"{command}\"";
            ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/s /c " + cmd);

            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;

            using (Process process = new Process()) {
                process.StartInfo = procStartInfo;
                process.Start();

                // Wait until process does its work
                process.WaitForExit();

                // Then read the result
                result = process.StandardOutput.ReadToEnd();

            }
        }
        catch (Exception ex) {
            _log.WriteWarning($"There was an error running the command file {command}");
            result = "Error";
        }
        return result;
    }
Rich Bianco
  • 4,141
  • 3
  • 29
  • 48