0

I am currently learning java and I encountered this problem. I am not sure if it can be done like this as I am still in the learning stage. So in my java main coding:

import java.io.File;
import java.io.IOException;

public class TestRun1
{
    public static void main(String args[])
    {

        //Detect usb drive letter
        drive d = new drive();
        System.out.println(d.detectDrive());

        //Check for .raw files in the drive (e.g. E:\)
        MainEntry m = new MainEntry();
        m.walkin(new File(d.detectDrive()));

        try
        {
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec("cmd /c start d.detectDrive()\\MyBatchFile.bat");
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

The "cmd /c start d.detectDrive()\MyBatchFile.bat" does not work. I do not know how to replace the variable.

And i created a batch file (MyBatchFile.bat):

@echo off
set Path1 = d.detectDrive()
Path1
pause
set Path2 = m.walkin(new File(d.detectDrive()))
vol231.exe -f Path2 imageinfo > Volatility.txt
pause
exit

It does not work. Please do not laugh.

I really isn't good in programming since I just started on java and batch file. Can anyone help me with it? I don't want to hard code it to become a E: or something like that. I want to make it flexible. But I have no idea how to do it. I sincerely ask for any help.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Linify
  • 227
  • 4
  • 13

1 Answers1

1

Procedure:

You should append the return value of the method which detects the drive, to the filename and compose the proper Batch command string.

Steps:

Get the return value of the method

  • String drive = d.detectDrive();
  • so, drive contains the value E:

append the value of drive to the filename

  • drive+"\MyBatchFile.bat"
  • so, we have E:\MyBatchFile.bat

append the result the batch command

  • cmd /c start "+drive+"\MyBatchFile.bat
  • result is cmd /c start E:\MyBatchFile.bat

So to invoke the batch command, the final code should be as follows:

    try {
        System.out.println(d.detectDrive()); 
        Runtime rt = Runtime.getRuntime();
        String drive = d.detectDrive();
        // <<---append the return value to the compose Batch command string--->>
        Process p = rt.exec("cmd /c start "+drive+"\\MyBatchFile.bat");
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
  • What do you mean by **You should append the return value of the method which detects the drive, to the filename and compose the proper Batch command string**??? Do you mind explaining? I did not really understand. Sorry – Linify Jul 15 '14 at 06:00
  • Edited. I broke down the steps so that you can understand better. – Infinite Recursion Jul 15 '14 at 06:04
  • For better understanding, you can print it `System.out.println("cmd /c start "+drive+"\\MyBatchFile.bat");` – Infinite Recursion Jul 15 '14 at 06:07
  • So part of my questions are solved. Thank you, the coding works. Is there a way to create a file of commands so that my java can run? Because a comment by alfsin above mentioned that java code cannot be included in batch file. So for example in volatility (a CLI software), a command: `vol231.exe -f E:\KOHMOHOJOJO-PC-20140714-152414.raw imageinfo > Volatility.txt`. Is it possible to change `E:\KOHMOHOJOJO-PC-20140714-152414.raw` with `m.walkin(new File(d.detectDrive()));`??? I do not want to hard code it. Please do inform me with u r unable to understand what I am saying. I'm bad with words. – Linify Jul 15 '14 at 06:17
  • so it is possible to add java code into batch file??? i tried `vol231.exe -f m.walkin(new File(d.detectDrive())) imageinfo > Volatility.txt` into the batch file but it cannot work. Okay, I will wait for you to append the solution. I will refresh this page again later. Thanks – Linify Jul 15 '14 at 06:35
  • I think you misunderstood my meaning. Here is my "more explained" question. Click this --> [link](http://stackoverflow.com/questions/24752040/alternatives-to-batch-file-since-i-am-unable-to-include-java-code-in-it) – Linify Jul 15 '14 at 07:12