bat 执行cmd 命令

1):执行java命令,定时删除指定路径下30天之前的的文件(/k:执行完毕后窗后不关闭)

deletearchivelog.bat

start "" "cmd /k "java DeleteFilesSimpe""

 

DeleteFilesSimpe.class

package com.vtradex.stms.server.utils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DeleteFilesSimpe {
	public static SimpleDateFormat dmy_hms = 
			new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
	public static void main(String[] args) {
		 String filePath = "D:/app/archivelog";
		while(true){
			int l = moveFileToReady(filePath);
			if(l>0){
				System.out.println(dmy_hms.format(
						new Date())+","+filePath+"----------"+l);
			}else{
				System.out.println(dmy_hms.format(
						new Date())+","+filePath);
			}
			try {
				//1000=1s,1h=60m,1m=60s,10800000=3h,3600000=1h
				Thread.sleep(10800000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	private static Integer moveFileToReady(String fromDir){
		File srcDir = new File(fromDir);
		if (!srcDir.exists()) {
			return 0;
		}
		File[] files = srcDir.listFiles();
		if (files == null || files.length <= 0) {
			return 0;
		}
		int l = 0;
		Date today = new Date();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				try {
					File ff = files[i];
			        long time=ff.lastModified();
				    Calendar cal=Calendar.getInstance();   
			        cal.setTimeInMillis(time);   
			        Date lastModified = cal.getTime();
			      //(int)(today.getTime() - lastModified.getTime())/86400000;
			        long days = getDistDates(today, lastModified);
			        if(days>=30){
			        	files[i].delete();
			        	l++;
			        }
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return l;
	}
	
	 /** 
     * @param startDate 
     * @param endDate 
     * @return 
     * @throws ParseException 
     */  
    public static long getDistDates(Date startDate,Date endDate)    
    {  
        long totalDate = 0;  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(startDate);  
        long timestart = calendar.getTimeInMillis();  
        calendar.setTime(endDate);  
        long timeend = calendar.getTimeInMillis();  
        totalDate = Math.abs((timeend - timestart))/(1000*60*60*24);  
        return totalDate;  
    }  
}

 

2):执行ping命令

cmd.bat

start "" "cmd /k "ping 192.168.1.109""

你可能感兴趣的:(cmd)