Java exec executeCD(String dirPath)

Here you can find the source of executeCD(String dirPath)

Description

execute CD

License

Open Source License

Declaration

public static String executeCD(String dirPath) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    public static final String ROOT = "user.dir";

    public static String executeCD(String dirPath) {
        // TODO Auto-generated method stub
        String retStatus = null;/*from www  . j a  v  a 2 s  .c o  m*/
        try {

            if (dirPath.equals("..")) {
                File curr = new File(executePWD());
                if (curr.getAbsoluteFile().getParent() != null)
                    System.setProperty(ROOT, curr.getAbsoluteFile().getParent());
                else
                    System.out.println("::: Reached root directory :::" + ROOT);
                retStatus = "Directory changed successfully";
            } else {
                File current = new File(System.getProperty(ROOT) + File.separator + dirPath);
                System.out.println("Inside CD ::" + current.getAbsolutePath());
                if (current.exists() && current.isDirectory()) {
                    System.setProperty(ROOT, current.getAbsoluteFile().getPath());
                    retStatus = ":::: Directory changed successfully ::::";
                } else
                    retStatus = "Directory does not exist. Enter correct directory name and try again.";
            }

        } catch (Exception e) {
            System.out.println("----Exception in CD command execution -----" + e.getMessage());
        }

        return retStatus;
    }

    public static String executePWD() {
        // TODO Auto-generated method stub
        String currentDirectory = null;
        try {
            currentDirectory = System.getProperty(ROOT);
        } catch (Exception e) {
            System.out.println("----Exception in PWD command execution -----" + e.getMessage());
        }
        return currentDirectory;
    }
}

Related

  1. execute2(final String appPath)
  2. executeAndWait(String command)
  3. executeApplication(String filePath)
  4. executeBashScriptWithParams(ArrayList script)
  5. executeBatFile(String batFile)
  6. executeCmdCommand(String command)
  7. executeCmdForReader(String cmd)
  8. executeComand(String[] comand)
  9. executeCommand(final ProcessBuilder pb)