[Mono-list] wildcard error in System.Diagnostics.Process

Michael Hutchinson m.j.hutchinson at gmail.com
Sun Mar 22 18:57:21 EDT 2009


On Sat, Mar 21, 2009 at 5:40 PM, amalafrida <amalafrida at gmail.com> wrote:
>
> /* wildcard won't work ... any ideas?
> directories exist ... with correct permissions ... dir1 contains files ...
>
> error:
> /bin/cp: cannot stat `/mnt/dir1/*': No such file or directory
> */
>
> using System;
> using System.Diagnostics;
> using System.IO;
> using System.Text;
>
> class Program
> {
>    public static void Main(string[] args)
>    {
>    Process proc = new Process();
>    proc.StartInfo.FileName = "cp ";
>    proc.StartInfo.Arguments = "/mnt/dir1/* /mnt/dir2";
>    proc.Start();
>    proc.WaitForExit();
>    proc.Close();
>    }
> }

AFAIK the * expansion is usually done by sh, not cp. You have 2 options.

a) Do the copy in managed code, using System.IO.Directory.GetFiles
(which has wildcard options) and System.IO.File.Copy. This will work
cross-platform, and may well be faster, since it doesn't require
creating a process.
b) Invoke bash, passing your commands as an argument with -c:
proc.StartInfo.FileName = "bash"; proc.StartInfo.Arguments = "-c \"cp
/mnt/dir1/* /mnt/dir2\"";

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Mono-list mailing list