Note that there are some explanatory texts on larger screens.

plurals
  1. POadd a commandline command to a file rename batch
    primarykey
    data
    text
    <p>I have a working file rename java tool, now I want to add an if condition to run a commandline command if I checked a box as part of the rename process on each file it renames.</p> <p>I will be changing the dos code later, but its a sample I found that works. Part of my problem is my filerename is its own class, so I will also need to figure out how to combine this class or reference the dos command somehow from my main rename class.</p> <p><strong>update</strong></p> <p>I updated the code with the changes from the answer, but the commandline command does not work and it crashes java with no error. The command does work from cmd line.</p> <pre><code>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(); } } } </code></pre> <p><code>enter code here</code>File rename code:</p> <pre><code>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() &amp;&amp; OUTPUT_ON == false){ buildOutput(); OUTPUT_ON = true; } //display the list of files and readability of each file for(int i = 0; i &lt; 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 &lt; 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-&gt;"+ fileList[i] + "\nfile name-&gt;" + fileName + "\nextension-&gt;" + 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 -&gt;"+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 &amp;&amp; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload