1. 程式人生 > >刪除檔案-萬用字元

刪除檔案-萬用字元

公司用到這個了,把原有的Del檔案修改了一下

 public Boolean DeleteFile() {
        String sourceFileName = "";
        if (sSMBSourceFile == null) {
            sourceFileName = new File(sSourceFile).getName();           
            if (sourceFileName.indexOf("*") >= 0) {
                return DeleteSign(sSourceFile);
            } else {
                File fFile = new File(sSourceFile);
                if (fFile.isFile()) {
                    /*
                     * Set the file , can be deleted
                     */
                    if (!fFile.canWrite()) {
                        fFile.setWritable(true);
                    }
                    return fFile.delete();
                } else if (fFile.isDirectory()) {
                    return pDeleteDirectory(sSourceFile);
                } else {
                    return true;
                }
            }
        } else {
            sourceFileName = sSMBSourceFile.getName();
            if (sourceFileName.indexOf("*") >= 0) {
                return DeleteSign(sSourceFile);
            } else {
                if (sourceExist) {
                    return deleteSMBFile(sSMBSourceFile);
                } else {
                    return true;
                }
            }
        }
    }

    private Boolean DeleteSign(String sSrcPath) {
        String sTmpFile = "";
        if (sSrcPath.indexOf("smb://") != -1) {
            SmbFile fTmpFile = defineSmbFile(sSrcPath);
            if (fTmpFile == null) {
                return false;
            }
            sTmpFile = fTmpFile.getName();
        } else {
            File fTmpFile = new File(sSrcPath);
            sTmpFile = fTmpFile.getName();
        }
        if (sTmpFile.length() <= 1) {
            AddLog(EnumLogType.LOGTYPE_ERROR, EnumBuildStatus.BSTATUS_ONGOING, "Incorrect filename: " + sSrcPath);
            return false;
        }
        String sTPF = "*";
        String sTPF2 = "*";
        if (sTmpFile.indexOf("*.*") == -1) {
            if (sTmpFile.substring(0, 2).equals("*.")) {
                sTPF2 = sTmpFile.substring(2).toUpperCase();
            } else if (sTmpFile.substring(sTmpFile.length() - 2).equals(".*")) {
                sTPF = sTmpFile.substring(0, sTmpFile.length() - 2).toUpperCase();
            } else {
                AddLog(EnumLogType.LOGTYPE_ERROR, EnumBuildStatus.BSTATUS_ONGOING, "Invalid filename, please use *.X or X.*: " + sSrcPath);
                return false;
            }
        }
        Boolean bTotalRlt = true;
        if (sSrcPath.indexOf("smb://") != -1) {
            SmbFile file;
            try {
                file = defineSmbFile(new SmbFile(sSrcPath).getParent());
                if (file != null) {
                    SmbFile[] fileList = file.listFiles();
                    if (fileList != null) {                        
                        for (int i = 0; i < fileList.length; i++) {
                            if (fileList[i].isFile()) {
                                String sTmpSrc = fileList[i].getPath();
                                String fileName = fileList[i].getName();
                                String fileFirstName = fileName;
                                String fileLastName = "";
                                if (fileName.lastIndexOf(".") != -1) {
                                    fileFirstName = fileName.substring(0, fileName.lastIndexOf("."));
                                    if (fileName.length() > fileName.lastIndexOf(".") + 1) {
                                        fileLastName = fileName.substring(fileName.lastIndexOf(".") + 1);
                                    }
                                }
                                if (sTPF.equals("*") && !sTPF2.equals("*")) {
                                    if (!sTPF2.equals(fileLastName.toUpperCase())) {
                                        continue;
                                    }
                                }
                                if (!sTPF.equals("*") && sTPF2.equals("*")) {
                                    if (!sTPF.equals(fileFirstName.toUpperCase())) {
                                        continue;
                                    }
                                }                            
                                if (!deleteSMBFile(fileList[i])) {
                                    AddLog(EnumLogType.LOGTYPE_ERROR, EnumBuildStatus.BSTATUS_ONGOING, "Del failed from [" + sTmpSrc + "] ");
                                    bTotalRlt = false;
                                    break;
                                } else {
                                    bTotalRlt = true;
                                }
                            }
                        }
                    }
                } else {
                    return false;
                }
            } catch (Exception ex) {
                Logger.getLogger(CCommFileOpt.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            File file = new File(sSrcPath.substring(0, sSrcPath.lastIndexOf("/")));
            File[] fileList = file.listFiles();                        
            if (fileList != null) {               
                for (int i = 0; i < fileList.length; i++) {
                    if (fileList[i].isFile()) {
                        String sTmpSrc = fileList[i].getAbsolutePath().replace("\\", "/");
                        String fileName = fileList[i].getName();
                        String fileFirstName = fileName;
                        String fileLastName = "";
                        if (fileName.lastIndexOf(".") != -1) {
                            fileFirstName = fileName.substring(0, fileName.lastIndexOf("."));
                            if (fileName.length() > fileName.lastIndexOf(".") + 1) {
                                fileLastName = fileName.substring(fileName.lastIndexOf(".") + 1);
                            }
                        }
                        if (sTPF.equals("*") && !sTPF2.equals("*")) {
                            if (!sTPF2.equals(fileLastName.toUpperCase())) {
                                continue;
                            }
                        }
                        if (!sTPF.equals("*") && sTPF2.equals("*")) {
                            if (!sTPF.equals(fileFirstName.toUpperCase())) {
                                continue;
                            }
                        }
                        if (!fileList[i].canWrite()) {
                            fileList[i].setWritable(true);
                        }                        
                        bTotalRlt = bTotalRlt && fileList[i].delete();
                        if (!bTotalRlt) {
                            AddLog(EnumLogType.LOGTYPE_ERROR, EnumBuildStatus.BSTATUS_ONGOING, "Del failed from [" + sTmpSrc + "] ");
                            bTotalRlt = false;
                            break;
                        } else {
                            bTotalRlt = true;
                        }
                    }
                }               
            }
        }
        return bTotalRlt;
    }