Link to home
Start Free TrialLog in
Avatar of ipjyo
ipjyoFlag for United States of America

asked on

How to format Process.StartInfo.Arguments with spaces in the arguments?

hi,

I am trying to invoke an ftp command from my C# application using a Process.
I have the below code in my program.
I have spaces in the first argument of the Process and it is acting weird and not formatting properly.

scriptfile = "C:\Program files\PR Ent\AddIns\PR Bridge\Upload\UploadScript.txt";
s.Filename = "File1";

Process P = new Process();
                    P.EnableRaisingEvents = true;
                    P.Exited += P_Exited;
                    P.StartInfo.FileName = filename;
                    P.StartInfo.Arguments = scriptfile + s.Filename;
                    P.Start();

Could anybody please help me how to format properly when having spaces?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipjyo

ASKER

Thanks for the response.

Can you please clarify why we need to change

scriptfile = "C:\Program files\PR Ent\AddIns\PR Bridge\Upload\UploadScript.txt";
-->
scriptfile = "C:\\Program files\\PR Ent\\AddIns\\PR Bridge\\Upload\\UploadScript.txt";

Also, Would the below line is considered as having two arguments?

P.StartInfo.Arguments = "\"" + scriptfile +"\" "+ s.Filename;

Thanks.
Avatar of ipjyo

ASKER

For some reason it is still having the same problem.

In my script I have the following code. When this script is executed it is supposed to create a file in the below location. But it is creating a file in "C:\Program" since it is taking the file name only up to the first space.

C:\\Program files\\PR Ent\\AddIns\\PR Bridge\\Upload\\UploadScript.txt

Thanks for your help.
@echo off
setlocal
set ftpscript=%1
echo open ftp.coinc.com>> "%ftpscript%"
echo co-ftp>> "%ftpscript%"
echo pw-ftp>> "%ftpscript%"
echo lcd C:\Program Files\PR Ent\AddIns\PR Bridge\Upload>> "%ftpscript%"
echo put %2>> "%ftpscript%"
echo disconnect>> "%ftpscript%"
echo bye>> "%ftpscript%"

ftp -v -i -s:%ftpscript%

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipjyo

ASKER

Thank you for the responses.
I have been trying to get it worked for several hours but still having a problem.
I am trying exactly the attached code. And I have my Upload.cmd as below.
It is creating the below "UploadScript.txt" as expected in the correct location "C:\Temp Files". But it is saying "Error opening script file C:\Temp"


@echo off
setlocal
set ftpscript="%1"
echo open ftp.coinc.com>> "%ftpscript%"
echo co-ftp>> "%ftpscript%"
echo pw-ftp>> "%ftpscript%"
echo lcd "C:\Temp Files\UploadFiles">> "%ftpscript%"
echo put %2>> "%ftpscript%"
echo disconnect>> "%ftpscript%"
echo bye>> "%ftpscript%"

ftp -v -i -s:"%ftpscript%"


UploadScript.txt is created as follows and looks good. But for some reason it is still trying to look for C:\Temp

open ftp.coinc.com
co-ftp
pw-ftp
lcd "C:\Temp Files\UploadFiles"
put G3A336
disconnect
bye


Thanks for your help.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace seasharpwinapp
{
    public partial class Form1 : Form
    {

        string batchscriptfile = @"C:\Temp Files\UploadScript.txt";
        string filename = "G3A336";

        bool hasEventException = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool status = StartUploadProcess();

            if (status)
            {
                MessageBox.Show("Upload successful");
            }
            else
            {
                MessageBox.Show("Upload failed");
            }
        }
        
        private bool StartUploadProcess()
        {
            try
            {
                Process P = new Process();
                P.EnableRaisingEvents = true;
                
                P.StartInfo.FileName = @"C:\\Temp Files\\Upload.cmd";
                
                P.StartInfo.Arguments = string.Format("\"{0}\" {1}", batchscriptfile, filename);
                
                P.Start();
                System.Threading.Thread.Sleep(20000);


                return !hasEventException;               
            }


            catch (Exception x)
            {
                MessageBox.Show(string.Format("Error while uploading. {0} \r\n {1}", x.Message, x.InnerException.ToString()));
                return false;
            }
        }

        
        
    }
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Complete upload.cmd:
@echo off
setlocal
set ftpscript=%1
echo open ftp.coinc.com>> "%ftpscript%"
echo co-ftp>> "%ftpscript%"
echo pw-ftp>> "%ftpscript%"
echo lcd "C:\Temp Files\UploadFiles">> "%ftpscript%"
echo put %2>> "%ftpscript%"
echo disconnect>> "%ftpscript%"
echo bye>> "%ftpscript%"

ftp -v -i -s:%ftpscript%

Open in new window

try single quotes

echo lcd "C:\Temp Files\UploadFiles">> "%ftpscript%"
-->
echo lcd 'C:\Temp Files\UploadFiles'>> "%ftpscript%"

or use 8 char dir name

echo lcd C:\TEMPFI~1\UPLOAD~1>> "%ftpscript%"

HainKurt:
I have reproduce the problem locally and my updated upload.cmd works fine.
Avatar of ipjyo

ASKER

Please give me a little bit time. I am trying and let you know.

Thanks guys for your help.
Avatar of ipjyo

ASKER

Please give me a little bit time. I am trying and let you know.

Thanks guys for your help.
MikeQc,

what is your explanation for this?

"
UploadScript.txt is created as follows and looks good. But for some reason it is still trying to look for C:\Temp

open ftp.coinc.com
co-ftp
pw-ftp
lcd "C:\Temp Files\UploadFiles"
put G3A336
disconnect
bye
"
Avatar of ipjyo

ASKER

Hi Guys,

It is working now. I had to remove the quotes in each line of the Upload.cmd script.
The script looks as follows and I did not have to change anything else in the C# program.
This line still works.
P.StartInfo.Arguments = string.Format("\"{0}\" {1}", batchscriptfile, filename);

echo off
setlocal
set ftpscript=%1
echo open ftp.coinc.com>> %ftpscript%
echo co-ftp>> %ftpscript%
echo pw-ftp>> %ftpscript%
echo lcd "C:\Temp Files\UploadFiles">> %ftpscript%
echo put %2>> %ftpscript%
echo disconnect>> %ftpscript%
echo bye>> %ftpscript%

ftp -v -i -s:%ftpscript%


I appreciate both of your help on this. Thank you very much.
Avatar of ipjyo

ASKER

Thank you.