2012-06-22 103 views
0

我有一个工作文件重命名java工具,现在我想添加一个if条件来运行命令行命令,如果我在重命名每个文件的重命名过程中选中了一个框作为其一部分。将命令行命令添加到文件重命名批处理

我将在稍后更改dos代码,但它的样本我发现有效。我的部分问题是我的filerename是它自己的类,所以我还需要弄清楚如何组合这个类或者从我的主重命名类中引用dos命令。

更新

我更新与答案修改代码,但命令行命令不工作,它崩溃的Java,没有错误。该命令从cmd行开始工作。

import java.io.IOException; 
import java.io.InputStream; 

public class doscommandrun { 
    public static void run() { 
     final String dosCommand = "cmd converter.exe file.doc -android -o file.txt"; 
      try { 
       final Process process = Runtime.getRuntime().exec(
         dosCommand + " "); 
       final InputStream in = process.getInputStream(); 
       int ch; 
        while((ch = in.read()) != -1) { 
         System.out.print((char)ch); 
        } 
        } catch (IOException e) { 
         e.printStackTrace(); 
      } 
     } 
    } 

enter code here文件重命名代码:

private void renameFile(){ 

    boolean operationResult = false; 
    boolean overallResult = true; 
    int failCount = 0; 

    /* the operation of this part is ensured by the chooseDirectory() 
    * WE get the list of files in the directory 
    * get the conditions set by users 
    * and perform the file rename operation. 
    */ 

    //Let's get all the information from user 
    String[] fileList = directory.list(); //the list of files in the directory 
    String Prefix = txtPrefix.getText(); 
    String Rename = txtRename.getText(); 
    String Suffix = txtSuffix.getText(); 
    String digits = (String) cboSequence.getSelectedItem(); 
    int StartingNum; 
    String generatedSequence; 
    File oldFile; 

    //let's call the output frame 
    if(cbxOutput.isSelected() && OUTPUT_ON == false){ 
     buildOutput(); 
     OUTPUT_ON = true; 
    } 




    //display the list of files and readability of each file 
    for(int i = 0; i < fileList.length; i++){ 
     oldFile = new File(directory.getPath()+"/"+ fileList[i]); 
     String readability = fileList[i] +" - readable?: "+oldFile.canRead(); 
     System.out.println(readability); 

     if(OUTPUT_ON) 
      txaOutput.append("\n"+readability); 
    } 

    for(int i = 0; i < fileList.length; i++){ 

     /* get the file extension that we need, and form a new name, 
     * we would check if the Ignore File Extension is selected 
     */ 
     oldFile = new File(directory.getPath()+"/"+ fileList[i]); 

     String fileExtension; 

     if(cbxIgnoreExtension.isSelected() == true){ 
      fileExtension = ""; 
     } 
     else 
      fileExtension = getFileExtension(fileList[i]); 

     //this part get the original filename  
     String fileName = getFileName(fileList[i]); 



     String inputInfo = "The input filename->"+ fileList[i] + "\nfile name->" + fileName + "\nextension->" + fileExtension; 
     System.out.println(inputInfo); 

     if(OUTPUT_ON) 
      txaOutput.append("\n"+inputInfo); 



     /* generate sequence for the Name 
     *if the digits selection is NONE, we ignore it 
     */ 
     if(digits.equals("None") == true){ 
      generatedSequence = ""; 
     } 
     else{ 
      StartingNum = Integer.parseInt(txtSequence.getText()); 
      generatedSequence = nameSequence(StartingNum + i, digits); 
     } 




     //this is affected by the RenameOption, if Rename has something then only we RENAME 
     if(cbxRename.isSelected() == true){ 
      fileName = Rename + generatedSequence; //the fileName will change. 
     } 
     else{ 
      //if Rename has nothing, but the txtSequence has some Value, we take it to the naming too 
      fileName = fileName.substring(0,4)+ generatedSequence; 
      if(cbxAndroid.isSelected() == true){ 
       doscommandrun.run(); 
       } 



     //the New File Name 
     String newFileName = Prefix + fileName.substring(0,4) + Suffix + fileExtension; 
     String tentativeName = "new Filename will be ->"+newFileName+"\n"; 
     System.out.println(tentativeName); 

     if(OUTPUT_ON) 
      txaOutput.append("\n"+tentativeName); 




     // ! Perform the file rename, if the Experimental Mode is not selected 
     if(cbxExperiment.isSelected() == false){ 

      operationResult = oldFile.renameTo(new File(directory.getPath()+"/"+newFileName)); 
      String renameResult = "\t*Rename successfully?: " + operationResult+"\n\n"; 
      System.out.println(renameResult); 
       if(operationResult == false) 
        failCount++; 

       if(OUTPUT_ON) 
        txaOutput.append("\n"+renameResult); 

      //make up the overall result 
      overallResult = (operationResult && overallResult); 
     } 

    } 

    if(cbxExperiment.isSelected() == false){ 
     System.out.println("Overall Result: "+overallResult); 
     if(overallResult) 
      JOptionPane.showMessageDialog(null, "All files renamed successfully!"); 
     else 
      JOptionPane.showMessageDialog(null, "File renamed with "+ failCount+ " failure(s)"); 
    }//end if 
    } 

}//end renameFile 
+0

请让你的问题更具体。很难理解你在问什么 – pb2q

+0

@jerhynsoen你必须从你的过程的输入和错误流中读取数据。否则,当发生错误时您会遇到问题。为此,你将不得不使用线程。 – Andy

回答

0

我的CMD后添加/ C在我renametool类

我仍然有问题,但枯萎一个不同的问题。

0

您可以创建一个名为run()的doccommandrun类的静态方法将执行什么是目前的主要方法调用时。

public class DosCommandRun 
{ 
    public static void run() 
    { 
     //..do stuff from main 
    } 
} 

现在,只要你想调用dos命令,你可以在你的代码中插入DosCommandRun.run()

+0

我使用我的代码来完成您的建议,但这并不奏效。我更新这个队列中的代码以反映这些更改。 – jerhynsoen