我目前正在使用任务计划程序和使用xcopy的批处理文件每隔6小时将桌面上的文件夹备份到另一个文件夹。比如说,如何删除超过12个备份之前的备份?例如,如果我已经有12个备份并且它是下一个6小时标记,它会备份源文件夹,然后删除最旧的备份,就像现在的那样,只有那里只有#13 12.这会重复,所以它会循环备份。
我的文件夹格式如下:YYYY-MM-DD_HHMM
(24小时格式)
答案 0 :(得分:1)
你不是很清楚。假设您的备份是文件夹,其名称类似于import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import com.google.gson.Gson;
public class Recipe {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
Gson gson = new Gson();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
/* StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
} */
Roo roo=gson.fromJson(value.toString(),Roo.class);
if(roo.cookTime!=null)
{
word.set(roo.cookTime);
}
else
{
word.set("none");
}
context.write(word, one);
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
/* for ( String string : otherArgs) {
System.out.println(string);
}*/
if (otherArgs.length != 2) {
System.err.println("Usage: recipe <in> <out>");
System.exit(2);
}
@SuppressWarnings("deprecation")
Job job = new Job(conf, "Recipe");
job.setJarByClass(Recipe.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// FileInputFormat.addInputPath(job, new Path("hdfs://127.0.0.1:9000/in"));
// FileOutputFormat.setOutputPath(job, new Path("hdfs://127.0.0.1:9000/out"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
// job.submit();
}
}
class Id
{
public String oid;
}
class Ts
{
public long date ;
}
class Roo
{
public Id _id ;
public String name ;
public String ingredients ;
public String url ;
public String image ;
public Ts ts ;
public String cookTime;
public String source ;
public String recipeYield ;
public String datePublished;
public String prepTime ;
public String description;
}
BackupYYYYMMDD
为您提供备份文件夹(简单格式,仅限目录,按名称排序(从最新开始反转)*)
将其放入dir /b /ad /o-n backup*
以通过跳过12行来解析输出,从而产生:
for /f
当输出似乎正常时,删除for /f "skip=12 tokens=*" %%i in ('dir /b /ad /o-n backup*') do ECHO rd /s /q %%i
。
*)如果您有其他日期格式,请更好地使用ECHO
(按日期排序,最新排序)
对于名为/o-d
的备份文件夹,您可以使用YYYY-MM-DD_HHMM
来最大限度地减少计算或影响任何其他文件夹的机会。