Solved

pass argument to batch file with space in the argument

Posted on 2011-09-21
7
724 Views
Last Modified: 2012-05-12
I have a console application written in C# that starts a process and I need to pass an argument to it that has a space in the name. This example shows how I can have the batch file start an application that I set as the parameter. I used it to launch notepad, but if I put a space between note and pad it no longer works.

                try
                {
                    string targetDir;
                    targetDir =
                      string.Format(@"W:\");
                    p = new Process();
                    p.StartInfo.WorkingDirectory = targetDir;
                    p.StartInfo.FileName = "test.bat";

                    p.StartInfo.Arguments =
                      string.Format("note pad");
                    p.StartInfo.CreateNoWindow = false;
                    p.Start();
                    p.WaitForExit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occurred :{0},{1}",
                                ex.Message, ex.StackTrace.ToString());
                }

Open in new window


How can I pass that argument with the space in it?
0
Comment
Question by:wiggy353
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36576547
You need double quotes around the argument on the command line.  As to how to escape the " characters in C#,not my language.  
May involve referring to it as character 34, doubling up "" or using \" etc.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36576566
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36576570
So I presume that is

string.Format("\"note pad\"");

or
string.Format("""note pad""");

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:wiggy353
ID: 36576580
Tried both of those. Still just sees the argument as note.
0
 
LVL 1

Author Comment

by:wiggy353
ID: 36576615
Actually, when I do it with the "\"note pag\"", it opens a new console window titled "note pad", but doesn't launch the note pad.exe application.
0
 
LVL 1

Author Comment

by:wiggy353
ID: 36576683
which is actually what it does if I hard code note pad into the batch file... hmm.. so it is passing the argument correctly, I must need to do something with the batch file.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 36576694
I presume you are using something else to note pad.exe as of course it's real name is notepad.exe ?

Ahh so if that is doing the same as start then?...
If so the first command in "" is the title.  The second is the progr. to run, so
try
 "\"Some title\" note pad.exe"

Anyway I see this already in c# zone so should leave it to someone who knows the language...
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question