删除文件-通配符

公司用到这个了,把原有的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;
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值