0

Ok I'm learning how shell commands work, so I decided to develop a app to send the commands. This is what I got.

moveDirectory.setOnClickListener(new OnClickListener(){
    public void onClick(View v)
    {
        try{
            Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/", "cp pic1 /sdcard/pic1"});
            send.waitFor();
        } catch (Exception ex){
            String toast = null;
            Log.i(toast, "Couldn't copy file", ex);
            }
        }
    });

But it isn't working, the first command is working, but not the second one. What should I add to it?

Thanks

EDIT: forgot to add the send.waitFor(); line

2
  • Do you have all permissions to success these directories? Commented Sep 28, 2012 at 6:42
  • Didn't know that I need permissions, which ones should I need? and if its a permission problem, why it changes the directory to /sdcard/music/, but it doesn't copy the file? Commented Sep 28, 2012 at 6:45

2 Answers 2

3

Use normal command delimeter ;

moveDirectory.setOnClickListener(new OnClickListener(){
    public void onClick(View v)
    {
        try{
            Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/ ; cp pic1 /sdcard/pic1"});
        } catch (Exception ex){
            String toast = null;
            Log.i(toast, "Couldn't copy file", ex);
            }
        }
    });

In this code you
1) go to the /sdcard/music
2) copy from /sdcard/music pic1 to /sdcard/pic1

Sign up to request clarification or add additional context in comments.

3 Comments

OMG, can't believe that it was so simple... thank you very much this did the trick, last question: Can I add as much as I want?
yes but all must be saparate with ; - symbol. Also more about bash you can find here gnu.org/software/bash/manual/bashref.html
Awesome link, this will keep me reading for a few days. Thank you very much!
0

I'm speculating, but you may have misunderstood what the parameter to exec really is. It's not a list of commands to be executed (effectivly a batch/shell script), but a single command WITH it's arguments to be executed by a shell. Making it a one-liner like Pepelac suggests or putting the series of commands into a single file that you execute with exec later may be what you are looking for. For the command you are trying to execute there is absolute no reason why you can not make it a one-liner with the full source path included (instead of changing to it), but there may be other reasons why you need to do this that you have not mentioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.