0

这是我的dos命令代码。

我需要将当前文件名从我的重命名批处理工具中提取dos command到运行中。很确定我只是打破命令

add +filename+ with quotes like "cmd /c \\Converter.exe \\android\\"+filename+" -android -o \\android\\"+filename

但是我的文件在另一个class. eclipse 建议getfilename(null)不起作用。

我怎样才能做到这一点?下面我尝试了通配符,这当然不起作用。

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class doscommand {         
    public static void run() {            
        try {
                 Runtime rt = Runtime.getRuntime();                 
             //Process pr = rt.exec("cmd /c dir");                 
             Process pr = rt.exec("cmd /c \\Converter.exe \\android\\*.sdtid -android -o \\android\\*.txt");                  
             BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));                  
             String line=null;                 
                 while((line=input.readLine()) != null) {                     
                         System.out.println(line);
                 }                  
                 int exitVal = pr.waitFor();                 
                 System.out.println("Exited with error code "+exitVal);              
            } catch(Exception e) {                 
            System.out.println(e.toString());                 
             e.printStackTrace();             
           }         
 }
} 

重命名代码

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 ){
            doscommand.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
4

2 回答 2

0

我还没有阅读 Java 代码,但你的批处理代码对我来说看起来很不对劲。

你不应该有\\Converter.exe一个文件名。应该是Converter.exe吧?

除非您在服务器共享上使用文件,否则在这种情况下,您需要转义反斜杠并提供像\\\\server\\Converter.exe.

"\\android\\"+filenamewhich should be相同"android\\"+filename。(我假设您正在使用\\*.sdtid转义引号,不确定这在 Java 中是否与在 C# 中相同)。

批处理命令应该看起来更像

"cmd /c Converter.exe android\\"+filename+" -android -o android\\"+filename

如果您需要在空格的情况下引用文件名,我想您需要转义它,再次在 C# 中这将是\".

如果这不是问题,那么我建议您调试其余代码和/或改进您的问题。

于 2012-06-25T16:12:49.410 回答
0

我终于弄清楚了问题所在。\\android\\"+filename worked

于 2012-06-25T20:38:26.960 回答